Last active
November 3, 2020 05:48
-
-
Save chimon2000/7982cc4fd86e88cd46328590bc5b26b5 to your computer and use it in GitHub Desktop.
state_example: mobx page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MobxPage extends StatefulWidget { | |
const MobxPage({Key key}) : super(key: key); | |
@override | |
_MobxPageState createState() => _MobxPageState(); | |
} | |
class _MobxPageState extends State<MobxPage> { | |
var _notesStore = Notes(); | |
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 addNote action | |
// to add a note. | |
_notesStore.addNote(); | |
_controller.clear(); | |
}, | |
child: Text('Create Note')), | |
TextField( | |
controller: _controller, | |
// Execute updateInput action | |
// to update the input value. | |
onChanged: _notesStore.updateInput, | |
decoration: InputDecoration.collapsed(hintText: 'Add a note'), | |
), | |
Divider(), | |
// Use Observer to subscribe | |
// to updates to the NotesStore. | |
Observer( | |
builder: (_) => Expanded( | |
child: ListView.builder( | |
itemBuilder: (context, index) => | |
Note(text: _notesStore.notes[index]), | |
itemCount: _notesStore.notes.length, | |
), | |
), | |
) | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment