Skip to content

Instantly share code, notes, and snippets.

Last active November 3, 2020 05:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
state_examples bloc page
class BlocPage extends StatefulWidget {
const BlocPage({Key key}) : super(key: key);
_BlocPageState createState() => _BlocPageState();
class _BlocPageState extends State<BlocPage> {
TextEditingController _controller;
void initState() {
_controller = TextEditingController();
void dispose() {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('My notes app: Bloc')),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
onPressed: () {
// Get a reference of the NotesCubit
// and add a note.
child: Text('Create Note')),
controller: _controller,
// Get a reference of the NotesCubit
// and update the input value.
onChanged: (value) =>
decoration: InputDecoration.collapsed(hintText: 'Add a note'),
// Subscribe to the NotesCubit's state
BlocBuilder<NotesCubit, NotesState>(
builder: (context, state) => Expanded(
child: ListView.builder(
itemBuilder: (context, index) =>
Note(text: state.notes[index]),
itemCount: state.notes.length,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment