Update PopupMenuButton example (#96681)

This commit is contained in:
Taha Tesser
2022-01-29 00:10:14 +02:00
committed by GitHub
parent ebfdeaec6e
commit e5f9d5bd26
3 changed files with 112 additions and 33 deletions

View File

@@ -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<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
String _selectedMenu = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
// This button presents popup menu items.
PopupMenuButton<Menu>(
// Callback that sets the selected popup menu item.
onSelected: (Menu item) {
setState(() {
_selectedMenu = item.name;
});
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<Menu>>[
const PopupMenuItem<Menu>(
value: Menu.itemOne,
child: Text('Item 1'),
),
const PopupMenuItem<Menu>(
value: Menu.itemTwo,
child: Text('Item 2'),
),
const PopupMenuItem<Menu>(
value: Menu.itemThree,
child: Text('Item 3'),
),
const PopupMenuItem<Menu>(
value: Menu.itemFour,
child: Text('Item 4'),
),
])
],
),
body: Center(
child: Text('_selectedMenu: $_selectedMenu'),
),
);
}
}

View File

@@ -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);
});
}