Skip to content

Instantly share code, notes, and snippets.

@viveky259259
Created October 16, 2019 11:43
Show Gist options
  • Save viveky259259/022d5cf539eabcdd660368b08ee31ad8 to your computer and use it in GitHub Desktop.
Save viveky259259/022d5cf539eabcdd660368b08ee31ad8 to your computer and use it in GitHub Desktop.
BLoC Pattern for flutter -Part 3
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_for_people/bloc/notes/note.bloc.dart';
import 'package:flutter_for_people/bloc/notes/note.event.dart';
import 'package:flutter_for_people/bloc/notes/note.model.dart';
import 'package:flutter_for_people/bloc/notes/note.state.dart';
class NotesAddUi extends StatefulWidget {
@override
_NotesAddUiState createState() => _NotesAddUiState();
}
class _NotesAddUiState extends State<NotesAddUi> {
TextEditingController titleController;
TextEditingController descriptionController;
NoteBloc noteBloc;
@override
initState() {
super.initState();
titleController = TextEditingController();
descriptionController = TextEditingController();
}
void addNote() {
noteBloc.dispatch(AddNoteEvent(
NoteModel(titleController.text, descriptionController.text)));
}
@override
Widget build(BuildContext context) {
noteBloc = BlocProvider.of<NoteBloc>(context);
return Scaffold(
appBar: AppBar(
title: Text('Add Note'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
hintText: "Enter title",
alignLabelWithHint: true,
),
controller: titleController,
),
TextFormField(
decoration: InputDecoration(
hintText: "Enter description",
alignLabelWithHint: true,
),
controller: descriptionController,
),
SizedBox(
height: 16,
),
BlocBuilder<NoteBloc, NoteState>(
bloc: noteBloc,
builder: (BuildContext context, NoteState state) {
if (state is AddingNoteInProgressState) {
return Center(child: CircularProgressIndicator());
} else
return RaisedButton(
onPressed: () {
addNote();
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Add'),
),
);
}),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment