1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
---|---|
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | import 'package:flutter/material.dart'; |
6 | |
7 | /// Flutter code sample for [MenuAnchor]. |
8 | |
9 | void main() => runApp(const MenuAnchorApp()); |
10 | |
11 | // This is the type used by the menu below. |
12 | enum SampleItem { itemOne, itemTwo, itemThree } |
13 | |
14 | class MenuAnchorApp extends StatelessWidget { |
15 | const MenuAnchorApp({super.key}); |
16 | |
17 | @override |
18 | Widget build(BuildContext context) { |
19 | return const MaterialApp(home: MenuAnchorExample()); |
20 | } |
21 | } |
22 | |
23 | class MenuAnchorExample extends StatefulWidget { |
24 | const MenuAnchorExample({super.key}); |
25 | |
26 | @override |
27 | State<MenuAnchorExample> createState() => _MenuAnchorExampleState(); |
28 | } |
29 | |
30 | class _MenuAnchorExampleState extends State<MenuAnchorExample> { |
31 | SampleItem? selectedMenu; |
32 | |
33 | @override |
34 | Widget build(BuildContext context) { |
35 | return Scaffold( |
36 | appBar: AppBar( |
37 | title: const Text('MenuAnchorButton'), |
38 | backgroundColor: Theme.of(context).primaryColorLight, |
39 | ), |
40 | body: Center( |
41 | child: MenuAnchor( |
42 | builder: (BuildContext context, MenuController controller, Widget? child) { |
43 | return IconButton( |
44 | onPressed: () { |
45 | if (controller.isOpen) { |
46 | controller.close(); |
47 | } else { |
48 | controller.open(); |
49 | } |
50 | }, |
51 | icon: const Icon(Icons.more_horiz), |
52 | tooltip: 'Show menu', |
53 | ); |
54 | }, |
55 | menuChildren: List<MenuItemButton>.generate( |
56 | 3, |
57 | (int index) => MenuItemButton( |
58 | onPressed: () => setState(() => selectedMenu = SampleItem.values[index]), |
59 | child: Text('Item${index + 1} '), |
60 | ), |
61 | ), |
62 | ), |
63 | ), |
64 | ); |
65 | } |
66 | } |
67 |