|
| 1 | +import 'package:expansion_tile_group/expansion_tile_group.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | + |
| 4 | +class KeepStatePage extends StatefulWidget { |
| 5 | + const KeepStatePage({super.key}); |
| 6 | + |
| 7 | + @override |
| 8 | + State<KeepStatePage> createState() => _KeepStatePageState(); |
| 9 | +} |
| 10 | + |
| 11 | +class _KeepStatePageState extends State<KeepStatePage> { |
| 12 | + @override |
| 13 | + Widget build(BuildContext context) { |
| 14 | + return Scaffold( |
| 15 | + appBar: AppBar( |
| 16 | + title: const Text('Keep State Example'), |
| 17 | + ), |
| 18 | + body: SingleChildScrollView( |
| 19 | + child: Padding( |
| 20 | + padding: const EdgeInsets.all(16.0), |
| 21 | + child: Column( |
| 22 | + children: [ |
| 23 | + Row( |
| 24 | + children: [ |
| 25 | + _buildGroupLayout( |
| 26 | + context, |
| 27 | + title: 'Group 1: keepState: true', |
| 28 | + child: ExpansionTileGroup( |
| 29 | + spaceBetweenItem: 16, |
| 30 | + keepState: true, |
| 31 | + children: [ |
| 32 | + _buildExpansionItem(context), |
| 33 | + _buildExpansionItem(context), |
| 34 | + _buildExpansionItem(context), |
| 35 | + ], |
| 36 | + ), |
| 37 | + ), |
| 38 | + const SizedBox( |
| 39 | + width: 16, |
| 40 | + ), |
| 41 | + _buildGroupLayout( |
| 42 | + context, |
| 43 | + title: 'Group 2: keepState: false', |
| 44 | + child: ExpansionTileGroup( |
| 45 | + spaceBetweenItem: 16, |
| 46 | + keepState: false, |
| 47 | + children: [ |
| 48 | + _buildExpansionItem(context), |
| 49 | + _buildExpansionItem(context), |
| 50 | + _buildExpansionItem(context), |
| 51 | + ], |
| 52 | + ), |
| 53 | + ), |
| 54 | + ], |
| 55 | + ), |
| 56 | + const SizedBox( |
| 57 | + height: 16, |
| 58 | + ), |
| 59 | + const Text( |
| 60 | + 'Try to expand some items of each group and then tap to below button, you can see the different', |
| 61 | + textAlign: TextAlign.center, |
| 62 | + ), |
| 63 | + const SizedBox( |
| 64 | + height: 16, |
| 65 | + ), |
| 66 | + ElevatedButton( |
| 67 | + onPressed: () { |
| 68 | + setState(() {}); |
| 69 | + }, |
| 70 | + child: const Text('Rebuild this Page')), |
| 71 | + ], |
| 72 | + ), |
| 73 | + ), |
| 74 | + ), |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + ExpansionTileItem _buildExpansionItem(BuildContext context, |
| 79 | + {bool isExpanded = false}) { |
| 80 | + return ExpansionTileItem.outlined( |
| 81 | + initiallyExpanded: isExpanded, |
| 82 | + title: const Text('ExpansionTile Item'), |
| 83 | + children: [ |
| 84 | + Material( |
| 85 | + child: InkWell( |
| 86 | + onTap: () {}, |
| 87 | + child: const Text( |
| 88 | + ''' Nullam eleifend ultrices tortor, sit amet gravida sapien cursus vitae. Duis rutrum convallis erat et ultrices. Morbi a luctus ligula, at varius ligula. Nam mollis sapien ac nunc hendrerit consequat. Cras posuere metus felis, at pellentesque sem ornare id. Praesent ut nunc aliquam, dictum felis eu, congue metus. Nunc vitae elit eros. In eu dui pharetra, varius metus a, efficitur eros.'''), |
| 89 | + ), |
| 90 | + ), |
| 91 | + ], |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + Widget _buildGroupLayout(BuildContext context, |
| 96 | + {required String title, required Widget child}) { |
| 97 | + return Expanded( |
| 98 | + child: Column( |
| 99 | + mainAxisAlignment: MainAxisAlignment.start, |
| 100 | + children: [ |
| 101 | + Text( |
| 102 | + title, |
| 103 | + style: const TextStyle(fontSize: 20), |
| 104 | + ), |
| 105 | + const SizedBox( |
| 106 | + height: 16, |
| 107 | + ), |
| 108 | + Card( |
| 109 | + child: Padding( |
| 110 | + padding: const EdgeInsets.all(8.0), |
| 111 | + child: child, |
| 112 | + ), |
| 113 | + ), |
| 114 | + ], |
| 115 | + ), |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments