|
6 | 6 |
|
7 | 7 | import 'package:flutter/material.dart'; |
8 | 8 |
|
9 | | -void main() => runApp(const MyApp()); |
| 9 | +final List<int> _items = List<int>.generate(51, (int index) => index); |
10 | 10 |
|
11 | | -class MyApp extends StatelessWidget { |
12 | | - const MyApp({super.key}); |
| 11 | +void main() => runApp(const AppBarApp()); |
13 | 12 |
|
14 | | - static const String _title = 'Flutter Code Sample'; |
| 13 | +class AppBarApp extends StatelessWidget { |
| 14 | + const AppBarApp({super.key}); |
15 | 15 |
|
16 | 16 | @override |
17 | 17 | Widget build(BuildContext context) { |
18 | | - return const MaterialApp( |
19 | | - title: _title, |
20 | | - home: MyStatelessWidget(), |
| 18 | + return MaterialApp( |
| 19 | + theme: ThemeData( |
| 20 | + colorSchemeSeed: const Color(0xff6750a4), |
| 21 | + useMaterial3: true, |
| 22 | + ), |
| 23 | + home: const AppBarExample(), |
21 | 24 | ); |
22 | 25 | } |
23 | 26 | } |
24 | 27 |
|
25 | | -class MyStatelessWidget extends StatelessWidget { |
26 | | - const MyStatelessWidget({super.key}); |
| 28 | +class AppBarExample extends StatefulWidget { |
| 29 | + const AppBarExample({super.key}); |
| 30 | + |
| 31 | + @override |
| 32 | + State<AppBarExample> createState() => _AppBarExampleState(); |
| 33 | +} |
| 34 | + |
| 35 | +class _AppBarExampleState extends State<AppBarExample> { |
| 36 | + bool shadowColor = false; |
| 37 | + double? scrolledUnderElevation; |
27 | 38 |
|
28 | 39 | @override |
29 | 40 | Widget build(BuildContext context) { |
30 | | - final ButtonStyle style = |
31 | | - TextButton.styleFrom(primary: Theme.of(context).colorScheme.onPrimary); |
| 41 | + final ColorScheme colorScheme = Theme.of(context).colorScheme; |
| 42 | + final Color oddItemColor = colorScheme.primary.withOpacity(0.05); |
| 43 | + final Color evenItemColor = colorScheme.primary.withOpacity(0.15); |
| 44 | + |
32 | 45 | return Scaffold( |
33 | 46 | appBar: AppBar( |
34 | | - actions: <Widget>[ |
35 | | - TextButton( |
36 | | - style: style, |
37 | | - onPressed: () {}, |
38 | | - child: const Text('Action 1'), |
39 | | - ), |
40 | | - TextButton( |
41 | | - style: style, |
42 | | - onPressed: () {}, |
43 | | - child: const Text('Action 2'), |
| 47 | + title: const Text('AppBar Demo'), |
| 48 | + scrolledUnderElevation: scrolledUnderElevation, |
| 49 | + shadowColor: shadowColor ? Theme.of(context).colorScheme.shadow : null, |
| 50 | + ), |
| 51 | + body: GridView.builder( |
| 52 | + shrinkWrap: true, |
| 53 | + itemCount: _items.length, |
| 54 | + padding: const EdgeInsets.all(8.0), |
| 55 | + gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( |
| 56 | + crossAxisCount: 3, |
| 57 | + childAspectRatio: 2.0, |
| 58 | + mainAxisSpacing: 10.0, |
| 59 | + crossAxisSpacing: 10.0, |
| 60 | + ), |
| 61 | + itemBuilder: (BuildContext context, int index) { |
| 62 | + if (index == 0) { |
| 63 | + return Center( |
| 64 | + child: Text( |
| 65 | + 'Scroll to see the Appbar in effect.', |
| 66 | + style: Theme.of(context).textTheme.labelLarge, |
| 67 | + textAlign: TextAlign.center, |
| 68 | + ), |
| 69 | + ); |
| 70 | + } |
| 71 | + return Container( |
| 72 | + alignment: Alignment.center, |
| 73 | + // tileColor: _items[index].isOdd ? oddItemColor : evenItemColor, |
| 74 | + decoration: BoxDecoration( |
| 75 | + borderRadius: BorderRadius.circular(20.0), |
| 76 | + color: _items[index].isOdd ? oddItemColor : evenItemColor, |
| 77 | + ), |
| 78 | + child: Text('Item $index'), |
| 79 | + ); |
| 80 | + }, |
| 81 | + ), |
| 82 | + bottomNavigationBar: BottomAppBar( |
| 83 | + child: Padding( |
| 84 | + padding: const EdgeInsets.all(8), |
| 85 | + child: OverflowBar( |
| 86 | + overflowAlignment: OverflowBarAlignment.center, |
| 87 | + alignment: MainAxisAlignment.center, |
| 88 | + overflowSpacing: 5.0, |
| 89 | + children: <Widget>[ |
| 90 | + ElevatedButton.icon( |
| 91 | + onPressed: () { |
| 92 | + setState(() { |
| 93 | + shadowColor = !shadowColor; |
| 94 | + }); |
| 95 | + }, |
| 96 | + icon: Icon( |
| 97 | + shadowColor ? Icons.visibility_off : Icons.visibility, |
| 98 | + ), |
| 99 | + label: const Text('shadow color'), |
| 100 | + ), |
| 101 | + const SizedBox(width: 5), |
| 102 | + ElevatedButton.icon( |
| 103 | + onPressed: () { |
| 104 | + if (scrolledUnderElevation == null) { |
| 105 | + setState(() { |
| 106 | + // Default elevation is 3.0, increment by 1.0. |
| 107 | + scrolledUnderElevation = 4.0; |
| 108 | + }); |
| 109 | + } else { |
| 110 | + setState(() { |
| 111 | + scrolledUnderElevation = scrolledUnderElevation! + 1.0; |
| 112 | + }); |
| 113 | + } |
| 114 | + }, |
| 115 | + icon: const Icon(Icons.add), |
| 116 | + label: Text( |
| 117 | + 'scrolledUnderElevation: ${scrolledUnderElevation ?? 'default'}', |
| 118 | + ), |
| 119 | + ), |
| 120 | + ], |
44 | 121 | ), |
45 | | - ], |
| 122 | + ), |
46 | 123 | ), |
47 | 124 | ); |
48 | 125 | } |
|
0 commit comments