mirror of
https://github.com/flutter/flutter
synced 2024-10-14 04:02:56 +00:00
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:
parent
9e9524977e
commit
1aaf73fad0
|
@ -1561,4 +1561,12 @@ class _DropdownButtonFormFieldState<T> extends FormFieldState<T> {
|
||||||
assert(widget.onChanged != null);
|
assert(widget.onChanged != null);
|
||||||
widget.onChanged(value);
|
widget.onChanged(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didUpdateWidget(DropdownButtonFormField<T> oldWidget) {
|
||||||
|
super.didUpdateWidget(oldWidget);
|
||||||
|
if (oldWidget.initialValue != widget.initialValue) {
|
||||||
|
setValue(widget.initialValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -715,4 +715,50 @@ void main() {
|
||||||
expect(value, equals('two'));
|
expect(value, equals('two'));
|
||||||
expect(dropdownButtonTapCounter, 2); // Should not change.
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue