From e5f9d5bd26982922dca64acc9066e32fe806e12a Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Sat, 29 Jan 2022 00:10:14 +0200 Subject: [PATCH] Update `PopupMenuButton` example (#96681) --- .../lib/material/popupmenu/popupmenu.0.dart | 76 +++++++++++++++++++ .../material/popupmenu/popupmenu.0.test.dart | 29 +++++++ .../flutter/lib/src/material/popup_menu.dart | 40 ++-------- 3 files changed, 112 insertions(+), 33 deletions(-) create mode 100644 examples/api/lib/material/popupmenu/popupmenu.0.dart create mode 100644 examples/api/test/material/popupmenu/popupmenu.0.test.dart diff --git a/examples/api/lib/material/popupmenu/popupmenu.0.dart b/examples/api/lib/material/popupmenu/popupmenu.0.dart new file mode 100644 index 0000000000..8e9709f3db --- /dev/null +++ b/examples/api/lib/material/popupmenu/popupmenu.0.dart @@ -0,0 +1,76 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Flutter code sample for PopupMenuButton + +import 'package:flutter/material.dart'; + +// This is the type used by the popup menu below. +enum Menu { itemOne, itemTwo, itemThree, itemFour } + +void main() => runApp(const MyApp()); + +class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + + static const String _title = 'Flutter Code Sample'; + + @override + Widget build(BuildContext context) { + return const MaterialApp( + title: _title, + home: MyStatefulWidget(), + ); + } +} + +class MyStatefulWidget extends StatefulWidget { + const MyStatefulWidget({Key? key}) : super(key: key); + + @override + State createState() => _MyStatefulWidgetState(); +} + +class _MyStatefulWidgetState extends State { + String _selectedMenu = ''; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + actions: [ + // This button presents popup menu items. + PopupMenuButton( + // Callback that sets the selected popup menu item. + onSelected: (Menu item) { + setState(() { + _selectedMenu = item.name; + }); + }, + itemBuilder: (BuildContext context) => >[ + const PopupMenuItem( + value: Menu.itemOne, + child: Text('Item 1'), + ), + const PopupMenuItem( + value: Menu.itemTwo, + child: Text('Item 2'), + ), + const PopupMenuItem( + value: Menu.itemThree, + child: Text('Item 3'), + ), + const PopupMenuItem( + value: Menu.itemFour, + child: Text('Item 4'), + ), + ]) + ], + ), + body: Center( + child: Text('_selectedMenu: $_selectedMenu'), + ), + ); + } +} diff --git a/examples/api/test/material/popupmenu/popupmenu.0.test.dart b/examples/api/test/material/popupmenu/popupmenu.0.test.dart new file mode 100644 index 0000000000..a075fdcac4 --- /dev/null +++ b/examples/api/test/material/popupmenu/popupmenu.0.test.dart @@ -0,0 +1,29 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_api_samples/material/popupmenu/popupmenu.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Can select a menu item', (WidgetTester tester) async { + final Key popupButtonKey = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: example.MyStatefulWidget( + key: popupButtonKey, + ), + ), + ), + ); + + await tester.tap(find.byKey(popupButtonKey)); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); // finish the menu animation + await tester.tapAt(const Offset(1.0, 1.0)); + await tester.pumpAndSettle(); + expect(find.text('_selectedMenu: itemOne'), findsNothing); + }); +} diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 3ca45c8aeb..8d446a2a89 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -26,6 +26,7 @@ import 'tooltip.dart'; // late dynamic _selection; // late BuildContext context; // void setState(VoidCallback fn) { } +// enum Menu { itemOne, itemTwo, itemThree, itemFour } const Duration _kMenuDuration = Duration(milliseconds: 300); const double _kMenuCloseIntervalEnd = 2.0 / 3.0; @@ -186,13 +187,13 @@ class _RenderMenuItem extends RenderShiftedBox { /// /// {@tool snippet} /// -/// Here, a [Text] widget is used with a popup menu item. The `WhyFarther` type +/// Here, a [Text] widget is used with a popup menu item. The `Menu` type /// is an enum, not shown here. /// /// ```dart -/// const PopupMenuItem( -/// value: WhyFarther.harder, -/// child: Text('Working a lot harder'), +/// const PopupMenuItem( +/// value: Menu.itemOne, +/// child: Text('Item 1'), /// ) /// ``` /// {@end-tool} @@ -933,36 +934,9 @@ typedef PopupMenuItemBuilder = List> Function(BuildContext /// {@tool snippet} /// /// This example shows a menu with four items, selecting between an enum's -/// values and setting a `_selection` field based on the selection. +/// values and setting a `_selectedMenu` field based on the selection /// -/// ```dart -/// // This is the type used by the popup menu below. -/// enum WhyFarther { harder, smarter, selfStarter, tradingCharter } -/// -/// // This menu button widget updates a _selection field (of type WhyFarther, -/// // not shown here). -/// PopupMenuButton( -/// onSelected: (WhyFarther result) { setState(() { _selection = result; }); }, -/// itemBuilder: (BuildContext context) => >[ -/// const PopupMenuItem( -/// value: WhyFarther.harder, -/// child: Text('Working a lot harder'), -/// ), -/// const PopupMenuItem( -/// value: WhyFarther.smarter, -/// child: Text('Being a lot smarter'), -/// ), -/// const PopupMenuItem( -/// value: WhyFarther.selfStarter, -/// child: Text('Being a self-starter'), -/// ), -/// const PopupMenuItem( -/// value: WhyFarther.tradingCharter, -/// child: Text('Placed in charge of trading charter'), -/// ), -/// ], -/// ) -/// ``` +/// ** See code in examples/api/lib/material/popupmenu/popupmenu.0.dart ** /// {@end-tool} /// /// See also: