Skip to content

Instantly share code, notes, and snippets.

@gsandaru
Created June 3, 2025 05:23
Show Gist options
  • Select an option

  • Save gsandaru/cd5f37fee39ff860984d5ca4a88798f5 to your computer and use it in GitHub Desktop.

Select an option

Save gsandaru/cd5f37fee39ff860984d5ca4a88798f5 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Expandable Card',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Expandable Card'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: ListView(
children: const [
ExpandableCard(
title: 'Card 1',
content:
'This is the content for card 1. It can be any amount of text, widgets, or whatever you want to put here.',
),
SizedBox(height: 16),
ExpandableCard(
title: 'Card 2',
content:
'This is the content for card 2. It can be any amount of text, widgets, or whatever you want to put here. This is just a demonstration, but you could do anything you want.',
),
SizedBox(height: 16),
ExpandableCard(
title: 'Card 3',
content:
'This is the content for card 3. It can be any amount of text, widgets, or whatever you want to put here. This could be settings, data displays, or really anything you can imagine.',
),
],
),
),
);
}
}
class ExpandableCard extends StatefulWidget {
const ExpandableCard({super.key, required this.title, required this.content});
final String title;
final String content;
@override
State<ExpandableCard> createState() => _ExpandableCardState();
}
class _ExpandableCardState extends State<ExpandableCard> {
bool _isExpanded = false;
@override
Widget build(BuildContext context) {
return Card(
elevation: 4,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
clipBehavior: Clip.antiAlias,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
InkWell(
onTap: () {
setState(() {
_isExpanded = !_isExpanded;
});
},
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
widget.title,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
AnimatedRotation(
duration: const Duration(milliseconds: 200),
turns: _isExpanded ? 0.5 : 0,
child: const Icon(Icons.arrow_drop_down),
),
],
),
),
),
AnimatedSize(
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
child: ConstrainedBox(
constraints: _isExpanded
? const BoxConstraints()
: const BoxConstraints(maxHeight: 0),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Text(widget.content),
),
),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment