Making DropdownButtonFormField to re-render if parent widget changes (#57037)

* Update DropdownButtonFormField's state if widget updates

Co-authored-by: Shi-Hao Hong <shihaohong@google.com>
This commit is contained in:
Pedro Massango 2020-05-13 17:16:02 +01:00 committed by GitHub
parent 9e9524977e
commit 1aaf73fad0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View file

@ -1561,4 +1561,12 @@ class _DropdownButtonFormFieldState<T> extends FormFieldState<T> {
assert(widget.onChanged != null);
widget.onChanged(value);
}
@override
void didUpdateWidget(DropdownButtonFormField<T> oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.initialValue != widget.initialValue) {
setValue(widget.initialValue);
}
}
}

View file

@ -715,4 +715,50 @@ void main() {
expect(value, equals('two'));
expect(dropdownButtonTapCounter, 2); // Should not change.
});
testWidgets('DropdownButtonFormField should re-render if value param changes', (WidgetTester tester) async {
String currentValue = 'two';
await tester.pumpWidget(
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return MaterialApp(
home: Material(
child: DropdownButtonFormField<String>(
value: currentValue,
onChanged: onChanged,
items: menuItems.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
onTap: () {
setState(() {
currentValue = value;
});
},
);
}).toList(),
),
),
);
},
),
);
// Make sure the rendered text value matches the initial state value.
expect(currentValue, equals('two'));
expect(find.text(currentValue), findsOneWidget);
// Tap the DropdownButtonFormField widget
await tester.tap(find.byType(dropdownButtonType));
await tester.pumpAndSettle();
// Tap the first dropdown menu item.
await tester.tap(find.text('one').last);
await tester.pumpAndSettle();
// Make sure the rendered text value matches the updated state value.
expect(currentValue, equals('one'));
expect(find.text(currentValue), findsOneWidget);
});
}