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
This commit is contained in:
Jacques 2024-04-25 08:39:37 +02:00
parent fddf100fb0
commit f09037b850
8 changed files with 136 additions and 4 deletions

View file

@ -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`.

View file

@ -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';

View file

@ -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> {
int super.initialValue = 0,
int minValue = 0,
int maxValue = 100,
Axis axis = Axis.vertical,
}) : super(
builder: (FormFieldState<int> state) => NumberPicker(
minValue: minValue,
@ -55,7 +59,9 @@ class NumberPickerFormField extends FormField<int> {
state.didChange(value);
},
itemHeight: 35,
itemWidth: 35,
itemCount: 5,
axis: axis,
),
);
}

View file

@ -182,8 +182,8 @@ class NumberPickerState extends State<NumberPicker> {
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),

View file

@ -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<RadioItem> 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<RadioItem?> {
RadioPickerFormField({
required FormFieldSetter<RadioItem> super.onSaved,
required List<RadioItem> items,
void Function(RadioItem value)? onChanged,
super.initialValue,
super.key,
}) : super(
builder: (FormFieldState<RadioItem?> state) => RadioPicker(
onChanged: (value) {
onChanged?.call(value);
state.didChange(value);
},
items: items,
initialValue: initialValue,
),
);
}
class RadioItem<T> {
/// 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;
}

View file

@ -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<RadioItem> items;
@override
State<RadioPicker> createState() => _RadioPickerState();
}
class _RadioPickerState extends State<RadioPicker> {
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<RadioItem>(
value: item,
groupValue: value,
onChanged: (v) {
if (v != null) {
setState(() {
value = v;
});
widget.onChanged(v);
}
},
),
],
),
],
],
);
}

View file

@ -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: