From 1eeaf653597cea76c2385024bf848c63e543b22d Mon Sep 17 00:00:00 2001 From: Jacques Doeleman Date: Wed, 12 Oct 2022 16:30:14 +0200 Subject: [PATCH 1/7] Added onChange to formInputControllers --- example/lib/form_example.dart | 4 +++ example/pubspec.lock | 28 ------------------- lib/src/widgets/input/abstractions.dart | 17 +++++++---- .../input_carousel/carousel_form.dart | 3 ++ .../input_carousel/input_carousel.dart | 17 +++++++---- .../input/input_types/input_email.dart | 13 ++++++--- .../input_number_picker.dart | 24 ++++++++++------ .../input_password/input_password.dart | 12 +++++--- .../input_types/input_password/password.dart | 1 + .../input/input_types/input_plain_text.dart | 17 +++++++---- .../input_slider/input_slider.dart | 16 +++++++---- .../input_types/input_slider/slider.dart | 3 ++ pubspec.yaml | 3 -- 13 files changed, 86 insertions(+), 72 deletions(-) diff --git a/example/lib/form_example.dart b/example/lib/form_example.dart index 0d7c025..8c395ea 100644 --- a/example/lib/form_example.dart +++ b/example/lib/form_example.dart @@ -23,6 +23,7 @@ class _FormExampleState extends ConsumerState { checkPageTitle: (dynamic amount) { return "Age: $amount years"; }, + onChanged: (value) => print(value), ); late final FlutterFormInputCarouselController carouselInputController; @@ -49,6 +50,7 @@ class _FormExampleState extends ConsumerState { checkPageTitle: (dynamic firstName) { return "First Name: $firstName"; }, + onChanged: (value) => print(value), ); FlutterFormInputPlainTextController lastNameController = @@ -58,6 +60,7 @@ class _FormExampleState extends ConsumerState { checkPageTitle: (dynamic lastName) { return "Last Name: $lastName"; }, + onChanged: (value) => print(value), ); @override @@ -71,6 +74,7 @@ class _FormExampleState extends ConsumerState { checkPageDescription: (dynamic index) { return cars[index]["description"]; }, + onChanged: (value) => print(value), ); } diff --git a/example/pubspec.lock b/example/pubspec.lock index c0b7486..864d646 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -36,13 +36,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.16.0" - crypto: - dependency: transitive - description: - name: crypto - url: "https://pub.dartlang.org" - source: hosted - version: "3.0.2" cupertino_icons: dependency: "direct main" description: @@ -154,13 +147,6 @@ packages: description: flutter source: sdk version: "0.0.99" - sliding_up_panel: - dependency: transitive - description: - name: sliding_up_panel - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.0+1" source_span: dependency: transitive description: @@ -210,20 +196,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.4.12" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.1" - uuid: - dependency: transitive - description: - name: uuid - url: "https://pub.dartlang.org" - source: hosted - version: "3.0.6" vector_math: dependency: transitive description: diff --git a/lib/src/widgets/input/abstractions.dart b/lib/src/widgets/input/abstractions.dart index 097b229..c439d20 100644 --- a/lib/src/widgets/input/abstractions.dart +++ b/lib/src/widgets/input/abstractions.dart @@ -10,7 +10,7 @@ import '/src/utils/formstate.dart' as fs; /// label is a standard parameter to normally sets the label of the input. /// /// [registerController] should be called to register the given [controller] to the form page. -abstract class FlutterFormInputWidget extends ConsumerWidget { +abstract class FlutterFormInputWidget extends ConsumerWidget { const FlutterFormInputWidget({ Key? key, required this.controller, @@ -19,7 +19,7 @@ abstract class FlutterFormInputWidget extends ConsumerWidget { }) : super(key: key); /// The [controller] which determines how the value is handled and how the value is shown on the checkpage. - final FlutterFormInputController controller; + final FlutterFormInputController controller; /// [label] is a standard parameter to normally sets the label of the input. final Widget? label; @@ -56,6 +56,8 @@ abstract class FlutterFormInputWidget extends ConsumerWidget { /// [checkPageDescription] is the same as checkPageTitle but for the description. /// If null no description will be shown. /// +/// [onChanged] can be set to get the value whenever the user changes it. +/// /// [onSaved] goes of when the save function is called for the page if [onValidate] return null. /// /// [onValidate] is used to validate the given input by the user. @@ -78,16 +80,19 @@ abstract class FlutterFormInputController { /// return "$amount persons"; /// }, /// ``` - String Function(T value)? checkPageTitle; + String Function(T? value)? checkPageTitle; /// [checkPageDescription] is the same as checkPageTitle but for the description. /// If null no description will be shown. - String Function(T value)? checkPageDescription; + String Function(T? value)? checkPageDescription; + + /// [onChanged] can be set to get the value whenever the user changes it. + void Function(T? value)? onChanged; /// [onSaved] goes of when the save function is called for the page if [onValidate] return null. - void onSaved(T value); + void onSaved(T? value); /// [onValidate] is used to validate the given input by the user. String? onValidate( - T value, String Function(String, {List? params}) translator); + T? value, String Function(String, {List? params}) translator); } diff --git a/lib/src/widgets/input/input_types/input_carousel/carousel_form.dart b/lib/src/widgets/input/input_types/input_carousel/carousel_form.dart index ed1b621..d4ea6d9 100644 --- a/lib/src/widgets/input/input_types/input_carousel/carousel_form.dart +++ b/lib/src/widgets/input/input_types/input_carousel/carousel_form.dart @@ -6,6 +6,7 @@ class CarouselFormField extends FormField { Key? key, required FormFieldSetter onSaved, required FormFieldValidator validator, + void Function(int value)? onChanged, int initialValue = 0, bool autovalidate = false, required List items, @@ -19,6 +20,8 @@ class CarouselFormField extends FormField { options: CarouselOptions( initialPage: initialValue, onPageChanged: (index, reason) { + onChanged?.call(index); + state.didChange(index); }, height: 425, diff --git a/lib/src/widgets/input/input_types/input_carousel/input_carousel.dart b/lib/src/widgets/input/input_types/input_carousel/input_carousel.dart index 2e9867d..de999b9 100644 --- a/lib/src/widgets/input/input_types/input_carousel/input_carousel.dart +++ b/lib/src/widgets/input/input_types/input_carousel/input_carousel.dart @@ -10,10 +10,10 @@ import 'carousel_form.dart'; /// [items] will be the [Widget]s to be displayed in the carousel. /// /// Standard controller is [FlutterFormInputCarouselController]. -class FlutterFormInputCarousel extends FlutterFormInputWidget { +class FlutterFormInputCarousel extends FlutterFormInputWidget { const FlutterFormInputCarousel({ Key? key, - required FlutterFormInputController controller, + required FlutterFormInputController controller, Widget? label, required this.items, }) : super(key: key, controller: controller, label: label); @@ -30,6 +30,7 @@ class FlutterFormInputCarousel extends FlutterFormInputWidget { return CarouselFormField( onSaved: (value) => controller.onSaved(value), validator: (value) => controller.onValidate(value, _), + onChanged: controller.onChanged, initialValue: controller.value ?? 0, items: items, ); @@ -47,6 +48,7 @@ class FlutterFormInputCarouselController this.value, this.checkPageTitle, this.checkPageDescription, + this.onChanged, }); @override @@ -59,19 +61,22 @@ class FlutterFormInputCarouselController bool mandatory; @override - String Function(int value)? checkPageTitle; + String Function(int? value)? checkPageTitle; @override - String Function(int value)? checkPageDescription; + String Function(int? value)? checkPageDescription; @override - void onSaved(int value) { + void Function(int? value)? onChanged; + + @override + void onSaved(int? value) { this.value = value; } @override String? onValidate( - int value, String Function(String, {List? params}) translator) { + int? value, String Function(String, {List? params}) translator) { if (mandatory) {} return null; diff --git a/lib/src/widgets/input/input_types/input_email.dart b/lib/src/widgets/input/input_types/input_email.dart index f635442..2ed9402 100644 --- a/lib/src/widgets/input/input_types/input_email.dart +++ b/lib/src/widgets/input/input_types/input_email.dart @@ -7,10 +7,10 @@ import '../../../../flutter_form.dart'; /// Input for an email used in a [FlutterForm]. /// /// Standard controller is [FlutterFormInputEmailController]. -class FlutterFormInputEmail extends FlutterFormInputWidget { +class FlutterFormInputEmail extends FlutterFormInputWidget { const FlutterFormInputEmail({ Key? key, - required FlutterFormInputController controller, + required FlutterFormInputController controller, Widget? label, }) : super( key: key, @@ -31,6 +31,7 @@ class FlutterFormInputEmail extends FlutterFormInputWidget { controller.onSaved(value); }, validator: (value) => controller.onValidate(value, _), + onChanged: (value) => controller.onChanged?.call(value), decoration: InputDecoration( focusColor: Theme.of(context).primaryColor, label: label ?? const Text("Email"), @@ -50,6 +51,7 @@ class FlutterFormInputEmailController this.value, this.checkPageTitle, this.checkPageDescription, + this.onChanged, }); @override @@ -62,10 +64,13 @@ class FlutterFormInputEmailController bool mandatory; @override - String Function(String value)? checkPageTitle; + String Function(String? value)? checkPageTitle; @override - String Function(String value)? checkPageDescription; + String Function(String? value)? checkPageDescription; + + @override + void Function(String? value)? onChanged; @override void onSaved(dynamic value) { diff --git a/lib/src/widgets/input/input_types/input_number_picker/input_number_picker.dart b/lib/src/widgets/input/input_types/input_number_picker/input_number_picker.dart index b9bb8c3..055f4b8 100644 --- a/lib/src/widgets/input/input_types/input_number_picker/input_number_picker.dart +++ b/lib/src/widgets/input/input_types/input_number_picker/input_number_picker.dart @@ -11,10 +11,10 @@ import 'numberpicker.dart'; /// [maxValue] sets the maximal value of the picker. /// /// Standard controller is [FlutterFormInputNumberPickerController]. -class FlutterFormInputNumberPicker extends FlutterFormInputWidget { +class FlutterFormInputNumberPicker extends FlutterFormInputWidget { const FlutterFormInputNumberPicker({ Key? key, - required FlutterFormInputController controller, + required FlutterFormInputController controller, Widget? label, this.minValue = 0, this.maxValue = 100, @@ -34,10 +34,9 @@ class FlutterFormInputNumberPicker extends FlutterFormInputWidget { return NumberPickerFormField( minValue: minValue, maxValue: maxValue, - onSaved: (value) { - controller.onSaved(value); - }, + onSaved: (value) => controller.onSaved(value), validator: (value) => controller.onValidate(value, _), + onChanged: (value) => controller.onChanged?.call(value), initialValue: controller.value ?? minValue, ); } @@ -51,6 +50,7 @@ class NumberPickerFormField extends FormField { Key? key, required FormFieldSetter onSaved, required FormFieldValidator validator, + void Function(int value)? onChanged, int initialValue = 0, bool autovalidate = false, int minValue = 0, @@ -66,6 +66,8 @@ class NumberPickerFormField extends FormField { maxValue: maxValue, value: initialValue, onChanged: (int value) { + onChanged?.call(value); + state.didChange(value); }, itemHeight: 35, @@ -82,6 +84,7 @@ class FlutterFormInputNumberPickerController this.value, this.checkPageTitle, this.checkPageDescription, + this.onChanged, }); @override @@ -94,19 +97,22 @@ class FlutterFormInputNumberPickerController bool mandatory; @override - String Function(int value)? checkPageTitle; + String Function(int? value)? checkPageTitle; @override - String Function(int value)? checkPageDescription; + String Function(int? value)? checkPageDescription; @override - void onSaved(int value) { + void Function(int? value)? onChanged; + + @override + void onSaved(int? value) { this.value = value; } @override String? onValidate( - int value, String Function(String, {List? params}) translator) { + int? value, String Function(String, {List? params}) translator) { if (mandatory) {} return null; diff --git a/lib/src/widgets/input/input_types/input_password/input_password.dart b/lib/src/widgets/input/input_types/input_password/input_password.dart index f99fc13..a966e1c 100644 --- a/lib/src/widgets/input/input_types/input_password/input_password.dart +++ b/lib/src/widgets/input/input_types/input_password/input_password.dart @@ -6,10 +6,10 @@ import '../../../../../flutter_form.dart'; /// Input for a password used in a [FlutterForm]. /// /// Standard controller is [FlutterFormInputEmailController]. -class FlutterFormInputPassword extends FlutterFormInputWidget { +class FlutterFormInputPassword extends FlutterFormInputWidget { const FlutterFormInputPassword({ Key? key, - required FlutterFormInputController controller, + required FlutterFormInputController controller, Widget? label, }) : super(key: key, controller: controller, label: label); @@ -35,6 +35,7 @@ class FlutterFormInputPasswordController this.value, this.checkPageTitle, this.checkPageDescription, + this.onChanged, }); @override @@ -47,10 +48,13 @@ class FlutterFormInputPasswordController bool mandatory; @override - String Function(String value)? checkPageTitle; + String Function(String? value)? checkPageTitle; @override - String Function(String value)? checkPageDescription; + String Function(String? value)? checkPageDescription; + + @override + void Function(String? value)? onChanged; @override void onSaved(dynamic value) { diff --git a/lib/src/widgets/input/input_types/input_password/password.dart b/lib/src/widgets/input/input_types/input_password/password.dart index 834a54d..5b93069 100644 --- a/lib/src/widgets/input/input_types/input_password/password.dart +++ b/lib/src/widgets/input/input_types/input_password/password.dart @@ -32,6 +32,7 @@ class _PasswordTextFieldState extends ConsumerState { obscureText: obscured, onSaved: (value) => widget.controller.onSaved(value), validator: (value) => widget.controller.onValidate(value, _), + onChanged: (value) => widget.controller.onChanged?.call(value), decoration: InputDecoration( label: widget.label ?? const Text("Password"), suffixIcon: IconButton( diff --git a/lib/src/widgets/input/input_types/input_plain_text.dart b/lib/src/widgets/input/input_types/input_plain_text.dart index f9d57bd..be0b887 100644 --- a/lib/src/widgets/input/input_types/input_plain_text.dart +++ b/lib/src/widgets/input/input_types/input_plain_text.dart @@ -7,10 +7,10 @@ import '../../../../flutter_form.dart'; /// Input for plain text input used in a [FlutterForm]. /// /// Standard controller is [FlutterFormInputPlainTextController]. -class FlutterFormInputPlainText extends FlutterFormInputWidget { +class FlutterFormInputPlainText extends FlutterFormInputWidget { const FlutterFormInputPlainText({ Key? key, - required FlutterFormInputController controller, + required FlutterFormInputController controller, Widget? label, this.decoration, this.textAlignVertical, @@ -41,6 +41,7 @@ class FlutterFormInputPlainText extends FlutterFormInputWidget { initialValue: controller.value, onSaved: (value) => controller.onSaved(value), validator: (value) => controller.onValidate(value, _), + onChanged: (value) => controller.onChanged?.call(value), decoration: inputDecoration, textAlignVertical: textAlignVertical, expands: expands, @@ -66,7 +67,7 @@ class FlutterFormInputMultiLine extends StatelessWidget { this.maxCharacters, }) : super(key: key); - final FlutterFormInputController controller; + final FlutterFormInputController controller; final Widget? label; final String? hint; @@ -114,6 +115,7 @@ class FlutterFormInputPlainTextController this.value, this.checkPageTitle, this.checkPageDescription, + this.onChanged, }); @override @@ -126,13 +128,16 @@ class FlutterFormInputPlainTextController bool mandatory; @override - String Function(String value)? checkPageTitle; + String Function(String? value)? checkPageTitle; @override - String Function(String value)? checkPageDescription; + String Function(String? value)? checkPageDescription; @override - void onSaved(String value) { + void Function(String? value)? onChanged; + + @override + void onSaved(String? value) { this.value = value; } diff --git a/lib/src/widgets/input/input_types/input_slider/input_slider.dart b/lib/src/widgets/input/input_types/input_slider/input_slider.dart index 49a476b..025b3c3 100644 --- a/lib/src/widgets/input/input_types/input_slider/input_slider.dart +++ b/lib/src/widgets/input/input_types/input_slider/input_slider.dart @@ -8,10 +8,10 @@ import '../../../../../flutter_form.dart'; /// Input for a number value between two values via a slider. Used in a [FlutterForm]. /// /// Standard controller is [FlutterFormInputSliderController]. -class FlutterFormInputSlider extends FlutterFormInputWidget { +class FlutterFormInputSlider extends FlutterFormInputWidget { const FlutterFormInputSlider({ Key? key, - required FlutterFormInputController controller, + required FlutterFormInputController controller, Widget? label, this.minValue = 0, this.maxValue = 100, @@ -46,6 +46,7 @@ class FlutterFormInputSliderController this.value, this.checkPageTitle, this.checkPageDescription, + this.onChanged, }); @override @@ -58,18 +59,21 @@ class FlutterFormInputSliderController bool mandatory; @override - String Function(double value)? checkPageTitle; + String Function(double? value)? checkPageTitle; @override - String Function(double value)? checkPageDescription; + String Function(double? value)? checkPageDescription; @override - void onSaved(double value) { + void Function(double? value)? onChanged; + + @override + void onSaved(double? value) { this.value = value; } @override - String? onValidate(double value, + String? onValidate(double? value, String Function(String, {List? params}) translator) { if (mandatory) {} diff --git a/lib/src/widgets/input/input_types/input_slider/slider.dart b/lib/src/widgets/input/input_types/input_slider/slider.dart index 1ac28dd..84e1c12 100644 --- a/lib/src/widgets/input/input_types/input_slider/slider.dart +++ b/lib/src/widgets/input/input_types/input_slider/slider.dart @@ -6,6 +6,7 @@ class SliderFormField extends FormField { Key? key, required FormFieldSetter onSaved, required FormFieldValidator validator, + void Function(double value)? onChanged, double initialValue = 0.5, }) : super( key: key, @@ -16,6 +17,8 @@ class SliderFormField extends FormField { return Slider( value: state.value ?? initialValue, onChanged: (double value) { + onChanged?.call(value); + state.didChange(value); }, ); diff --git a/pubspec.yaml b/pubspec.yaml index 6cf096c..d148513 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,9 +16,6 @@ dependencies: sdk: flutter flutter_riverpod: ^1.0.4 localization: ^2.1.0 - - sliding_up_panel: ^2.0.0+1 - uuid: ^3.0.6 dev_dependencies: flutter_test: From dd6fdcce454bf5e4e91d2cff7708674b61d4411c Mon Sep 17 00:00:00 2001 From: Jacques Doeleman Date: Wed, 12 Oct 2022 16:32:47 +0200 Subject: [PATCH 2/7] Removed problems --- example/lib/form_example.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/example/lib/form_example.dart b/example/lib/form_example.dart index 8c395ea..c3b0c88 100644 --- a/example/lib/form_example.dart +++ b/example/lib/form_example.dart @@ -23,7 +23,7 @@ class _FormExampleState extends ConsumerState { checkPageTitle: (dynamic amount) { return "Age: $amount years"; }, - onChanged: (value) => print(value), + onChanged: (value) => debugPrint(value.toString()), ); late final FlutterFormInputCarouselController carouselInputController; @@ -50,7 +50,7 @@ class _FormExampleState extends ConsumerState { checkPageTitle: (dynamic firstName) { return "First Name: $firstName"; }, - onChanged: (value) => print(value), + onChanged: (value) => debugPrint(value), ); FlutterFormInputPlainTextController lastNameController = @@ -60,7 +60,7 @@ class _FormExampleState extends ConsumerState { checkPageTitle: (dynamic lastName) { return "Last Name: $lastName"; }, - onChanged: (value) => print(value), + onChanged: (value) => debugPrint(value), ); @override @@ -74,7 +74,7 @@ class _FormExampleState extends ConsumerState { checkPageDescription: (dynamic index) { return cars[index]["description"]; }, - onChanged: (value) => print(value), + onChanged: (value) => debugPrint(value.toString()), ); } From b0402e0385dca148d74e85ef7177df6bf8540e8f Mon Sep 17 00:00:00 2001 From: Jacques Doeleman Date: Wed, 12 Oct 2022 16:35:33 +0200 Subject: [PATCH 3/7] Changed readme --- lib/src/widgets/input/abstractions.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/widgets/input/abstractions.dart b/lib/src/widgets/input/abstractions.dart index c439d20..7f2908f 100644 --- a/lib/src/widgets/input/abstractions.dart +++ b/lib/src/widgets/input/abstractions.dart @@ -56,7 +56,7 @@ abstract class FlutterFormInputWidget extends ConsumerWidget { /// [checkPageDescription] is the same as checkPageTitle but for the description. /// If null no description will be shown. /// -/// [onChanged] can be set to get the value whenever the user changes it. +/// [onChanged] can be set to get the value whenever the user changes it. Should not be used to save the value. /// /// [onSaved] goes of when the save function is called for the page if [onValidate] return null. /// @@ -86,7 +86,7 @@ abstract class FlutterFormInputController { /// If null no description will be shown. String Function(T? value)? checkPageDescription; - /// [onChanged] can be set to get the value whenever the user changes it. + /// [onChanged] can be set to get the value whenever the user changes it. Should not be used to save the value. void Function(T? value)? onChanged; /// [onSaved] goes of when the save function is called for the page if [onValidate] return null. From 2a70cd857675923b212c0427204695115d36fd18 Mon Sep 17 00:00:00 2001 From: Jacques Doeleman Date: Thu, 13 Oct 2022 13:25:37 +0200 Subject: [PATCH 4/7] Added the switch input field --- .../input_switch/input_switch.dart | 76 +++++++++++++++++++ .../input_switch/input_switch_field.dart | 53 +++++++++++++ .../input/input_types/input_types.dart | 1 + 3 files changed, 130 insertions(+) create mode 100644 lib/src/widgets/input/input_types/input_switch/input_switch.dart create mode 100644 lib/src/widgets/input/input_types/input_switch/input_switch_field.dart diff --git a/lib/src/widgets/input/input_types/input_switch/input_switch.dart b/lib/src/widgets/input/input_types/input_switch/input_switch.dart new file mode 100644 index 0000000..2197ed3 --- /dev/null +++ b/lib/src/widgets/input/input_types/input_switch/input_switch.dart @@ -0,0 +1,76 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_form/flutter_form.dart'; +import 'package:flutter_form/src/widgets/input/input_types/input_switch/input_switch_field.dart'; +import 'package:flutter_form/utils/translation_service.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +/// Input for a boolean switch. Used in a [FlutterForm]. +/// +/// Standard controller is [FlutterFormInputSwitchController]. +class FlutterFormInputSwitch extends FlutterFormInputWidget { + const FlutterFormInputSwitch({ + Key? key, + required FlutterFormInputController controller, + Widget? label, + }) : super(key: key, controller: controller, label: label); + + @override + Widget build(BuildContext context, WidgetRef ref) { + String Function(String, {List? params}) _ = + getTranslator(context, ref); + + super.registerController(context); + + return SwitchFormField( + onSaved: (value) { + controller.onSaved(value); + }, + validator: (value) => controller.onValidate(value, _), + initialValue: controller.value ?? false, + ); + } +} + +/// Controller for the switch used by a [FlutterFormInputWidget] used in a [FlutterForm]. +/// +/// Mainly used by [FlutterFormInputSwitch]. +class FlutterFormInputSwitchController + implements FlutterFormInputController { + FlutterFormInputSwitchController({ + required this.id, + this.mandatory = true, + this.value, + this.checkPageTitle, + this.checkPageDescription, + this.onChanged, + }); + + @override + String? id; + + @override + bool? value; + + @override + bool mandatory; + + @override + String Function(bool? value)? checkPageTitle; + + @override + String Function(bool? value)? checkPageDescription; + + @override + void Function(bool? value)? onChanged; + + @override + void onSaved(bool? value) { + this.value = value; + } + + @override + String? onValidate( + bool? value, String Function(String, {List? params}) translator) { + return null; + } +} diff --git a/lib/src/widgets/input/input_types/input_switch/input_switch_field.dart b/lib/src/widgets/input/input_types/input_switch/input_switch_field.dart new file mode 100644 index 0000000..b8b97d0 --- /dev/null +++ b/lib/src/widgets/input/input_types/input_switch/input_switch_field.dart @@ -0,0 +1,53 @@ +import 'package:flutter/material.dart'; + +class SwitchFormField extends FormField { + SwitchFormField({ + Key? key, + required FormFieldSetter onSaved, + required FormFieldValidator validator, + bool initialValue = false, + bool autovalidate = false, + }) : super( + key: key, + onSaved: onSaved, + validator: validator, + initialValue: initialValue, + builder: (FormFieldState state) { + return SwitchWidget( + initialValue: initialValue, + state: state, + ); + }); +} + +class SwitchWidget extends StatefulWidget { + const SwitchWidget({ + this.initialValue = false, + required this.state, + super.key, + }); + + final bool initialValue; + final FormFieldState state; + + @override + State createState() => _SwitchWidgetState(); +} + +class _SwitchWidgetState extends State { + late bool value = widget.initialValue; + + @override + Widget build(BuildContext context) { + return Switch( + value: value, + onChanged: (bool value) { + widget.state.didChange(value); + + setState(() { + this.value = value; + }); + }, + ); + } +} diff --git a/lib/src/widgets/input/input_types/input_types.dart b/lib/src/widgets/input/input_types/input_types.dart index 8920d92..2d95e4c 100644 --- a/lib/src/widgets/input/input_types/input_types.dart +++ b/lib/src/widgets/input/input_types/input_types.dart @@ -4,3 +4,4 @@ export 'input_number_picker/input_number_picker.dart'; export 'input_password/input_password.dart'; export 'input_plain_text.dart'; export 'input_slider/input_slider.dart'; +export 'input_switch/input_switch.dart'; From e690b8a063213ec7b9235592ab2efc3181e63dd2 Mon Sep 17 00:00:00 2001 From: Jacques Doeleman Date: Thu, 13 Oct 2022 13:38:42 +0200 Subject: [PATCH 5/7] Updated changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71e0586..0e8903a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,3 +11,8 @@ ## 0.2.0 - October 13th 2022 - Added date input widget + +## 1.0.0 + +- Fix: Proper use of generics +- Inputcontrollers now have an onChange. From d7eb86217fe019b4791d773efda7a56075f6b61f Mon Sep 17 00:00:00 2001 From: Jacques Doeleman Date: Thu, 13 Oct 2022 13:39:12 +0200 Subject: [PATCH 6/7] Update changeLog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e8903a..10d62b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,3 +16,4 @@ - Fix: Proper use of generics - Inputcontrollers now have an onChange. +- Added switch input field From d7176e547a5007cc8729c06e000940f35ce291a1 Mon Sep 17 00:00:00 2001 From: Jacques Doeleman Date: Thu, 13 Oct 2022 13:39:35 +0200 Subject: [PATCH 7/7] Update changeLog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10d62b8..59484ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ - Added date input widget -## 1.0.0 +## 1.0.0 - October 13th 2022 - Fix: Proper use of generics - Inputcontrollers now have an onChange.