mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-28 17:08:29 +01:00
Fix set-state-error on TimeInputWidget
The widget is read-only, so we don't need to use a controller, it is simply rebuilt when the user selects a new time
This commit is contained in:
@@ -58,7 +58,6 @@ class TimeInputWidget extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _TimeInputWidgetState extends State<TimeInputWidget> {
|
||||
final _controller = TextEditingController();
|
||||
TimeOfDay? _value;
|
||||
|
||||
@override
|
||||
@@ -75,20 +74,14 @@ class _TimeInputWidgetState extends State<TimeInputWidget> {
|
||||
}
|
||||
}
|
||||
|
||||
@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<DateInputWidget> {
|
||||
final _controller = TextEditingController();
|
||||
DateTime? _value;
|
||||
|
||||
@override
|
||||
@@ -187,21 +179,15 @@ class _DateInputWidgetState extends State<DateInputWidget> {
|
||||
}
|
||||
}
|
||||
|
||||
@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,
|
||||
|
||||
@@ -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<TimeOfDay?>(null);
|
||||
await tester.pumpWidget(
|
||||
wrap(
|
||||
Form(
|
||||
child: ValueListenableBuilder<TimeOfDay?>(
|
||||
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<DateTime?>(null);
|
||||
await tester.pumpWidget(
|
||||
wrap(
|
||||
Form(
|
||||
child: ValueListenableBuilder<DateTime?>(
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user