Last active
September 24, 2021 04:23
-
-
Save jamieastley/cc86d22602d299e0bf62dcc9081f6fa2 to your computer and use it in GitHub Desktop.
Expandable container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| void main() { | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| debugShowCheckedModeBanner: false, | |
| home: Scaffold( | |
| body: Center( | |
| child: MyWidget(), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class MyWidget extends StatefulWidget { | |
| @override | |
| State<MyWidget> createState() => _MyWidgetState(); | |
| } | |
| class _MyWidgetState extends State<MyWidget> { | |
| bool isExpanded = false; | |
| @override | |
| Widget build(BuildContext context) { | |
| return GestureDetector( | |
| onTap: () { | |
| setState(() { | |
| isExpanded = !isExpanded; | |
| }); | |
| }, | |
| child: Container( | |
| decoration: BoxDecoration( | |
| border: Border.all( | |
| width: 1, | |
| color: Colors.grey, | |
| ), | |
| borderRadius: const BorderRadius.all( | |
| Radius.circular(10), | |
| ), | |
| ), | |
| width: 350, | |
| child: Builder( | |
| builder: (context) { | |
| if (isExpanded) { | |
| return Column( | |
| mainAxisSize: MainAxisSize.min, | |
| children: const [ | |
| Text('Title'), | |
| _Options(), | |
| ], | |
| ); | |
| } else { | |
| return const Text('Title'); | |
| } | |
| }, | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class _Options extends StatelessWidget { | |
| const _Options({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Column(mainAxisSize: MainAxisSize.min, children: const [ | |
| Text('Option 2'), | |
| Text('Option 3'), | |
| ]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment