From f09037b8507140e2ae61e501df2f69a0773062c6 Mon Sep 17 00:00:00 2001 From: Jacques Date: Thu, 25 Apr 2024 08:39:37 +0200 Subject: [PATCH] feat: add radio button to the input library. Made some changes to the number pciker and added the axis parameter. change the format of the outcoming value of the showDateRangePicker --- CHANGELOG.md | 5 ++ .../inputs/date_picker/date_picker_field.dart | 2 +- lib/src/inputs/inputs.dart | 1 + .../inputs/number_picker/number_picker.dart | 6 ++ .../number_picker/number_picker_field.dart | 4 +- lib/src/inputs/radio/radio_picker.dart | 72 +++++++++++++++++++ lib/src/inputs/radio/radio_picker_field.dart | 48 +++++++++++++ pubspec.yaml | 2 +- 8 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 lib/src/inputs/radio/radio_picker.dart create mode 100644 lib/src/inputs/radio/radio_picker_field.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index b2da3ff..1a598e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,3 +76,8 @@ ## 3.2.1 * Added `PhoneNumber` model to save the `FlutterFormInputPhone` result. * Added more customization for `FlutterFormInputPhone`. + +## 3.3.0 +* Added `FlutterFormInputRadioPicker` +* Changed the `FlutterFormInputNumberPicker` and added axis parameter. +* Changed the formating of the result value of the `showDateRangePicker`. diff --git a/lib/src/inputs/date_picker/date_picker_field.dart b/lib/src/inputs/date_picker/date_picker_field.dart index 576fe35..8eede5b 100644 --- a/lib/src/inputs/date_picker/date_picker_field.dart +++ b/lib/src/inputs/date_picker/date_picker_field.dart @@ -142,7 +142,7 @@ class _DateInputFieldState extends State { initialDateRange: initialDateRange, ).then( (value) => value != null - ? '${widget.dateFormat.format(value.start)} -' + ? '${widget.dateFormat.format(value.start)} - ' '${widget.dateFormat.format(value.end)}' : '', ); diff --git a/lib/src/inputs/inputs.dart b/lib/src/inputs/inputs.dart index d0825a7..f3671d9 100644 --- a/lib/src/inputs/inputs.dart +++ b/lib/src/inputs/inputs.dart @@ -5,6 +5,7 @@ export 'number_picker/number_picker.dart'; export 'phone/countries.dart'; export 'phone/phone.dart'; export 'phone/phone_number_model.dart'; +export 'radio/radio_picker.dart'; export 'scroll_picker/scroll_picker.dart'; export 'slider/slider.dart'; export 'text/password.dart'; diff --git a/lib/src/inputs/number_picker/number_picker.dart b/lib/src/inputs/number_picker/number_picker.dart index 67fc8d0..219d538 100644 --- a/lib/src/inputs/number_picker/number_picker.dart +++ b/lib/src/inputs/number_picker/number_picker.dart @@ -15,6 +15,7 @@ class FlutterFormInputNumberPicker extends StatelessWidget { this.onChanged, this.initialValue, this.validator, + this.axis = Axis.vertical, }) : assert(minValue < maxValue, 'minValue must be less than maxValue'); final int minValue; @@ -23,6 +24,7 @@ class FlutterFormInputNumberPicker extends StatelessWidget { final String? Function(int?)? validator; final int? initialValue; final Function(int?)? onChanged; + final Axis axis; @override Widget build(BuildContext context) => NumberPickerFormField( @@ -32,6 +34,7 @@ class FlutterFormInputNumberPicker extends StatelessWidget { validator: (value) => validator?.call(value), onChanged: (value) => onChanged?.call(value), initialValue: initialValue ?? 0, + axis: axis, ); } @@ -44,6 +47,7 @@ class NumberPickerFormField extends FormField { int super.initialValue = 0, int minValue = 0, int maxValue = 100, + Axis axis = Axis.vertical, }) : super( builder: (FormFieldState state) => NumberPicker( minValue: minValue, @@ -55,7 +59,9 @@ class NumberPickerFormField extends FormField { state.didChange(value); }, itemHeight: 35, + itemWidth: 35, itemCount: 5, + axis: axis, ), ); } diff --git a/lib/src/inputs/number_picker/number_picker_field.dart b/lib/src/inputs/number_picker/number_picker_field.dart index 2b8d854..056af3c 100644 --- a/lib/src/inputs/number_picker/number_picker_field.dart +++ b/lib/src/inputs/number_picker/number_picker_field.dart @@ -182,8 +182,8 @@ class NumberPickerState extends State { children: [ Center( child: Container( - width: 300, - height: 45, + width: widget.itemWidth, + height: widget.itemHeight, decoration: BoxDecoration( borderRadius: BorderRadius.circular(5), color: const Color(0xFFD8D8D8).withOpacity(0.50), diff --git a/lib/src/inputs/radio/radio_picker.dart b/lib/src/inputs/radio/radio_picker.dart new file mode 100644 index 0000000..6887968 --- /dev/null +++ b/lib/src/inputs/radio/radio_picker.dart @@ -0,0 +1,72 @@ +// SPDX-FileCopyrightText: 2024 Iconica +// +// SPDX-License-Identifier: BSD-3-Clause + +// ignore: depend_on_referenced_packages +import 'package:collection/collection.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_input_library/src/inputs/radio/radio_picker_field.dart'; + +class FlutterFormInputRadioPicker extends StatelessWidget { + const FlutterFormInputRadioPicker({ + required this.items, + super.key, + this.onSaved, + this.onChanged, + this.initialValue, + }); + + final Function(RadioItem?)? onSaved; + final String? initialValue; + final Function(RadioItem?)? onChanged; + final List items; + + @override + Widget build(BuildContext context) => RadioPickerFormField( + onSaved: (value) => onSaved?.call(value), + onChanged: (value) => onChanged?.call(value), + initialValue: items.firstWhereOrNull((i) => i.value == initialValue), + items: items, + ); +} + +class RadioPickerFormField extends FormField { + RadioPickerFormField({ + required FormFieldSetter super.onSaved, + required List items, + void Function(RadioItem value)? onChanged, + super.initialValue, + super.key, + }) : super( + builder: (FormFieldState state) => RadioPicker( + onChanged: (value) { + onChanged?.call(value); + + state.didChange(value); + }, + items: items, + initialValue: initialValue, + ), + ); +} + +class RadioItem { + /// Creates an item for a radio menu. + /// + /// The [child] argument is required. + const RadioItem({ + required this.child, + required this.value, + this.onTap, + }); + + /// Called when the dropdown menu item is tapped. + final VoidCallback? onTap; + + /// The value to return if the user selects this menu item. + /// + /// Eventually returned in a call to [DropdownButton.onChanged]. + final T value; + + final Widget child; +} diff --git a/lib/src/inputs/radio/radio_picker_field.dart b/lib/src/inputs/radio/radio_picker_field.dart new file mode 100644 index 0000000..f128cbf --- /dev/null +++ b/lib/src/inputs/radio/radio_picker_field.dart @@ -0,0 +1,48 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_input_library/src/inputs/radio/radio_picker.dart'; + +class RadioPicker extends StatefulWidget { + const RadioPicker({ + required this.onChanged, + required this.items, + this.initialValue, + super.key, + }); + + final RadioItem? initialValue; + final Function(RadioItem) onChanged; + final List items; + + @override + State createState() => _RadioPickerState(); +} + +class _RadioPickerState extends State { + late var value = widget.initialValue; + + @override + Widget build(BuildContext context) => Wrap( + children: [ + for (var item in widget.items) ...[ + Row( + mainAxisSize: MainAxisSize.min, + children: [ + item.child, + Radio( + value: item, + groupValue: value, + onChanged: (v) { + if (v != null) { + setState(() { + value = v; + }); + widget.onChanged(v); + } + }, + ), + ], + ), + ], + ], + ); +} diff --git a/pubspec.yaml b/pubspec.yaml index fc64213..91ca5e3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_input_library description: A new Flutter package project. -version: 3.2.1 +version: 3.3.0 repository: https://github.com/Iconica-Development/flutter_input_library environment: