Skip to content

Instantly share code, notes, and snippets.

@felangel
Last active June 18, 2020 23:13
Show Gist options
  • Save felangel/43e65c4687c4c96dbf22de6bcb3dfd32 to your computer and use it in GitHub Desktop.
Save felangel/43e65c4687c4c96dbf22de6bcb3dfd32 to your computer and use it in GitHub Desktop.
StatefulBuilder Demo
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: MyChild(),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() {}),
child: Icon(Icons.refresh),
),
);
}
}
class MyChild extends StatelessWidget {
@override
Widget build(BuildContext context) {
var i = 0;
return StatefulBuilder(builder: (context, setState) {
return Center(
child: RaisedButton(
child: Text('$i'),
onPressed: () => setState(() => i++),
),
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment