Skip to content

Instantly share code, notes, and snippets.

@chimon2000
Last active November 3, 2020 05: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/793a7a813f8afb3ce97d2a3bd33dd8b8 to your computer and use it in GitHub Desktop.
Save chimon2000/793a7a813f8afb3ce97d2a3bd33dd8b8 to your computer and use it in GitHub Desktop.
state_examples: flutter command page
class FlutterCommandPage extends StatefulWidget {
const FlutterCommandPage({Key key}) : super(key: key);
@override
_FlutterCommandPageState createState() => _FlutterCommandPageState();
}
class _FlutterCommandPageState extends State<FlutterCommandPage> {
final _notesViewModel = NotesViewModel();
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: () {
// Execute updateNotesCommand
// to add a note.
_notesViewModel.updateNotesCommand
.execute(_notesViewModel.inputChangedCommand.value);
_controller.clear();
},
child: Text('Create Note')),
TextField(
controller: _controller,
// Execute inputChangedCommand
// to update the input value.
onChanged: _notesViewModel.inputChangedCommand,
decoration: InputDecoration.collapsed(hintText: 'Add a note'),
),
Divider(),
// Use ValueListenableBuilder
// to subscribe to state.
ValueListenableBuilder<NotesState>(
valueListenable: _notesViewModel.updateNotesCommand,
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