|
| 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 [PopupMenuButton]. |
| 8 | +
|
| 9 | +void main() => runApp(const PopupMenuApp()); |
| 10 | + |
| 11 | +class PopupMenuApp extends StatelessWidget { |
| 12 | + const PopupMenuApp({super.key}); |
| 13 | + |
| 14 | + @override |
| 15 | + Widget build(BuildContext context) { |
| 16 | + return const MaterialApp( |
| 17 | + home: PopupMenuExample(), |
| 18 | + ); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +enum AnimationStyles { defaultStyle, custom, none } |
| 23 | +const List<(AnimationStyles, String)> animationStyleSegments = <(AnimationStyles, String)>[ |
| 24 | + (AnimationStyles.defaultStyle, 'Default'), |
| 25 | + (AnimationStyles.custom, 'Custom'), |
| 26 | + (AnimationStyles.none, 'None'), |
| 27 | +]; |
| 28 | + |
| 29 | +enum Menu { preview, share, getLink, remove, download } |
| 30 | + |
| 31 | +class PopupMenuExample extends StatefulWidget { |
| 32 | + const PopupMenuExample({super.key}); |
| 33 | + |
| 34 | + @override |
| 35 | + State<PopupMenuExample> createState() => _PopupMenuExampleState(); |
| 36 | +} |
| 37 | + |
| 38 | +class _PopupMenuExampleState extends State<PopupMenuExample> { |
| 39 | + Set<AnimationStyles> _animationStyleSelection = <AnimationStyles>{AnimationStyles.defaultStyle}; |
| 40 | + AnimationStyle? _animationStyle; |
| 41 | + |
| 42 | + @override |
| 43 | + Widget build(BuildContext context) { |
| 44 | + return Scaffold( |
| 45 | + body: SafeArea( |
| 46 | + child: Padding( |
| 47 | + padding: const EdgeInsets.only(top: 50), |
| 48 | + child: Align( |
| 49 | + alignment: Alignment.topCenter, |
| 50 | + child: Column( |
| 51 | + mainAxisSize: MainAxisSize.min, |
| 52 | + mainAxisAlignment: MainAxisAlignment.center, |
| 53 | + children: <Widget>[ |
| 54 | + SegmentedButton<AnimationStyles>( |
| 55 | + selected: _animationStyleSelection, |
| 56 | + onSelectionChanged: (Set<AnimationStyles> styles) { |
| 57 | + setState(() { |
| 58 | + _animationStyleSelection = styles; |
| 59 | + switch (styles.first) { |
| 60 | + case AnimationStyles.defaultStyle: |
| 61 | + _animationStyle = null; |
| 62 | + case AnimationStyles.custom: |
| 63 | + _animationStyle = AnimationStyle( |
| 64 | + curve: Easing.emphasizedDecelerate, |
| 65 | + duration: const Duration(seconds: 3), |
| 66 | + ); |
| 67 | + case AnimationStyles.none: |
| 68 | + _animationStyle = AnimationStyle.noAnimation; |
| 69 | + } |
| 70 | + }); |
| 71 | + }, |
| 72 | + segments: animationStyleSegments |
| 73 | + .map<ButtonSegment<AnimationStyles>>(((AnimationStyles, String) shirt) { |
| 74 | + return ButtonSegment<AnimationStyles>(value: shirt.$1, label: Text(shirt.$2)); |
| 75 | + }) |
| 76 | + .toList(), |
| 77 | + ), |
| 78 | + const SizedBox(height: 10), |
| 79 | + PopupMenuButton<Menu>( |
| 80 | + popUpAnimationStyle: _animationStyle, |
| 81 | + icon: const Icon(Icons.more_vert), |
| 82 | + onSelected: (Menu item) { }, |
| 83 | + itemBuilder: (BuildContext context) => <PopupMenuEntry<Menu>>[ |
| 84 | + const PopupMenuItem<Menu>( |
| 85 | + value: Menu.preview, |
| 86 | + child: ListTile( |
| 87 | + leading: Icon(Icons.visibility_outlined), |
| 88 | + title: Text('Preview'), |
| 89 | + ), |
| 90 | + ), |
| 91 | + const PopupMenuItem<Menu>( |
| 92 | + value: Menu.share, |
| 93 | + child: ListTile( |
| 94 | + leading: Icon(Icons.share_outlined), |
| 95 | + title: Text('Share'), |
| 96 | + ), |
| 97 | + ), |
| 98 | + const PopupMenuItem<Menu>( |
| 99 | + value: Menu.getLink, |
| 100 | + child: ListTile( |
| 101 | + leading: Icon(Icons.link_outlined), |
| 102 | + title: Text('Get link'), |
| 103 | + ), |
| 104 | + ), |
| 105 | + const PopupMenuDivider(), |
| 106 | + const PopupMenuItem<Menu>( |
| 107 | + value: Menu.remove, |
| 108 | + child: ListTile( |
| 109 | + leading: Icon(Icons.delete_outline), |
| 110 | + title: Text('Remove'), |
| 111 | + ), |
| 112 | + ), |
| 113 | + const PopupMenuItem<Menu>( |
| 114 | + value: Menu.download, |
| 115 | + child: ListTile( |
| 116 | + leading: Icon(Icons.download_outlined), |
| 117 | + title: Text('Download'), |
| 118 | + ), |
| 119 | + ), |
| 120 | + ], |
| 121 | + ), |
| 122 | + ], |
| 123 | + ), |
| 124 | + ), |
| 125 | + ), |
| 126 | + ), |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
0 commit comments