forked from flutter/flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Material 3 Popup Menu example and update existing example (flutte…
- Loading branch information
1 parent
8fe8728
commit eadda3c
Showing
7 changed files
with
209 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PopupMenuExample> createState() => _PopupMenuExampleState(); | ||
} | ||
|
||
class _PopupMenuExampleState extends State<PopupMenuExample> { | ||
SampleItem? selectedMenu; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('PopupMenuButton')), | ||
body: Center( | ||
child: PopupMenuButton<SampleItem>( | ||
initialValue: selectedMenu, | ||
// Callback that sets the selected popup menu item. | ||
onSelected: (SampleItem item) { | ||
setState(() { | ||
selectedMenu = item; | ||
}); | ||
}, | ||
itemBuilder: (BuildContext context) => <PopupMenuEntry<SampleItem>>[ | ||
const PopupMenuItem<SampleItem>( | ||
value: SampleItem.itemOne, | ||
child: Text('Item 1'), | ||
), | ||
const PopupMenuItem<SampleItem>( | ||
value: SampleItem.itemTwo, | ||
child: Text('Item 2'), | ||
), | ||
const PopupMenuItem<SampleItem>( | ||
value: SampleItem.itemThree, | ||
child: Text('Item 3'), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PopupMenuExample> createState() => _PopupMenuExampleState(); | ||
} | ||
|
||
class _PopupMenuExampleState extends State<PopupMenuExample> { | ||
SampleItem? selectedMenu; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('PopupMenuButton')), | ||
body: Center( | ||
child: PopupMenuButton<SampleItem>( | ||
initialValue: selectedMenu, | ||
// Callback that sets the selected popup menu item. | ||
onSelected: (SampleItem item) { | ||
setState(() { | ||
selectedMenu = item; | ||
}); | ||
}, | ||
itemBuilder: (BuildContext context) => <PopupMenuEntry<SampleItem>>[ | ||
const PopupMenuItem<SampleItem>( | ||
value: SampleItem.itemOne, | ||
child: Text('Item 1'), | ||
), | ||
const PopupMenuItem<SampleItem>( | ||
value: SampleItem.itemTwo, | ||
child: Text('Item 2'), | ||
), | ||
const PopupMenuItem<SampleItem>( | ||
value: SampleItem.itemThree, | ||
child: Text('Item 3'), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
examples/api/test/material/popup_menu/popup_menu.0_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
} |
33 changes: 33 additions & 0 deletions
33
examples/api/test/material/popup_menu/popup_menu.1_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
} |
34 changes: 0 additions & 34 deletions
34
examples/api/test/material/popupmenu/popupmenu.0_test.dart
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters