| 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 [CupertinoExpansionTile] showing both |
| 8 | /// fade and scroll transition modes. |
| 9 | |
| 10 | void main() => runApp(const CupertinoExpansionTileApp()); |
| 11 | |
| 12 | class CupertinoExpansionTileApp extends StatelessWidget { |
| 13 | const CupertinoExpansionTileApp({super.key}); |
| 14 | |
| 15 | @override |
| 16 | Widget build(BuildContext context) { |
| 17 | return const CupertinoApp( |
| 18 | home: CupertinoPageScaffold( |
| 19 | navigationBar: CupertinoNavigationBar(middle: Text('Cupertino Expansion Tile' )), |
| 20 | backgroundColor: CupertinoColors.systemGroupedBackground, |
| 21 | child: SafeArea(child: ExpansionTileExamples()), |
| 22 | ), |
| 23 | ); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | class ExpansionTileExamples extends StatelessWidget { |
| 28 | const ExpansionTileExamples({super.key}); |
| 29 | |
| 30 | @override |
| 31 | Widget build(BuildContext context) { |
| 32 | return const Column( |
| 33 | spacing: 10, |
| 34 | children: <Widget>[ |
| 35 | TransitionTileSection( |
| 36 | title: 'Fade Transition' , |
| 37 | transitionMode: ExpansionTileTransitionMode.fade, |
| 38 | ), |
| 39 | TransitionTileSection( |
| 40 | title: 'Scroll Transition' , |
| 41 | transitionMode: ExpansionTileTransitionMode.scroll, |
| 42 | ), |
| 43 | ], |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | class TransitionTileSection extends StatefulWidget { |
| 49 | const TransitionTileSection({super.key, required this.title, required this.transitionMode}); |
| 50 | |
| 51 | final String title; |
| 52 | final ExpansionTileTransitionMode transitionMode; |
| 53 | |
| 54 | @override |
| 55 | State<TransitionTileSection> createState() => _TransitionTileSectionState(); |
| 56 | } |
| 57 | |
| 58 | class _TransitionTileSectionState extends State<TransitionTileSection> { |
| 59 | late ExpansibleController _controller; |
| 60 | bool _isExpanded = false; |
| 61 | |
| 62 | @override |
| 63 | void initState() { |
| 64 | super.initState(); |
| 65 | _controller = ExpansibleController(); |
| 66 | _controller.addListener(() { |
| 67 | setState(() { |
| 68 | _isExpanded = _controller.isExpanded; |
| 69 | }); |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | @override |
| 74 | void dispose() { |
| 75 | _controller.dispose(); |
| 76 | super.dispose(); |
| 77 | } |
| 78 | |
| 79 | @override |
| 80 | Widget build(BuildContext context) { |
| 81 | return CupertinoExpansionTile( |
| 82 | title: Text( |
| 83 | ' ${widget.title} - ${_isExpanded ? 'Collapse me' : 'Tap to expand' }' , |
| 84 | style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600), |
| 85 | ), |
| 86 | controller: _controller, |
| 87 | transitionMode: widget.transitionMode, |
| 88 | child: CupertinoListSection.insetGrouped( |
| 89 | children: const <Widget>[ |
| 90 | CupertinoListTile( |
| 91 | leading: Icon(CupertinoIcons.person), |
| 92 | backgroundColor: CupertinoColors.white, |
| 93 | title: Text('Profile' , style: TextStyle(color: CupertinoColors.black)), |
| 94 | ), |
| 95 | CupertinoListTile( |
| 96 | leading: Icon(CupertinoIcons.mail), |
| 97 | backgroundColor: CupertinoColors.white, |
| 98 | title: Text('Messages' , style: TextStyle(color: CupertinoColors.black)), |
| 99 | ), |
| 100 | CupertinoListTile( |
| 101 | leading: Icon(CupertinoIcons.settings), |
| 102 | backgroundColor: CupertinoColors.white, |
| 103 | title: Text('Settings' , style: TextStyle(color: CupertinoColors.black)), |
| 104 | ), |
| 105 | ], |
| 106 | ), |
| 107 | ); |
| 108 | } |
| 109 | } |
| 110 | |