From cfc051531a98018061bc4a612b84b388e514622a Mon Sep 17 00:00:00 2001 From: Jacques Date: Thu, 26 Oct 2023 14:08:21 +0200 Subject: [PATCH] Added the ability to set the enabled of textfields --- .../inputs/date_picker/date_picker_field.dart | 59 +++++++++++-------- lib/src/inputs/text/password.dart | 3 + lib/src/inputs/text/plain_text.dart | 6 ++ 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/lib/src/inputs/date_picker/date_picker_field.dart b/lib/src/inputs/date_picker/date_picker_field.dart index ce604c9..a98c004 100644 --- a/lib/src/inputs/date_picker/date_picker_field.dart +++ b/lib/src/inputs/date_picker/date_picker_field.dart @@ -29,6 +29,7 @@ class DateTimeInputField extends StatefulWidget { this.validator, required this.timePickerEntryMode, required this.style, + this.enabled = true, }) : super( key: key, ); @@ -50,6 +51,7 @@ class DateTimeInputField extends StatefulWidget { final void Function(String?)? onSaved; final void Function(String?)? onChanged; final TimePickerEntryMode timePickerEntryMode; + final bool enabled; @override State createState() => _DateInputFieldState(); @@ -128,34 +130,38 @@ class _DateInputFieldState extends State { }); break; case FlutterFormDateTimeType.range: - userInput = (await showDateRangePicker( - context: context, - firstDate: firstDate, - lastDate: lastDate, - initialDateRange: initialDateRange) - .then((value) { - return value != null - ? '${widget.dateFormat.format(value.start)} - ${widget.dateFormat.format(value.end)}' - : ''; - })) - .toString(); + if (context.mounted) { + userInput = (await showDateRangePicker( + context: context, + firstDate: firstDate, + lastDate: lastDate, + initialDateRange: initialDateRange) + .then((value) { + return value != null + ? '${widget.dateFormat.format(value.start)} - ${widget.dateFormat.format(value.end)}' + : ''; + })) + .toString(); + } break; case FlutterFormDateTimeType.time: - userInput = await showTimePicker( - initialEntryMode: widget.timePickerEntryMode, - builder: (BuildContext context, Widget? child) { - return MediaQuery( - data: MediaQuery.of(context) - .copyWith(alwaysUse24HourFormat: true), - child: child!, - ); - }, - context: context, - initialTime: initialTimeOfDay, - ).then((value) => value == null - ? '' - : MaterialLocalizations.of(context) - .formatTimeOfDay(value, alwaysUse24HourFormat: true)); + if (context.mounted) { + userInput = await showTimePicker( + initialEntryMode: widget.timePickerEntryMode, + builder: (BuildContext context, Widget? child) { + return MediaQuery( + data: MediaQuery.of(context) + .copyWith(alwaysUse24HourFormat: true), + child: child!, + ); + }, + context: context, + initialTime: initialTimeOfDay, + ).then((value) => value == null + ? '' + : MaterialLocalizations.of(context) + .formatTimeOfDay(value, alwaysUse24HourFormat: true)); + } } return userInput; } @@ -185,6 +191,7 @@ class _DateInputFieldState extends State { focusColor: Theme.of(context).primaryColor, label: widget.label ?? const Text("Date"), ), + enabled: widget.enabled, ); } } diff --git a/lib/src/inputs/text/password.dart b/lib/src/inputs/text/password.dart index 005dd86..681053a 100644 --- a/lib/src/inputs/text/password.dart +++ b/lib/src/inputs/text/password.dart @@ -14,6 +14,7 @@ class FlutterFormInputPassword extends StatefulWidget { final String? Function(String?)? validator; final Function(String?)? onChanged; final Function(String?)? onFieldSubmitted; + final bool enabled; const FlutterFormInputPassword({ Key? key, @@ -24,6 +25,7 @@ class FlutterFormInputPassword extends StatefulWidget { this.validator, this.onChanged, this.onFieldSubmitted, + this.enabled = true, }) : super(key: key); @override @@ -54,6 +56,7 @@ class _PasswordTextFieldState extends State { icon: Icon(obscured ? Icons.visibility_off : Icons.visibility), ), ), + enabled: widget.enabled, ); } } diff --git a/lib/src/inputs/text/plain_text.dart b/lib/src/inputs/text/plain_text.dart index 3fc638c..dcf500b 100644 --- a/lib/src/inputs/text/plain_text.dart +++ b/lib/src/inputs/text/plain_text.dart @@ -22,6 +22,7 @@ class FlutterFormInputPlainText extends StatelessWidget { this.validator, this.onFieldSubmitted, this.style, + this.enabled = true, }) : super( key: key, ); @@ -41,6 +42,7 @@ class FlutterFormInputPlainText extends StatelessWidget { final Function(String?)? onChanged; final Function(String?)? onFieldSubmitted; final TextStyle? style; + final bool enabled; @override Widget build(BuildContext context) { @@ -64,6 +66,7 @@ class FlutterFormInputPlainText extends StatelessWidget { maxLines: maxLines, maxLength: maxLength, keyboardType: keyboardType, + enabled: enabled, ); } } @@ -75,6 +78,7 @@ class FlutterFormInputMultiLine extends StatelessWidget { this.focusNode, this.hint, this.maxCharacters, + this.enabled = true, this.scrollPadding, this.keyboardType, this.initialValue, @@ -89,6 +93,7 @@ class FlutterFormInputMultiLine extends StatelessWidget { final FocusNode? focusNode; final String? hint; final int? maxCharacters; + final bool enabled; final InputDecoration? decoration; final EdgeInsets? scrollPadding; @@ -131,6 +136,7 @@ class FlutterFormInputMultiLine extends StatelessWidget { ), filled: true, ), + enabled: enabled, ), ), ],