Last active
November 3, 2020 04:45
-
-
Save chimon2000/ad89d1656f4ce8164eafc7f086182a81 to your computer and use it in GitHub Desktop.
state_example: binder 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 BinderPage extends StatefulWidget { | |
const BinderPage({Key key}) : super(key: key); | |
@override | |
_BinderPageState createState() => _BinderPageState(); | |
} | |
class _BinderPageState extends State<BinderPage> { | |
TextEditingController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = TextEditingController(); | |
} | |
@override | |
void dispose() { | |
_controller?.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
// Subscribe to NotesState | |
final state = context.watch(notesRef); | |
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 NotesViewLogic | |
// and add a note. | |
context.use(notesViewLogicRef).addNote(); | |
_controller.clear(); | |
}, | |
child: Text('Create Note')), | |
TextField( | |
controller: _controller, | |
// Get a reference of NotesViewLogic | |
// and update the input value. | |
onChanged: (value) => | |
context.use(notesViewLogicRef).updateInput(value), | |
decoration: InputDecoration.collapsed(hintText: 'Add a note'), | |
), | |
Divider(), | |
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