Last active
November 3, 2020 05:39
-
-
Save chimon2000/c1db7acaba9d28c05509d194e2b7d497 to your computer and use it in GitHub Desktop.
state_examples bloc 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 BlocPage extends StatefulWidget { | |
const BlocPage({Key key}) : super(key: key); | |
@override | |
_BlocPageState createState() => _BlocPageState(); | |
} | |
class _BlocPageState extends State<BlocPage> { | |
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: Bloc')), | |
body: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Column( | |
children: [ | |
FlatButton( | |
onPressed: () { | |
// Get a reference of the NotesCubit | |
// and add a note. | |
context.bloc<NotesCubit>().addNote(); | |
_controller.clear(); | |
}, | |
child: Text('Create Note')), | |
TextField( | |
controller: _controller, | |
// Get a reference of the NotesCubit | |
// and update the input value. | |
onChanged: (value) => | |
context.bloc<NotesCubit>().updateInput(value), | |
decoration: InputDecoration.collapsed(hintText: 'Add a note'), | |
), | |
Divider(), | |
// 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