Skip to content

Instantly share code, notes, and snippets.

@Lxxyx
Created January 22, 2025 01:29
Show Gist options
  • Save Lxxyx/f557ea812f410e0317c96d619500d525 to your computer and use it in GitHub Desktop.
Save Lxxyx/f557ea812f410e0317c96d619500d525 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',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorSchemeSeed: Colors.blue,
),
home: const MyHomePage(title: 'Expandable Card Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
super.key,
required this.title,
});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: ExpandableCard(
name: 'John Doe',
introduction: 'This is a brief introduction about John Doe.',
description: 'This is a longer description about John Doe. You can add as much text as you want here.',
),
),
);
}
}
class ExpandableCard extends StatefulWidget {
final String name;
final String introduction;
final String description;
const ExpandableCard({
super.key,
required this.name,
required this.introduction,
required this.description,
});
@override
State<ExpandableCard> createState() => _ExpandableCardState();
}
class _ExpandableCardState extends State<ExpandableCard> {
bool _isExpanded = false;
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.all(16),
child: Column(
children: [
ListTile(
title: Text(widget.name),
subtitle: Text(widget.introduction),
trailing: IconButton(
icon: _isExpanded ? const Icon(Icons.arrow_upward) : const Icon(Icons.arrow_downward),
onPressed: () {
setState(() {
_isExpanded = !_isExpanded;
});
},
),
),
_isExpanded
? Padding(
padding: const EdgeInsets.all(16),
child: Text(widget.description),
)
: const SizedBox.shrink(),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment