Skip to content

Instantly share code, notes, and snippets.

@chimon2000
Last active November 3, 2020 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chimon2000/4b92f854775f1c15f8ba41476e3cd3dd to your computer and use it in GitHub Desktop.
Save chimon2000/4b92f854775f1c15f8ba41476e3cd3dd to your computer and use it in GitHub Desktop.
class RiverpodPage extends StatefulWidget {
const RiverpodPage({Key key}) : super(key: key);
@override
_RiverpodPageState createState() => _RiverpodPageState();
}
class _RiverpodPageState extends State<RiverpodPage> {
TextEditingController _controller;
@override
void initState() {
super.initState();
_controller = TextEditingController();
}
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('My notes app')),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
FlatButton(
onPressed: () {
// Get a reference of the NotesController
// and add a note.
context.read(notesProvider).addNote();
_controller.clear();
},
child: Text('Create Note')),
TextField(
controller: _controller,
// Get a reference of the NotesController
// and update the input value.
onChanged: (value) =>
context.read(notesProvider).updateInput(value),
decoration: InputDecoration.collapsed(hintText: 'Add a note'),
),
Divider(),
Expanded(
child: Consumer(
builder: (context, watch, child) {
// Subscribe to the NotesController's state
= var state = watch(notesProvider.state);
return 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