Skip to content

Instantly share code, notes, and snippets.

@jamieastley
Last active September 24, 2021 04:23
Show Gist options
  • Select an option

  • Save jamieastley/cc86d22602d299e0bf62dcc9081f6fa2 to your computer and use it in GitHub Desktop.

Select an option

Save jamieastley/cc86d22602d299e0bf62dcc9081f6fa2 to your computer and use it in GitHub Desktop.
Expandable container
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