Help users move around the app · 5 h
Talk to the user and show what is happening · 5 h
Make every pixel respond to touch · 4 h
Make the screen move, smoothly · 5 h
The skeleton of a screen: app bar, body, drawer, bottom bar, floating button.
3 to 5 main sections that users switch often.
More sections in a side menu, or related lists in tabs.
Scaffold is the frame of a screen. Each slot has a name.
void main() {
runApp(MaterialApp(home: Scaffold(
appBar: AppBar(title: const Text('Ranchi Shop'), actions: [IconButton(icon: const Icon(Icons.search), onPressed: () {})]),
drawer: const Drawer(child: Center(child: Text('Menu'))),
body: const Center(child: Text('Body')),
floatingActionButton: FloatingActionButton(onPressed: () {}, child: const Icon(Icons.add)),
bottomNavigationBar: BottomNavigationBar(items: const [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),
]),
)));
}Keep the current index in state. BottomNavigationBar (or the Material 3 NavigationBar) calls onTap with the new index. Show a different body for each index.
class Shell extends StatefulWidget {
const Shell({super.key});
@override
State<Shell> createState() => _ShellState();
}
class _ShellState extends State<Shell> {
int index = 0;
final pages = const [Center(child: Text('Home page')), Center(child: Text('Orders page')), Center(child: Text('Profile page'))];
@override
Widget build(BuildContext context) => Scaffold(
body: pages[index],
bottomNavigationBar: NavigationBar(
selectedIndex: index,
onDestinationSelected: (i) => setState(() => index = i),
destinations: const [
NavigationDestination(icon: Icon(Icons.home_outlined), selectedIcon: Icon(Icons.home), label: 'Home'),
NavigationDestination(icon: Icon(Icons.receipt_long_outlined), selectedIcon: Icon(Icons.receipt_long), label: 'Orders'),
NavigationDestination(icon: Icon(Icons.person_outline), selectedIcon: Icon(Icons.person), label: 'Profile'),
],
),
);
}
void main() => runApp(const MaterialApp(home: Shell()));Add a Drawer to the Scaffold and the AppBar shows the menu icon automatically. Put a DrawerHeader and ListTile items inside. Tap the menu icon or swipe from the left edge to open it.
Navigator.pop(context).void main() {
runApp(MaterialApp(home: Scaffold(
appBar: AppBar(title: const Text('My School')),
drawer: Drawer(child: ListView(children: [
const DrawerHeader(decoration: BoxDecoration(color: Colors.indigo), child: Text('DAV School Kanke', style: TextStyle(color: Colors.white, fontSize: 22))),
ListTile(leading: const Icon(Icons.home), title: const Text('Home'), onTap: () {}),
ListTile(leading: const Icon(Icons.calendar_month), title: const Text('Timetable'), onTap: () {}),
ListTile(leading: const Icon(Icons.payments), title: const Text('Fees'), onTap: () {}),
const Divider(),
ListTile(leading: const Icon(Icons.logout), title: const Text('Logout'), onTap: () {}),
])),
body: const Center(child: Text('Tap the menu icon')),
)));
}Tabs show related lists under one screen. DefaultTabController connects a TabBar (the labels) with a TabBarView (the pages). Swiping and tapping stay in sync automatically.
void main() {
runApp(const MaterialApp(home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(title: Text('Menu'), bottom: TabBar(tabs: [Tab(text: 'Veg'), Tab(text: 'Non-veg'), Tab(text: 'Sweets')])),
body: TabBarView(children: [
Center(child: Text('Paneer tikka, Dal fry')),
Center(child: Text('Chicken curry, Egg roll')),
Center(child: Text('Rasgulla, Gulab jamun')),
]),
),
)));
}void main() {
runApp(MaterialApp(home: Scaffold(
appBar: AppBar(title: const Text('Ranchi Kirana')),
body: const Center(child: Text('Welcome to Ranchi Kirana', style: TextStyle(fontSize: 20))),
floatingActionButton: FloatingActionButton(onPressed: () {}, child: const Icon(Icons.add)),
bottomNavigationBar: BottomNavigationBar(currentIndex: 0, items: const [
BottomNavigationBarItem(icon: Icon(Icons.store), label: 'Shop'),
BottomNavigationBarItem(icon: Icon(Icons.shopping_cart), label: 'Cart'),
]),
)));
}class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
String chosen = 'Nothing yet';
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Inbox'), actions: [
IconButton(icon: const Icon(Icons.search), onPressed: () {}),
PopupMenuButton<String>(
onSelected: (v) => setState(() => chosen = v),
itemBuilder: (c) => const [PopupMenuItem(value: 'Refresh', child: Text('Refresh')), PopupMenuItem(value: 'Settings', child: Text('Settings')), PopupMenuItem(value: 'Help', child: Text('Help'))],
),
]),
body: Center(child: Text('Chosen: $chosen', style: const TextStyle(fontSize: 22))),
);
}
void main() => runApp(const MaterialApp(home: Page()));void main() {
runApp(MaterialApp(home: Scaffold(
appBar: AppBar(title: const Text('Kanke School')),
drawer: Drawer(child: ListView(children: [
UserAccountsDrawerHeader(accountName: const Text('Asha Kumari'), accountEmail: const Text('asha@example.com'), currentAccountPicture: const CircleAvatar(child: Text('A'))),
const ListTile(leading: Icon(Icons.home), title: Text('Home')),
const ListTile(leading: Icon(Icons.book), title: Text('Homework')),
const ListTile(leading: Icon(Icons.directions_bus), title: Text('School bus')),
const ListTile(leading: Icon(Icons.settings), title: Text('Settings')),
])),
body: const Center(child: Text('Open the drawer')),
)));
}class Shell extends StatefulWidget {
const Shell({super.key});
@override
State<Shell> createState() => _ShellState();
}
class _ShellState extends State<Shell> {
int i = 0;
@override
Widget build(BuildContext context) {
const pages = [Text('Home: offers'), Text('Cart: 2 items'), Text('Profile: Asha')];
return Scaffold(
body: Center(child: DefaultTextStyle(style: const TextStyle(fontSize: 24, color: Colors.black), child: pages[i])),
bottomNavigationBar: BottomNavigationBar(
currentIndex: i, onTap: (v) => setState(() => i = v),
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.shopping_cart), label: 'Cart'),
BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),
],
),
);
}
}
void main() => runApp(const MaterialApp(home: Shell()));class Shell extends StatefulWidget {
const Shell({super.key});
@override
State<Shell> createState() => _ShellState();
}
class _ShellState extends State<Shell> {
int i = 1;
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: Text('Tab ${i + 1}', style: const TextStyle(fontSize: 28))),
bottomNavigationBar: NavigationBar(
selectedIndex: i, onDestinationSelected: (v) => setState(() => i = v),
destinations: const [
NavigationDestination(icon: Icon(Icons.explore_outlined), selectedIcon: Icon(Icons.explore), label: 'Explore'),
NavigationDestination(icon: Icon(Icons.favorite_border), selectedIcon: Icon(Icons.favorite), label: 'Saved'),
NavigationDestination(icon: Icon(Icons.person_outline), selectedIcon: Icon(Icons.person), label: 'Me'),
],
),
);
}
void main() => runApp(const MaterialApp(home: Shell()));Widget menu(List<List<String>> items) => ListView(children: [for (final i in items) ListTile(title: Text(i[0]), trailing: Text('Rs ${i[1]}'))]);
void main() {
runApp(MaterialApp(home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(title: const Text('Thali Ghar'), bottom: const TabBar(tabs: [Tab(icon: Icon(Icons.eco), text: 'Veg'), Tab(icon: Icon(Icons.set_meal), text: 'Non-veg'), Tab(icon: Icon(Icons.cake), text: 'Sweets')])),
body: TabBarView(children: [
menu([['Paneer butter masala', '220'], ['Dal tadka', '140'], ['Aloo gobi', '120']]),
menu([['Chicken curry', '260'], ['Egg curry', '150'], ['Fish fry', '280']]),
menu([['Gulab jamun', '60'], ['Rasgulla', '50'], ['Malpua', '70']]),
]),
),
)));
}class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
final ctl = ScrollController();
bool big = true;
@override
void initState() {
super.initState();
ctl.addListener(() {
final b = ctl.offset < 50;
if (b != big) setState(() => big = b);
});
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Mail')),
body: ListView.builder(controller: ctl, itemCount: 40, itemBuilder: (c, i) => ListTile(leading: const Icon(Icons.mail_outline), title: Text('Message ${i + 1}'))),
floatingActionButton: FloatingActionButton.extended(
onPressed: () {}, icon: const Icon(Icons.edit),
label: big ? const Text('Compose') : const SizedBox.shrink(),
),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
int sel = 0;
final names = ['Dashboard', 'Students', 'Fees', 'Reports'];
final icons = [Icons.dashboard, Icons.people, Icons.payments, Icons.bar_chart];
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text(names[sel])),
drawer: Drawer(child: ListView(children: [
const DrawerHeader(child: Text('School Admin', style: TextStyle(fontSize: 22))),
for (var i = 0; i < names.length; i++) ListTile(
leading: Icon(icons[i]), title: Text(names[i]), selected: sel == i,
onTap: () { setState(() => sel = i); Navigator.pop(context); },
),
])),
body: Center(child: Text('${names[sel]} page', style: const TextStyle(fontSize: 26))),
);
}
void main() => runApp(const MaterialApp(home: Home()));class Counter extends StatefulWidget {
final String name;
const Counter(this.name, {super.key});
@override
State<Counter> createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int n = 0;
@override
Widget build(BuildContext context) => Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
Text('${widget.name}: $n', style: const TextStyle(fontSize: 26)),
FilledButton(onPressed: () => setState(() => n++), child: const Text('Add')),
]));
}
class Shell extends StatefulWidget {
const Shell({super.key});
@override
State<Shell> createState() => _ShellState();
}
class _ShellState extends State<Shell> {
int i = 0;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('IndexedStack demo')),
body: IndexedStack(index: i, children: const [Counter('Home'), Counter('Orders'), Counter('Profile')]),
bottomNavigationBar: NavigationBar(
selectedIndex: i, onDestinationSelected: (v) => setState(() => i = v),
destinations: const [
NavigationDestination(icon: Icon(Icons.home), label: 'Home'),
NavigationDestination(icon: Icon(Icons.receipt), label: 'Orders'),
NavigationDestination(icon: Icon(Icons.person), label: 'Profile'),
],
),
);
}
void main() => runApp(const MaterialApp(home: Shell()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
bool searching = false;
String q = '';
final all = ['Ranchi', 'Patna', 'Gaya', 'Dhanbad', 'Bokaro', 'Deoghar', 'Muzaffarpur', 'Purnia'];
@override
Widget build(BuildContext context) {
final list = all.where((e) => e.toLowerCase().contains(q.toLowerCase())).toList();
return Scaffold(
appBar: AppBar(
title: searching
? TextField(autofocus: true, decoration: const InputDecoration(hintText: 'Search city', border: InputBorder.none), onChanged: (v) => setState(() => q = v))
: const Text('Cities'),
actions: [IconButton(
icon: Icon(searching ? Icons.close : Icons.search),
onPressed: () => setState(() { searching = !searching; q = ''; }),
)],
),
body: ListView(children: [for (final c in list) ListTile(title: Text(c))]),
);
}
}
void main() => runApp(const MaterialApp(home: Page()));appBar, body, drawer, FAB, bottom bar.
index in state, 3 to 5 sections.
Side menu; pop it after a tap.
DefaultTabController + TabBar + TabBarView.
Keeps tab state alive.
Actions, popup menu, search toggle.
Stops everything and asks. Use for confirmations.
A short message at the bottom that disappears. Can have an action.
Spinners and bars that say: please wait.
showDialog puts a dialog on top of the screen and returns a Future. When the user taps a button, you call Navigator.pop(context, value) and that value comes back to your await.
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
String result = 'No answer yet';
Future<void> ask() async {
final ok = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Delete order?'),
content: const Text('Order #1042 will be removed.'),
actions: [
TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('Cancel')),
FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('Delete')),
],
),
);
setState(() => result = ok == true ? 'Deleted' : 'Cancelled');
}
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
FilledButton(onPressed: ask, child: const Text('Delete order')),
const SizedBox(height: 16),
Text(result),
])),
);
}
void main() => runApp(const MaterialApp(home: Page()));Get the messenger with ScaffoldMessenger.of(context) and call showSnackBar. A SnackBarAction adds a button such as UNDO. A snack bar disappears after a few seconds.
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
final items = ['Dhuska', 'Litti', 'Thekua'];
@override
Widget build(BuildContext context) => Scaffold(
body: ListView(children: [
for (final s in List.of(items)) ListTile(
title: Text(s),
trailing: IconButton(icon: const Icon(Icons.delete), onPressed: () {
final idx = items.indexOf(s);
setState(() => items.remove(s));
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('$s deleted'),
action: SnackBarAction(label: 'UNDO', onPressed: () => setState(() => items.insert(idx, s))),
));
}),
),
]),
);
}
void main() => runApp(const MaterialApp(home: Page()));showModalBottomSheet slides a panel up from the bottom. It is perfect for share menus, filters and quick choices, and it is easier to reach with a thumb than a dialog.
class Page extends StatelessWidget {
const Page({super.key});
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: FilledButton(
onPressed: () => showModalBottomSheet(
context: context,
builder: (ctx) => Column(mainAxisSize: MainAxisSize.min, children: [
const ListTile(title: Text('Share via', style: TextStyle(fontWeight: FontWeight.bold))),
ListTile(leading: const Icon(Icons.message), title: const Text('WhatsApp'), onTap: () => Navigator.pop(ctx)),
ListTile(leading: const Icon(Icons.mail), title: const Text('Email'), onTap: () => Navigator.pop(ctx)),
ListTile(leading: const Icon(Icons.copy), title: const Text('Copy link'), onTap: () => Navigator.pop(ctx)),
]),
),
child: const Text('Share'),
)),
);
}
void main() => runApp(const MaterialApp(home: Page()));Without a value the indicator spins or slides forever (indeterminate). With value from 0.0 to 1.0 it shows real progress (determinate).
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
double p = 0.35;
@override
Widget build(BuildContext context) => Scaffold(
body: Padding(padding: const EdgeInsets.all(24), child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
const CircularProgressIndicator(),
const SizedBox(height: 24),
LinearProgressIndicator(value: p),
const SizedBox(height: 8),
Text('Downloading ${(p * 100).round()}%'),
Slider(value: p, onChanged: (v) => setState(() => p = v)),
])),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
String status = 'Order #1042 is active';
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
Text(status, style: const TextStyle(fontSize: 18)),
const SizedBox(height: 16),
ElevatedButton(onPressed: () async {
final ok = await showDialog<bool>(context: context, builder: (c) => AlertDialog(
title: const Text('Cancel order?'),
content: const Text('You cannot undo this.'),
actions: [TextButton(onPressed: () => Navigator.pop(c, false), child: const Text('No')), TextButton(onPressed: () => Navigator.pop(c, true), child: const Text('Yes, cancel'))],
));
if (ok == true) setState(() => status = 'Order #1042 cancelled');
}, child: const Text('Cancel order')),
])),
);
}
void main() => runApp(const MaterialApp(home: Page()));void main() {
runApp(MaterialApp(home: Builder(builder: (context) => Scaffold(
body: Center(child: FilledButton(
onPressed: () => ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Added to cart: Atta 5 kg'))),
child: const Text('Add to cart'),
)),
))));
}class Todo extends StatefulWidget {
const Todo({super.key});
@override
State<Todo> createState() => _TodoState();
}
class _TodoState extends State<Todo> {
final tasks = ['Buy vegetables', 'Pay electricity bill', 'Call Ravi'];
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Tasks')),
body: ListView(children: [
for (final t in List.of(tasks)) ListTile(
title: Text(t),
trailing: IconButton(icon: const Icon(Icons.check), onPressed: () {
final i = tasks.indexOf(t);
setState(() => tasks.remove(t));
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Done: $t'), action: SnackBarAction(label: 'UNDO', onPressed: () => setState(() => tasks.insert(i, t)))));
}),
),
]),
);
}
void main() => runApp(const MaterialApp(home: Todo()));void main() {
runApp(MaterialApp(home: Builder(builder: (context) => Scaffold(
body: Center(child: FilledButton.icon(
icon: const Icon(Icons.share), label: const Text('Share offer'),
onPressed: () => showModalBottomSheet(context: context, builder: (c) => Padding(padding: const EdgeInsets.all(16), child: Column(mainAxisSize: MainAxisSize.min, children: [
const Text('Share this offer', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
const SizedBox(height: 8),
ListTile(leading: const Icon(Icons.chat), title: const Text('WhatsApp'), onTap: () => Navigator.pop(c)),
ListTile(leading: const Icon(Icons.sms), title: const Text('SMS'), onTap: () => Navigator.pop(c)),
]))),
)),
))));
}void main() {
runApp(const MaterialApp(home: Scaffold(
body: Padding(padding: EdgeInsets.all(24), child: Column(mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [
Text('Flutter course 62%'),
SizedBox(height: 6),
LinearProgressIndicator(value: 0.62, minHeight: 10),
SizedBox(height: 28),
Text('Uploading photo...'),
SizedBox(height: 6),
LinearProgressIndicator(),
SizedBox(height: 28),
Center(child: CircularProgressIndicator(value: 0.75, strokeWidth: 8)),
])),
)));
}class Save extends StatefulWidget {
const Save({super.key});
@override
State<Save> createState() => _SaveState();
}
class _SaveState extends State<Save> {
bool loading = false;
Future<void> save() async {
setState(() => loading = true);
await Future.delayed(const Duration(seconds: 2));
if (!mounted) return;
setState(() => loading = false);
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Profile saved')));
}
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: FilledButton(
onPressed: loading ? null : save,
child: loading ? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white)) : const Text('Save profile'),
)),
);
}
void main() => runApp(const MaterialApp(home: Save()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
final items = ['Rice', 'Dal'];
Future<void> add() async {
final c = TextEditingController();
final name = await showDialog<String>(context: context, builder: (ctx) => AlertDialog(
title: const Text('Add item'),
content: TextField(controller: c, autofocus: true, decoration: const InputDecoration(hintText: 'Item name')),
actions: [TextButton(onPressed: () => Navigator.pop(ctx), child: const Text('Cancel')), FilledButton(onPressed: () => Navigator.pop(ctx, c.text), child: const Text('Add'))],
));
if (name != null && name.isNotEmpty) setState(() => items.add(name));
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Grocery list')),
body: ListView(children: [for (final i in items) ListTile(title: Text(i))]),
floatingActionButton: FloatingActionButton(onPressed: add, child: const Icon(Icons.add)),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
String lang = 'English';
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Settings'), actions: [
Tooltip(message: 'Change language', child: IconButton(icon: const Icon(Icons.language), onPressed: () async {
final l = await showDialog<String>(context: context, builder: (c) => SimpleDialog(title: const Text('Choose language'), children: [
for (final x in ['English', 'Hindi', 'Santhali', 'Bhojpuri']) SimpleDialogOption(onPressed: () => Navigator.pop(c, x), child: Text(x)),
]));
if (l != null) setState(() => lang = l);
})),
]),
body: Center(child: Text('Language: $lang', style: const TextStyle(fontSize: 24))),
);
}
void main() => runApp(const MaterialApp(home: Page()));import 'dart:async';
class Feed extends StatefulWidget {
const Feed({super.key});
@override
State<Feed> createState() => _FeedState();
}
class _FeedState extends State<Feed> {
bool loading = true, pulse = false;
Timer? t;
@override
void initState() {
super.initState();
t = Timer.periodic(const Duration(milliseconds: 700), (x) => setState(() => pulse = !pulse));
Future.delayed(const Duration(seconds: 3), () { if (mounted) setState(() => loading = false); });
}
@override
void dispose() { t?.cancel(); super.dispose(); }
Widget box(double w, double h) => AnimatedOpacity(
duration: const Duration(milliseconds: 600), opacity: pulse ? 0.4 : 1,
child: Container(width: w, height: h, decoration: BoxDecoration(color: Colors.grey.shade300, borderRadius: BorderRadius.circular(6))),
);
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('News')),
body: ListView(padding: const EdgeInsets.all(16), children: [
for (var i = 0; i < 4; i++) Padding(padding: const EdgeInsets.only(bottom: 18), child: loading
? Row(children: [box(64, 64), const SizedBox(width: 12), Column(crossAxisAlignment: CrossAxisAlignment.start, children: [box(180, 14), const SizedBox(height: 8), box(120, 12)])])
: Row(children: [Container(width: 64, height: 64, color: Colors.teal.shade100, child: const Icon(Icons.article)), const SizedBox(width: 12), Text('Ranchi news headline ${i + 1}')])),
]),
);
}
void main() => runApp(const MaterialApp(home: Feed()));class Pay extends StatefulWidget {
const Pay({super.key});
@override
State<Pay> createState() => _PayState();
}
class _PayState extends State<Pay> {
Future<void> pay() async {
final ok = await showDialog<bool>(context: context, builder: (c) => AlertDialog(
title: const Text('Pay Rs 1,240?'), content: const Text('Via UPI to Kanke Road Kirana'),
actions: [TextButton(onPressed: () => Navigator.pop(c, false), child: const Text('Cancel')), FilledButton(onPressed: () => Navigator.pop(c, true), child: const Text('Pay'))],
));
if (ok != true) return;
showDialog(context: context, barrierDismissible: false, builder: (c) => const AlertDialog(content: Row(children: [CircularProgressIndicator(), SizedBox(width: 20), Text('Processing...')])));
await Future.delayed(const Duration(seconds: 2));
if (!mounted) return;
Navigator.pop(context);
showModalBottomSheet(context: context, builder: (c) => const Padding(padding: EdgeInsets.all(32), child: Column(mainAxisSize: MainAxisSize.min, children: [
Icon(Icons.check_circle, color: Colors.green, size: 64),
SizedBox(height: 12),
Text('Payment successful', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
Text('Transaction ID 8841 2207'),
])));
}
@override
Widget build(BuildContext context) => Scaffold(body: Center(child: FilledButton(onPressed: pay, child: const Text('Pay Rs 1,240'))));
}
void main() => runApp(const MaterialApp(home: Pay()));Confirm and decide; await the result.
Short messages with Undo.
Choose an option near the thumb.
Determinate with value, else endless.
Bool + spinner + disabled button.
Placeholders feel faster than spinners.
Wrap any widget and listen to tap, double tap, long press and drag.
Same as tap, but with the Material ripple effect.
Draggable, DragTarget, Dismissible and ReorderableListView.
Wrap any widget in GestureDetector and give it callbacks: onTap, onDoubleTap, onLongPress. It has no visual effect of its own.
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
String msg = 'Touch the box';
Color c = Colors.indigo;
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
GestureDetector(
onTap: () => setState(() => msg = 'Tap'),
onDoubleTap: () => setState(() { msg = 'Double tap'; c = Colors.orange; }),
onLongPress: () => setState(() { msg = 'Long press'; c = Colors.green; }),
child: Container(width: 160, height: 160, color: c, alignment: Alignment.center, child: const Text('Me', style: TextStyle(color: Colors.white, fontSize: 28))),
),
const SizedBox(height: 20),
Text(msg, style: const TextStyle(fontSize: 20)),
])),
);
}
void main() => runApp(const MaterialApp(home: Page()));InkWell gives the Material splash. It must sit on a Material (Card, Scaffold or Material widget) and paints the ripple on that surface.
void main() {
runApp(MaterialApp(home: Scaffold(
body: Center(child: Card(child: InkWell(
onTap: () {},
borderRadius: BorderRadius.circular(12),
child: const Padding(padding: EdgeInsets.all(28), child: Text('Tap me for ripple', style: TextStyle(fontSize: 20))),
))),
)));
}Wrap each list item in Dismissible with a unique key. Swiping removes it. In onDismissed you must remove the item from your list too.
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
final mails = ['Class timing changed', 'Library book due', 'Milk bill', 'Exam form'];
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Inbox')),
body: ListView(children: [
for (final m in mails) Dismissible(
key: ValueKey(m),
background: Container(color: Colors.red, alignment: Alignment.centerRight, padding: const EdgeInsets.only(right: 20), child: const Icon(Icons.delete, color: Colors.white)),
onDismissed: (d) => setState(() => mails.remove(m)),
child: ListTile(leading: const Icon(Icons.mail), title: Text(m)),
),
]),
);
}
void main() => runApp(const MaterialApp(home: Page()));A Draggable carries data. A DragTarget accepts it with onAcceptWithDetails (or onAccept). While dragging, feedback is what the finger holds.
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
String where = 'Drag the ball into the box';
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
Draggable<String>(
data: 'ball',
feedback: const CircleAvatar(radius: 30, backgroundColor: Colors.orange),
childWhenDragging: const CircleAvatar(radius: 30, backgroundColor: Colors.grey),
child: const CircleAvatar(radius: 30, backgroundColor: Colors.orange),
),
DragTarget<String>(
onAcceptWithDetails: (d) => setState(() => where = 'Dropped: ${d.data}'),
builder: (c, cand, rej) => Container(width: 160, height: 120, color: cand.isNotEmpty ? Colors.green.shade200 : Colors.blue.shade100, alignment: Alignment.center, child: const Text('Drop here')),
),
Text(where),
])),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
int likes = 0;
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: GestureDetector(
onTap: () => setState(() => likes++),
child: Container(padding: const EdgeInsets.all(24), decoration: BoxDecoration(color: Colors.pink.shade50, borderRadius: BorderRadius.circular(16)), child: Column(mainAxisSize: MainAxisSize.min, children: [
const Icon(Icons.favorite, color: Colors.pink, size: 48),
Text('$likes likes', style: const TextStyle(fontSize: 22)),
])),
)),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
final sel = <int>{};
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text(sel.isEmpty ? 'Photos' : '${sel.length} selected')),
body: GridView.count(crossAxisCount: 3, mainAxisSpacing: 6, crossAxisSpacing: 6, padding: const EdgeInsets.all(6), children: [
for (var i = 0; i < 9; i++) GestureDetector(
onLongPress: () => setState(() => sel.contains(i) ? sel.remove(i) : sel.add(i)),
onTap: () { if (sel.isNotEmpty) setState(() => sel.contains(i) ? sel.remove(i) : sel.add(i)); },
child: Container(color: Colors.primaries[i * 2 % 18].shade200, child: sel.contains(i) ? const Icon(Icons.check_circle, color: Colors.white) : null),
),
]),
);
}
void main() => runApp(const MaterialApp(home: Page()));void main() {
runApp(MaterialApp(home: Scaffold(
appBar: AppBar(title: const Text('Courses')),
body: ListView(padding: const EdgeInsets.all(12), children: [
for (final c in ['Flutter', 'Python', 'Excel']) Card(child: InkWell(
onTap: () {},
child: Padding(padding: const EdgeInsets.all(20), child: Row(children: [
const Icon(Icons.school), const SizedBox(width: 16), Text(c, style: const TextStyle(fontSize: 20)), const Spacer(), const Icon(Icons.chevron_right),
])),
)),
]),
)));
}class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
final list = ['Dhurwa bus pass', 'Morabadi gym fee', 'Lalpur rent', 'Doranda tuition'];
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Reminders')),
body: ListView(children: [
for (final x in list) Dismissible(
key: ValueKey(x),
background: Container(color: Colors.red.shade400),
onDismissed: (_) => setState(() => list.remove(x)),
child: ListTile(title: Text(x)),
),
]),
);
}
void main() => runApp(const MaterialApp(home: Page()));void main() {
runApp(const MaterialApp(home: Scaffold(
body: Center(child: Draggable<int>(
data: 1,
feedback: CircleAvatar(radius: 34, backgroundColor: Colors.deepOrange),
childWhenDragging: CircleAvatar(radius: 34, backgroundColor: Colors.grey),
child: CircleAvatar(radius: 34, backgroundColor: Colors.deepOrange, child: Text('Drag', style: TextStyle(color: Colors.white))),
)),
)));
}class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
final list = ['Fee reminder', 'Result declared', 'Holiday notice'];
String last = 'Swipe an item';
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text(last)),
body: ListView(children: [
for (final x in List.of(list)) Dismissible(
key: ValueKey(x),
background: Container(color: Colors.green, alignment: Alignment.centerLeft, padding: const EdgeInsets.only(left: 20), child: const Icon(Icons.archive, color: Colors.white)),
secondaryBackground: Container(color: Colors.red, alignment: Alignment.centerRight, padding: const EdgeInsets.only(right: 20), child: const Icon(Icons.delete, color: Colors.white)),
onDismissed: (dir) => setState(() { list.remove(x); last = dir == DismissDirection.startToEnd ? 'Archived $x' : 'Deleted $x'; }),
child: ListTile(title: Text(x)),
),
]),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
final items = ['Ranchi Lake', 'Rock Garden', 'Jagannath Temple', 'Hundru Falls'];
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Trip order')),
body: ReorderableListView(
onReorder: (o, n) => setState(() {
if (n > o) n -= 1;
final it = items.removeAt(o);
items.insert(n, it);
}),
children: [for (final i in items) ListTile(key: ValueKey(i), title: Text(i), leading: const Icon(Icons.place))],
),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
final files = ['notes.pdf', 'fee.jpg', 'plan.xlsx'];
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Files')),
body: Column(children: [
Expanded(child: ListView(children: [
for (final f in files) Padding(padding: const EdgeInsets.all(8), child: Draggable<String>(
data: f,
feedback: Material(child: Chip(label: Text(f))),
childWhenDragging: Opacity(opacity: 0.3, child: Chip(label: Text(f))),
child: Chip(avatar: const Icon(Icons.insert_drive_file), label: Text(f)),
)),
])),
DragTarget<String>(
onAcceptWithDetails: (d) => setState(() => files.remove(d.data)),
builder: (c, cand, rej) => Container(height: 100, width: double.infinity, color: cand.isNotEmpty ? Colors.red.shade300 : Colors.red.shade50, alignment: Alignment.center, child: const Icon(Icons.delete, size: 40)),
),
]),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Board extends StatefulWidget {
const Board({super.key});
@override
State<Board> createState() => _BoardState();
}
class _BoardState extends State<Board> {
final todo = ['Prepare invoice', 'Call supplier', 'Update stock'];
final done = <String>['Open shop'];
Widget column(String title, List<String> mine, List<String> other, Color c) => Expanded(child: DragTarget<String>(
onAcceptWithDetails: (d) => setState(() { other.remove(d.data); if (!mine.contains(d.data)) mine.add(d.data); }),
builder: (ctx, cand, rej) => Container(margin: const EdgeInsets.all(6), padding: const EdgeInsets.all(8), decoration: BoxDecoration(color: cand.isNotEmpty ? c.withOpacity(0.35) : c.withOpacity(0.12), borderRadius: BorderRadius.circular(12)), child: Column(children: [
Text(title, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16)),
const SizedBox(height: 8),
for (final t in mine) Padding(padding: const EdgeInsets.only(bottom: 6), child: Draggable<String>(
data: t,
feedback: Material(child: Card(child: Padding(padding: const EdgeInsets.all(10), child: Text(t)))),
childWhenDragging: Opacity(opacity: 0.3, child: Card(child: Padding(padding: const EdgeInsets.all(10), child: Text(t)))),
child: Card(child: SizedBox(width: double.infinity, child: Padding(padding: const EdgeInsets.all(10), child: Text(t)))),
)),
])),
));
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Shop tasks')),
body: Row(crossAxisAlignment: CrossAxisAlignment.start, children: [column('To do', todo, done, Colors.orange), column('Done', done, todo, Colors.green)]),
);
}
void main() => runApp(const MaterialApp(home: Board()));class Swipe extends StatefulWidget {
const Swipe({super.key});
@override
State<Swipe> createState() => _SwipeState();
}
class _SwipeState extends State<Swipe> {
final names = ['Anita Kumari', 'Vikash Oraon', 'Pooja Sahu'];
double dx = 0;
String last = 'Swipe left or right';
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text(last)),
body: Center(child: names.isEmpty ? const Text('No more profiles') : GestureDetector(
onPanUpdate: (d) => setState(() => dx += d.delta.dx),
onPanEnd: (d) => setState(() {
if (dx.abs() > 100) { last = (dx > 0 ? 'Shortlisted ' : 'Skipped ') + names.first; names.removeAt(0); }
dx = 0;
}),
child: Transform.translate(offset: Offset(dx, 0), child: Transform.rotate(angle: dx / 1000, child: Card(elevation: 6, child: SizedBox(width: 240, height: 300, child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
const CircleAvatar(radius: 50, child: Icon(Icons.person, size: 50)),
const SizedBox(height: 16),
Text(names.first, style: const TextStyle(fontSize: 22)),
const Text('Candidate - Ranchi'),
]))))),
)),
);
}
void main() => runApp(const MaterialApp(home: Swipe()));Tap, double tap, long press, pan.
Tap with Material ripple.
Swipe away; key + remove from list.
Carries data and feedback.
Accepts a drop and highlights.
onReorder moves the item; adjust the index.
Change a value and Flutter animates to it. Easiest. Use first.
You hold an AnimationController. More power: repeat, reverse, sync.
One widget flies from one screen to the next.
Replace Container with AnimatedContainer, add a duration. Now whenever width, color or radius changes inside setState, Flutter animates between old and new values.
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
bool big = false;
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
AnimatedContainer(
duration: const Duration(milliseconds: 600),
curve: Curves.easeInOut,
width: big ? 220 : 100, height: big ? 220 : 100,
decoration: BoxDecoration(color: big ? Colors.teal : Colors.orange, borderRadius: BorderRadius.circular(big ? 110 : 12)),
),
const SizedBox(height: 24),
FilledButton(onPressed: () => setState(() => big = !big), child: const Text('Animate')),
])),
);
}
void main() => runApp(const MaterialApp(home: Page()));A Curve decides the speed profile. linear is robotic. easeInOut is natural. bounceOut and elasticOut are playful. Pick one and keep it consistent in the app.
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
bool right = false;
Widget lane(String name, Curve c) => Padding(padding: const EdgeInsets.symmetric(vertical: 6), child: Stack(children: [
Container(height: 40, color: Colors.grey.shade200, alignment: Alignment.centerLeft, padding: const EdgeInsets.only(left: 8), child: Text(name, style: const TextStyle(fontSize: 12))),
AnimatedAlign(duration: const Duration(milliseconds: 1200), curve: c, alignment: right ? Alignment.centerRight : Alignment.centerLeft, child: const CircleAvatar(radius: 20, backgroundColor: Colors.indigo)),
]));
@override
Widget build(BuildContext context) => Scaffold(
body: Padding(padding: const EdgeInsets.all(16), child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
lane('linear', Curves.linear), lane('easeInOut', Curves.easeInOut), lane('bounceOut', Curves.bounceOut), lane('elasticOut', Curves.elasticOut),
const SizedBox(height: 16),
FilledButton(onPressed: () => setState(() => right = !right), child: const Text('Go')),
])),
);
}
void main() => runApp(const MaterialApp(home: Page()));A AnimationController counts from 0 to 1 over a duration. It needs a vsync (the state class with SingleTickerProviderStateMixin). Use repeat(), forward() and reverse(). Always dispose it.
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> with SingleTickerProviderStateMixin {
late final AnimationController ctl = AnimationController(vsync: this, duration: const Duration(seconds: 2))..repeat(reverse: true);
@override
void dispose() { ctl.dispose(); super.dispose(); }
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: FadeTransition(opacity: ctl, child: const Icon(Icons.favorite, color: Colors.pink, size: 96))),
);
}
void main() => runApp(const MaterialApp(home: Page()));Give the same tag to a Hero on both screens. When you push the new page, Flutter flies the widget from its old place to its new place.
class Home extends StatelessWidget {
const Home({super.key});
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Gallery')),
body: Center(child: GestureDetector(
onTap: () => Navigator.push(context, MaterialPageRoute(builder: (c) => const Detail())),
child: const Hero(tag: 'pic', child: CircleAvatar(radius: 40, backgroundColor: Colors.teal, child: Icon(Icons.landscape, color: Colors.white, size: 40))),
)),
);
}
class Detail extends StatelessWidget {
const Detail({super.key});
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(),
body: const Center(child: Hero(tag: 'pic', child: CircleAvatar(radius: 120, backgroundColor: Colors.teal, child: Icon(Icons.landscape, color: Colors.white, size: 120)))),
);
}
void main() => runApp(const MaterialApp(home: Home()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
bool show = true;
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
AnimatedOpacity(duration: const Duration(milliseconds: 500), opacity: show ? 1 : 0, child: const Text('Welcome to Ranchi!', style: TextStyle(fontSize: 26))),
const SizedBox(height: 20),
ElevatedButton(onPressed: () => setState(() => show = !show), child: const Text('Fade')),
])),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
double s = 1;
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: AnimatedScale(
scale: s, duration: const Duration(milliseconds: 250), curve: Curves.easeOut,
child: FilledButton(onPressed: () => setState(() => s = s == 1 ? 1.5 : 1), child: const Text('Tap me')),
)),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
bool sale = false;
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
AnimatedDefaultTextStyle(
duration: const Duration(milliseconds: 400),
style: TextStyle(fontSize: sale ? 40 : 22, color: sale ? Colors.red : Colors.black, fontWeight: sale ? FontWeight.bold : FontWeight.normal),
child: const Text('Rs 499'),
),
Switch(value: sale, onChanged: (v) => setState(() => sale = v)),
])),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
int n = 0;
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
AnimatedSwitcher(duration: const Duration(milliseconds: 350), child: Text('$n', key: ValueKey(n), style: const TextStyle(fontSize: 72))),
ElevatedButton(onPressed: () => setState(() => n++), child: const Text('+1')),
])),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
bool open = false;
@override
Widget build(BuildContext context) => Scaffold(
body: Column(children: [
const SizedBox(height: 60),
ClipRect(child: AnimatedSlide(
duration: const Duration(milliseconds: 400), curve: Curves.easeOut,
offset: open ? Offset.zero : const Offset(-1, 0),
child: Container(width: double.infinity, color: Colors.green, padding: const EdgeInsets.all(16), child: const Text('Order placed successfully', style: TextStyle(color: Colors.white, fontSize: 18))),
)),
const SizedBox(height: 20),
FilledButton(onPressed: () => setState(() => open = !open), child: Text(open ? 'Hide' : 'Show')),
]),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Like extends StatefulWidget {
const Like({super.key});
@override
State<Like> createState() => _LikeState();
}
class _LikeState extends State<Like> {
bool liked = false;
int count = 24;
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: Row(mainAxisSize: MainAxisSize.min, children: [
IconButton(
onPressed: () => setState(() { liked = !liked; count += liked ? 1 : -1; }),
icon: AnimatedSwitcher(
duration: const Duration(milliseconds: 250),
transitionBuilder: (child, anim) => ScaleTransition(scale: anim, child: child),
child: Icon(liked ? Icons.favorite : Icons.favorite_border, key: ValueKey(liked), color: liked ? Colors.red : Colors.grey, size: 40),
),
),
Text('$count', style: const TextStyle(fontSize: 24)),
])),
);
}
void main() => runApp(const MaterialApp(home: Like()));class Spin extends StatefulWidget {
const Spin({super.key});
@override
State<Spin> createState() => _SpinState();
}
class _SpinState extends State<Spin> with SingleTickerProviderStateMixin {
late final AnimationController c = AnimationController(vsync: this, duration: const Duration(seconds: 1));
bool on = false;
@override
void dispose() { c.dispose(); super.dispose(); }
@override
Widget build(BuildContext context) => Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
RotationTransition(turns: c, child: const Icon(Icons.sync, size: 80, color: Colors.blue)),
const SizedBox(height: 20),
FilledButton(onPressed: () { setState(() => on = !on); if (on) { c.repeat(); } else { c.stop(); } }, child: Text(on ? 'Stop sync' : 'Start sync')),
])),
);
}
void main() => runApp(const MaterialApp(home: Spin()));class Faq extends StatefulWidget {
const Faq({super.key});
@override
State<Faq> createState() => _FaqState();
}
class _FaqState extends State<Faq> {
bool open = false;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('FAQ')),
body: Padding(padding: const EdgeInsets.all(12), child: Card(child: InkWell(
onTap: () => setState(() => open = !open),
child: Padding(padding: const EdgeInsets.all(16), child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Row(children: [const Expanded(child: Text('How do I get a refund?', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16))), AnimatedRotation(turns: open ? 0.5 : 0, duration: const Duration(milliseconds: 250), child: const Icon(Icons.expand_more))]),
AnimatedCrossFade(
duration: const Duration(milliseconds: 300),
crossFadeState: open ? CrossFadeState.showSecond : CrossFadeState.showFirst,
firstChild: const SizedBox(width: double.infinity),
secondChild: const Padding(padding: EdgeInsets.only(top: 8), child: Text('Go to Orders, tap the order and choose Refund. Money returns to your account in 5 to 7 days.')),
),
])),
))),
);
}
void main() => runApp(const MaterialApp(home: Faq()));class Menu extends StatefulWidget {
const Menu({super.key});
@override
State<Menu> createState() => _MenuState();
}
class _MenuState extends State<Menu> with SingleTickerProviderStateMixin {
late final AnimationController c = AnimationController(vsync: this, duration: const Duration(milliseconds: 1200))..forward();
final items = ['Dhuska', 'Litti Chokha', 'Rugra Sabzi', 'Handia Khichdi', 'Thekua'];
@override
void dispose() { c.dispose(); super.dispose(); }
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Jharkhand Menu'), actions: [IconButton(icon: const Icon(Icons.replay), onPressed: () => c.forward(from: 0))]),
body: ListView(children: [
for (var i = 0; i < items.length; i++) FadeTransition(
opacity: CurvedAnimation(parent: c, curve: Interval(i * 0.15, i * 0.15 + 0.4, curve: Curves.easeOut)),
child: SlideTransition(
position: Tween<Offset>(begin: const Offset(0.3, 0), end: Offset.zero).animate(CurvedAnimation(parent: c, curve: Interval(i * 0.15, i * 0.15 + 0.4, curve: Curves.easeOut))),
child: ListTile(leading: const Icon(Icons.restaurant), title: Text(items[i])),
),
),
]),
);
}
void main() => runApp(const MaterialApp(home: Menu()));class Gallery extends StatelessWidget {
const Gallery({super.key});
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Ranchi photos')),
body: GridView.count(crossAxisCount: 2, padding: const EdgeInsets.all(8), mainAxisSpacing: 8, crossAxisSpacing: 8, children: [
for (var i = 0; i < 4; i++) GestureDetector(
onTap: () => Navigator.push(context, MaterialPageRoute(builder: (c) => Big(i))),
child: Hero(tag: 'p$i', child: Container(color: Colors.primaries[i * 4].shade300, child: const Icon(Icons.photo, size: 48, color: Colors.white))),
),
]),
);
}
class Big extends StatelessWidget {
final int i;
const Big(this.i, {super.key});
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text('Photo ${i + 1}')),
body: Center(child: Hero(tag: 'p$i', child: Container(width: 280, height: 280, color: Colors.primaries[i * 4].shade300, child: const Icon(Icons.photo, size: 120, color: Colors.white)))),
);
}
void main() => runApp(const MaterialApp(home: Gallery()));Change value; Flutter animates.
easeInOut feels natural.
Explicit, needs vsync and dispose.
Fade, Scale, Slide, Rotation.
Needs a new key per child.
Same tag on two screens.