mirror of
https://github.com/flutter/flutter
synced 2024-08-25 02:46:31 +00:00
Fix null operator error when tapping on 'MenuItemButton' (#142230)
This PR fixes null operator error when you change focus node of a 'MenuItemButton' to null. fixes: [issue142095](https://github.com/flutter/flutter/issues/142095)
This commit is contained in:
parent
d93e7ae971
commit
4a6cbefe34
|
@ -1071,7 +1071,7 @@ class _MenuItemButtonState extends State<MenuItemButton> {
|
|||
@override
|
||||
void didUpdateWidget(MenuItemButton oldWidget) {
|
||||
if (widget.focusNode != oldWidget.focusNode) {
|
||||
_focusNode.removeListener(_handleFocusChange);
|
||||
(oldWidget.focusNode ?? _internalFocusNode)?.removeListener(_handleFocusChange);
|
||||
if (widget.focusNode != null) {
|
||||
_internalFocusNode?.dispose();
|
||||
_internalFocusNode = null;
|
||||
|
|
|
@ -2599,6 +2599,57 @@ void main() {
|
|||
);
|
||||
});
|
||||
|
||||
testWidgets('tapping MenuItemButton with null focus node', (WidgetTester tester) async {
|
||||
|
||||
FocusNode? buttonFocusNode = FocusNode();
|
||||
|
||||
// Build our app and trigger a frame.
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: StatefulBuilder(
|
||||
builder: (BuildContext context, StateSetter setState) {
|
||||
return MenuAnchor(
|
||||
menuChildren: <Widget>[
|
||||
MenuItemButton(
|
||||
focusNode: buttonFocusNode,
|
||||
closeOnActivate: false,
|
||||
child: const Text('Set focus to null'),
|
||||
onPressed: () {
|
||||
setState((){
|
||||
buttonFocusNode = null;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
builder: (BuildContext context, MenuController controller, Widget? child) {
|
||||
return TextButton(
|
||||
onPressed: () {
|
||||
if (controller.isOpen) {
|
||||
controller.close();
|
||||
} else {
|
||||
controller.open();
|
||||
}
|
||||
},
|
||||
child: const Text('OPEN MENU'),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.text('OPEN MENU'));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Set focus to null'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('Set focus to null'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
|
||||
testWidgets('constrained menus show up in the right place in RTL', (WidgetTester tester) async {
|
||||
await changeSurfaceSize(tester, const Size(300, 300));
|
||||
await tester.pumpWidget(
|
||||
|
|
Loading…
Reference in a new issue