Expose showButtonMenu of PopupMenuButtonState (#50670)
This commit is contained in:
parent
63ca33483f
commit
08c835913e
@ -1037,10 +1037,22 @@ class PopupMenuButton<T> extends StatefulWidget {
|
|||||||
final bool captureInheritedThemes;
|
final bool captureInheritedThemes;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_PopupMenuButtonState<T> createState() => _PopupMenuButtonState<T>();
|
PopupMenuButtonState<T> createState() => PopupMenuButtonState<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _PopupMenuButtonState<T> extends State<PopupMenuButton<T>> {
|
/// The [State] for a [PopupMenuButton].
|
||||||
|
///
|
||||||
|
/// See [showButtonMenu] for a way to programmatically open the popup menu
|
||||||
|
/// of your button state.
|
||||||
|
class PopupMenuButtonState<T> extends State<PopupMenuButton<T>> {
|
||||||
|
/// A method to show a popup menu with the items supplied to
|
||||||
|
/// [PopupMenuButton.itemBuilder] at the position of your [PopupMenuButton].
|
||||||
|
///
|
||||||
|
/// By default, it is called when the user taps the button and [PopupMenuButton.enabled]
|
||||||
|
/// is set to `true`. Moreover, you can open the button by calling the method manually.
|
||||||
|
///
|
||||||
|
/// You would access your [PopupMenuButtonState] using a [GlobalKey] and
|
||||||
|
/// show the menu of the button with `globalKey.currentState.showButtonMenu`.
|
||||||
void showButtonMenu() {
|
void showButtonMenu() {
|
||||||
final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context);
|
final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context);
|
||||||
final RenderBox button = context.findRenderObject() as RenderBox;
|
final RenderBox button = context.findRenderObject() as RenderBox;
|
||||||
|
@ -1197,6 +1197,41 @@ void main() {
|
|||||||
expect(rootObserver.menuCount, 1);
|
expect(rootObserver.menuCount, 1);
|
||||||
expect(nestedObserver.menuCount, 0);
|
expect(nestedObserver.menuCount, 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('PopupMenuButton calling showButtonMenu manually', (WidgetTester tester) async {
|
||||||
|
final GlobalKey<PopupMenuButtonState<int>> globalKey = GlobalKey();
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
home: Material(
|
||||||
|
child: Column(
|
||||||
|
children: <Widget>[
|
||||||
|
PopupMenuButton<int>(
|
||||||
|
key: globalKey,
|
||||||
|
itemBuilder: (BuildContext context) {
|
||||||
|
return <PopupMenuEntry<int>>[
|
||||||
|
const PopupMenuItem<int>(
|
||||||
|
value: 1,
|
||||||
|
child: Text('Tap me please!'),
|
||||||
|
),
|
||||||
|
];
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(find.text('Tap me please!'), findsNothing);
|
||||||
|
|
||||||
|
globalKey.currentState.showButtonMenu();
|
||||||
|
// The PopupMenuItem will appear after an animation, hence,
|
||||||
|
// we have to first wait for the tester to settle.
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('Tap me please!'), findsOneWidget);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestApp extends StatefulWidget {
|
class TestApp extends StatefulWidget {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user