diff --git a/examples/api/lib/material/popup_menu/popup_menu.0.dart b/examples/api/lib/material/popup_menu/popup_menu.0.dart new file mode 100644 index 00000000000..f2e49370ffb --- /dev/null +++ b/examples/api/lib/material/popup_menu/popup_menu.0.dart @@ -0,0 +1,66 @@ +// 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 SampleItem { itemOne, itemTwo, itemThree } + +void main() => runApp(const PopupMenuApp()); + +class PopupMenuApp extends StatelessWidget { + const PopupMenuApp({super.key}); + + @override + Widget build(BuildContext context) { + return const MaterialApp( + home: PopupMenuExample(), + ); + } +} + +class PopupMenuExample extends StatefulWidget { + const PopupMenuExample({super.key}); + + @override + State createState() => _PopupMenuExampleState(); +} + +class _PopupMenuExampleState extends State { + SampleItem? selectedMenu; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(title: const Text('PopupMenuButton')), + body: Center( + child: PopupMenuButton( + initialValue: selectedMenu, + // Callback that sets the selected popup menu item. + onSelected: (SampleItem item) { + setState(() { + selectedMenu = item; + }); + }, + itemBuilder: (BuildContext context) => >[ + const PopupMenuItem( + value: SampleItem.itemOne, + child: Text('Item 1'), + ), + const PopupMenuItem( + value: SampleItem.itemTwo, + child: Text('Item 2'), + ), + const PopupMenuItem( + value: SampleItem.itemThree, + child: Text('Item 3'), + ), + ], + ), + ), + ); + } +} diff --git a/examples/api/lib/material/popup_menu/popup_menu.1.dart b/examples/api/lib/material/popup_menu/popup_menu.1.dart new file mode 100644 index 00000000000..80cb83a7788 --- /dev/null +++ b/examples/api/lib/material/popup_menu/popup_menu.1.dart @@ -0,0 +1,67 @@ +// 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 SampleItem { itemOne, itemTwo, itemThree } + +void main() => runApp(const PopupMenuApp()); + +class PopupMenuApp extends StatelessWidget { + const PopupMenuApp({super.key}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + theme: ThemeData(useMaterial3: true, colorSchemeSeed: const Color(0xff6750a4)), + home: const PopupMenuExample(), + ); + } +} + +class PopupMenuExample extends StatefulWidget { + const PopupMenuExample({super.key}); + + @override + State createState() => _PopupMenuExampleState(); +} + +class _PopupMenuExampleState extends State { + SampleItem? selectedMenu; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(title: const Text('PopupMenuButton')), + body: Center( + child: PopupMenuButton( + initialValue: selectedMenu, + // Callback that sets the selected popup menu item. + onSelected: (SampleItem item) { + setState(() { + selectedMenu = item; + }); + }, + itemBuilder: (BuildContext context) => >[ + const PopupMenuItem( + value: SampleItem.itemOne, + child: Text('Item 1'), + ), + const PopupMenuItem( + value: SampleItem.itemTwo, + child: Text('Item 2'), + ), + const PopupMenuItem( + value: SampleItem.itemThree, + child: Text('Item 3'), + ), + ], + ), + ), + ); + } +} diff --git a/examples/api/lib/material/popupmenu/popupmenu.0.dart b/examples/api/lib/material/popupmenu/popupmenu.0.dart deleted file mode 100644 index 82e6a105187..00000000000 --- a/examples/api/lib/material/popupmenu/popupmenu.0.dart +++ /dev/null @@ -1,77 +0,0 @@ -// 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({super.key}); - - static const String _title = 'Flutter Code Sample'; - - @override - Widget build(BuildContext context) { - return const MaterialApp( - debugShowCheckedModeBanner: false, - title: _title, - home: MyStatefulWidget(), - ); - } -} - -class MyStatefulWidget extends StatefulWidget { - const MyStatefulWidget({super.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/popup_menu/popup_menu.0_test.dart b/examples/api/test/material/popup_menu/popup_menu.0_test.dart new file mode 100644 index 00000000000..ea43a197504 --- /dev/null +++ b/examples/api/test/material/popup_menu/popup_menu.0_test.dart @@ -0,0 +1,33 @@ +// 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/popup_menu/popup_menu.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Can open popup menu', (WidgetTester tester) async { + const String menuItem = 'Item 1'; + + await tester.pumpWidget( + const MaterialApp( + home: Scaffold( + body: example.PopupMenuApp(), + ), + ), + ); + + expect(find.text(menuItem), findsNothing); + + // Open popup menu. + await tester.tap(find.byIcon(Icons.adaptive.more)); + await tester.pumpAndSettle(); + expect(find.text(menuItem), findsOneWidget); + + // Close popup menu. + await tester.tapAt(const Offset(1, 1)); + await tester.pumpAndSettle(); + expect(find.text(menuItem), findsNothing); + }); +} diff --git a/examples/api/test/material/popup_menu/popup_menu.1_test.dart b/examples/api/test/material/popup_menu/popup_menu.1_test.dart new file mode 100644 index 00000000000..f6899344952 --- /dev/null +++ b/examples/api/test/material/popup_menu/popup_menu.1_test.dart @@ -0,0 +1,33 @@ +// 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/popup_menu/popup_menu.1.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Can open popup menu', (WidgetTester tester) async { + const String menuItem = 'Item 1'; + + await tester.pumpWidget( + const MaterialApp( + home: Scaffold( + body: example.PopupMenuApp(), + ), + ), + ); + + expect(find.text(menuItem), findsNothing); + + // Open popup menu. + await tester.tap(find.byIcon(Icons.adaptive.more)); + await tester.pumpAndSettle(); + expect(find.text(menuItem), findsOneWidget); + + // Close popup menu. + await tester.tapAt(const Offset(1, 1)); + await tester.pumpAndSettle(); + expect(find.text(menuItem), findsNothing); + }); +} diff --git a/examples/api/test/material/popupmenu/popupmenu.0_test.dart b/examples/api/test/material/popupmenu/popupmenu.0_test.dart deleted file mode 100644 index c8a813b44ef..00000000000 --- a/examples/api/test/material/popupmenu/popupmenu.0_test.dart +++ /dev/null @@ -1,34 +0,0 @@ -// 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); - }); - - testWidgets('Does not show debug banner', (WidgetTester tester) async { - await tester.pumpWidget(const example.MyApp()); - expect(find.byType(CheckedModeBanner), findsNothing); - }); -} diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 3ff1d57bf4d..30c5e6b5208 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -1017,10 +1017,17 @@ typedef PopupMenuItemBuilder = List> Function(BuildContext /// platform). /// /// {@tool dartpad} -/// This example shows a menu with four items, selecting between an enum's -/// values and setting a `_selectedMenu` field based on the selection +/// This example shows a menu with three items, selecting between an enum's +/// values and setting a `selectedMenu` field based on the selection. /// -/// ** See code in examples/api/lib/material/popupmenu/popupmenu.0.dart ** +/// ** See code in examples/api/lib/material/popup_menu/popup_menu.0.dart ** +/// {@end-tool} +/// +/// {@tool dartpad} +/// This sample shows the creation of a popup menu, as described in: +/// https://m3.material.io/components/menus/overview +/// +/// ** See code in examples/api/lib/material/popup_menu/popup_menu.1.dart ** /// {@end-tool} /// /// See also: