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