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/cupertino.dart'; |
6 | |
7 | /// Flutter code sample for [CupertinoContextMenu]. |
8 | |
9 | final DecorationTween _tween = DecorationTween( |
10 | begin: BoxDecoration( |
11 | color: CupertinoColors.systemYellow, |
12 | boxShadow: const <BoxShadow>[], |
13 | borderRadius: BorderRadius.circular(20.0), |
14 | ), |
15 | end: BoxDecoration( |
16 | color: CupertinoColors.systemYellow, |
17 | boxShadow: CupertinoContextMenu.kEndBoxShadow, |
18 | borderRadius: BorderRadius.circular(20.0), |
19 | ), |
20 | ); |
21 | |
22 | void main() => runApp(const ContextMenuApp()); |
23 | |
24 | class ContextMenuApp extends StatelessWidget { |
25 | const ContextMenuApp({super.key}); |
26 | |
27 | @override |
28 | Widget build(BuildContext context) { |
29 | return const CupertinoApp( |
30 | theme: CupertinoThemeData(brightness: Brightness.light), |
31 | home: ContextMenuExample(), |
32 | ); |
33 | } |
34 | } |
35 | |
36 | class ContextMenuExample extends StatelessWidget { |
37 | const ContextMenuExample({super.key}); |
38 | |
39 | // Or just do this inline in the builder below? |
40 | static Animation<Decoration> _boxDecorationAnimation(Animation<double> animation) { |
41 | return _tween.animate( |
42 | CurvedAnimation( |
43 | parent: animation, |
44 | curve: Interval(0.0, CupertinoContextMenu.animationOpensAt), |
45 | ), |
46 | ); |
47 | } |
48 | |
49 | @override |
50 | Widget build(BuildContext context) { |
51 | return CupertinoPageScaffold( |
52 | navigationBar: const CupertinoNavigationBar(middle: Text('CupertinoContextMenu Sample' )), |
53 | child: Center( |
54 | child: SizedBox( |
55 | width: 100, |
56 | height: 100, |
57 | child: CupertinoContextMenu.builder( |
58 | actions: <Widget>[ |
59 | CupertinoContextMenuAction( |
60 | onPressed: () { |
61 | Navigator.pop(context); |
62 | }, |
63 | isDefaultAction: true, |
64 | trailingIcon: CupertinoIcons.doc_on_clipboard_fill, |
65 | child: const Text('Copy' ), |
66 | ), |
67 | CupertinoContextMenuAction( |
68 | onPressed: () { |
69 | Navigator.pop(context); |
70 | }, |
71 | trailingIcon: CupertinoIcons.share, |
72 | child: const Text('Share' ), |
73 | ), |
74 | CupertinoContextMenuAction( |
75 | onPressed: () { |
76 | Navigator.pop(context); |
77 | }, |
78 | trailingIcon: CupertinoIcons.heart, |
79 | child: const Text('Favorite' ), |
80 | ), |
81 | CupertinoContextMenuAction( |
82 | onPressed: () { |
83 | Navigator.pop(context); |
84 | }, |
85 | isDestructiveAction: true, |
86 | trailingIcon: CupertinoIcons.delete, |
87 | child: const Text('Delete' ), |
88 | ), |
89 | ], |
90 | builder: (BuildContext context, Animation<double> animation) { |
91 | final Animation<Decoration> boxDecorationAnimation = _boxDecorationAnimation( |
92 | animation, |
93 | ); |
94 | |
95 | return Container( |
96 | decoration: |
97 | animation.value < CupertinoContextMenu.animationOpensAt |
98 | ? boxDecorationAnimation.value |
99 | : null, |
100 | child: Container( |
101 | decoration: BoxDecoration( |
102 | color: CupertinoColors.systemYellow, |
103 | borderRadius: BorderRadius.circular(20.0), |
104 | ), |
105 | child: const FlutterLogo(size: 500.0), |
106 | ), |
107 | ); |
108 | }, |
109 | ), |
110 | ), |
111 | ), |
112 | ); |
113 | } |
114 | } |
115 | |