Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add Material 3 PopupMenuButton example and update CheckedPopupMenuItem example #103627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions examples/api/lib/material/popup_menu/checked_popup_menu.0.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// 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 CheckedPopupMenuItem

import 'package:flutter/material.dart';

// This is the menu item type used by the popup menu below.
enum Menu { rounded, bordered, large, all }

void main() => runApp(const CheckedMenuItemApp());

class CheckedMenuItemApp extends StatelessWidget {
const CheckedMenuItemApp({super.key});

@override
Widget build(BuildContext context) {
return const MaterialApp(
home: CheckedMenuItemExample(),
);
}
}

class CheckedMenuItemExample extends StatefulWidget {
const CheckedMenuItemExample({super.key});

@override
State<CheckedMenuItemExample> createState() => _CheckedMenuItemExampleState();
}

class _CheckedMenuItemExampleState extends State<CheckedMenuItemExample> {
bool rounded = false;
bool bordered = false;
bool large = false;
bool all = false;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('CheckedPopupMenuItem Sample') ),
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
height: large || all ? 150.0 : 75.0,
width: large || all ? 150.0 : 75.0,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: rounded || all ? BorderRadius.circular(24.0) : BorderRadius.zero,
border: bordered || all ? Border.all(width: 6.0) : null,
),
),
// This button presents the popup menu items.
PopupMenuButton<Menu>(
icon: const Icon(Icons.more_vert),
onSelected: (Menu item) {
// Callback that sets the selected popup menu item.
setState(() {
switch (item.name) {
case 'rounded':
rounded = !rounded;
all = false;
break;
case 'bordered':
bordered = !bordered;
all = false;
break;
case 'large':
large = !large;
all = false;
break;
case 'all':
if (rounded && bordered && large){
rounded = false;
bordered = false;
large = false;
} else {
rounded = true;
bordered = true;
large = true;
}
break;
}
});
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<Menu>>[
CheckedPopupMenuItem<Menu>(
value: Menu.rounded,
checked: rounded,
child: const Text('Rounded'),
),
CheckedPopupMenuItem<Menu>(
value: Menu.bordered,
checked: bordered,
child: const Text('Bordered'),
),
CheckedPopupMenuItem<Menu>(
value: Menu.large,
checked: large,
child: const Text('Large'),
),
const PopupMenuDivider(),
CheckedPopupMenuItem<Menu>(
value: Menu.all,
checked: rounded && bordered && large,
child: const Text('All of the above'),
),
],
)
],
),
),
),
);
}
}
85 changes: 85 additions & 0 deletions examples/api/lib/material/popup_menu/popup_menu_button.0.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 menu item type used by the popup menu below.
enum Menu { itemOne, itemTwo, itemThree, itemFour }

void main() => runApp(const PopupMenuButtonApp());

class PopupMenuButtonApp extends StatelessWidget {
const PopupMenuButtonApp({super.key});

@override
Widget build(BuildContext context) {
return const MaterialApp(
home: PopupMenuButtonExample(),
);
}
}

class PopupMenuButtonExample extends StatefulWidget {
const PopupMenuButtonExample({super.key});

@override
State<PopupMenuButtonExample> createState() => _PopupMenuButtonExampleState();
}

class _PopupMenuButtonExampleState extends State<PopupMenuButtonExample> {
String? selectedMenu;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('PopupMenuButton Sample')),
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ListTile(
iconColor: Colors.blue[700],
tileColor: Colors.blue[100],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4.0)
),
title: Text(
selectedMenu == null ? 'Select an item' : 'Selected item: $selectedMenu',
style: TextStyle(color: Colors.blue[700]),
),
// This button presents popup menu items.
trailing: PopupMenuButton<Menu>(
icon: const Icon(Icons.more_vert),
onSelected: (Menu item) {
// Callback that sets the selected popup 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'),
),
],
),
),
),
),
);
}
}
86 changes: 86 additions & 0 deletions examples/api/lib/material/popup_menu/popup_menu_button.1.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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 menu item type used by the popup menu below.
enum Menu { itemOne, itemTwo, itemThree, itemFour }

void main() => runApp(const PopupMenuButtonApp());

class PopupMenuButtonApp extends StatelessWidget {
const PopupMenuButtonApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true),
home: const PopupMenuButtonExample(),
);
}
}

class PopupMenuButtonExample extends StatefulWidget {
const PopupMenuButtonExample({super.key});

@override
State<PopupMenuButtonExample> createState() => _PopupMenuButtonExampleState();
}

class _PopupMenuButtonExampleState extends State<PopupMenuButtonExample> {
String? selectedMenu;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('PopupMenuButton Sample')),
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ListTile(
iconColor: Colors.blue[700],
tileColor: Colors.blue[100],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4.0)
),
title: Text(
selectedMenu == null ? 'Select an item' : 'Selected item: $selectedMenu',
style: TextStyle(color: Colors.blue[700]),
),
// This button presents the popup menu items.
trailing: PopupMenuButton<Menu>(
icon: const Icon(Icons.more_vert),
onSelected: (Menu item) {
// Callback that sets the selected popup 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'),
),
],
),
),
),
),
);
}
}
76 changes: 0 additions & 76 deletions examples/api/lib/material/popupmenu/popupmenu.0.dart

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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/checked_popup_menu.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';

void main() {
testWidgets('Can check menu item', (WidgetTester tester) async {
await tester.pumpWidget(
const example.CheckedMenuItemApp(),
);

final Finder containerFinder = find.byType(Container);
expect(tester.getSize(containerFinder), const Size(75.0, 75.0));
await tester.tap(find.byIcon(Icons.more_vert));
await tester.pumpAndSettle();
expect(find.byIcon(Icons.done), findsNothing);
await tester.tap(find.text('Large'));
await tester.pump();
expect(find.byIcon(Icons.done), findsOneWidget);
await tester.pumpAndSettle();
expect(tester.getSize(containerFinder), const Size(150.0, 150.0));
});
}
Loading