diff --git a/lib/widgets/core/datetime_input.dart b/lib/widgets/core/datetime_input.dart index afcee295a..ab5ae65f2 100644 --- a/lib/widgets/core/datetime_input.dart +++ b/lib/widgets/core/datetime_input.dart @@ -58,7 +58,6 @@ class TimeInputWidget extends StatefulWidget { } class _TimeInputWidgetState extends State { - final _controller = TextEditingController(); TimeOfDay? _value; @override @@ -75,20 +74,14 @@ class _TimeInputWidgetState extends State { } } - @override - void dispose() { - _controller.dispose(); - super.dispose(); - } - @override Widget build(BuildContext context) { - // The field is read-only, so rewriting the display text every build is safe. - _controller.text = _value?.format(context) ?? ''; - + // Keyed initialValue, not a controller: a controller notifies the enclosing + // Form on assignment, which crashes if that happens during a build. return TextFormField( + key: ValueKey(_value), readOnly: true, - controller: _controller, + initialValue: _value?.format(context) ?? '', validator: widget.validator, decoration: InputDecoration( labelText: widget.labelText, @@ -170,7 +163,6 @@ class DateInputWidget extends StatefulWidget { } class _DateInputWidgetState extends State { - final _controller = TextEditingController(); DateTime? _value; @override @@ -187,21 +179,15 @@ class _DateInputWidgetState extends State { } } - @override - void dispose() { - _controller.dispose(); - super.dispose(); - } - @override Widget build(BuildContext context) { final dateFormat = DateFormat.yMd(Localizations.localeOf(context).languageCode); - // The field is read-only, so rewriting the display text every build is safe. - _controller.text = _value != null ? dateFormat.format(_value!) : ''; - + // Keyed initialValue, not a controller: a controller notifies the enclosing + // Form on assignment, which crashes if that happens during a build. return TextFormField( + key: ValueKey(_value), readOnly: true, - controller: _controller, + initialValue: _value != null ? dateFormat.format(_value!) : '', validator: widget.validator, decoration: InputDecoration( labelText: widget.labelText, diff --git a/test/core/datetime_input_test.dart b/test/core/datetime_input_test.dart index 697c13c08..a07ccabd2 100644 --- a/test/core/datetime_input_test.dart +++ b/test/core/datetime_input_test.dart @@ -95,6 +95,29 @@ void main() { expect(cleared, isTrue); expect(find.text('3:30 PM'), findsNothing); }); + + testWidgets('does not rebuild the enclosing Form during build when the value ' + 'changes from the parent (regression: issue #2401)', (WidgetTester tester) async { + final notifier = ValueNotifier(null); + await tester.pumpWidget( + wrap( + Form( + child: ValueListenableBuilder( + valueListenable: notifier, + builder: (context, value, _) => + TimeInputWidget(value: value, labelText: 'Time', onChanged: (_) {}), + ), + ), + ), + ); + await tester.pumpAndSettle(); + + notifier.value = const TimeOfDay(hour: 15, minute: 30); + await tester.pumpAndSettle(); + + expect(tester.takeException(), isNull); + expect(find.text('3:30 PM'), findsOneWidget); + }); }); group('DateInputWidget', () { @@ -130,5 +153,28 @@ void main() { expect(find.text('5.1.2021'), findsOneWidget); }); + + testWidgets('does not rebuild the enclosing Form during build when the value ' + 'changes from the parent (regression: issue #2401)', (WidgetTester tester) async { + final notifier = ValueNotifier(null); + await tester.pumpWidget( + wrap( + Form( + child: ValueListenableBuilder( + valueListenable: notifier, + builder: (context, value, _) => + DateInputWidget(value: value, labelText: 'Date', onChanged: (_) {}), + ), + ), + ), + ); + await tester.pumpAndSettle(); + + notifier.value = DateTime(2021, 1, 5); + await tester.pumpAndSettle(); + + expect(tester.takeException(), isNull); + expect(find.text('1/5/2021'), findsOneWidget); + }); }); }