I am struggling to put the block pattern in my Flutter App. I am using this library to make it a little bit simpler for me: https://felangel.github.io/bloc/#/
I have one main Question. How can I make my Bloc globally accessible without passing it down again and again?
Here is my Code:
main.dart:
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'db_bloc.dart';
import 'NotesPage.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
final DatabaseBloc _dbNoteBloc = DatabaseBloc();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: BlocProvider<DatabaseBloc>(
bloc: _dbNoteBloc,
child: NotesPage(),
),
);
}
@override
void dispose() {
_dbNoteBloc.dispose();
super.dispose();
}
}
NotesPage:
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'db_bloc.dart';
import 'note_model.dart';
class NotesPage extends StatefulWidget {
_NotesPageState createState() => _NotesPageState();
}
class _NotesPageState extends State<NotesPage> {
@override
Widget build(BuildContext context) {
final DatabaseBloc _dbNoteBloc = BlocProvider.of<DatabaseBloc>(context);
return BlocBuilder<NoteDbEvent, List<Note>>(
bloc: _dbNoteBloc,
builder: (BuildContext context, List<Note> state) {
return Scaffold(
appBar: AppBar(
title: Text("bla"),
actions: <Widget>[
// action button
IconButton(
icon: Icon(Icons.refresh),
onPressed: () => _dbNoteBloc
.dispatch(NoteDbEvent(type: NoteDbEventType.GetAll)),
), // action button
],
),
body: ListView.builder(
itemCount: state.length,
itemBuilder: (BuildContext context, int index) {
return Column(
children: <Widget>[
ListTile(
title: Text('${state[index].title}'),
onLongPress: () => showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Löschen"),
content: Text(
"Möchtest du diese Notiz wirklich löschen?"),
actions: <Widget>[
FlatButton(
child: Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text("Delete"),
onPressed: () {
_dbNoteBloc.dispatch(NoteDbEvent(
value: state[index],
type: NoteDbEventType.Delete));
Navigator.of(context).pop();
},
),
],
);
},
),
),
Divider(
height: 0,
),
],
);
},
),
);
},
);
}
}
As you can see I also have the show Dialog Code inside of the widget code and I would love to source it out but until now the show Dialog Code was not able to access the _dbNoteBloc if it is not inside of the Bloc Builder.
Thank you for every help.
PS: If someone needs the Bloc Code here it is:
import 'package:bloc/bloc.dart';
import "note_model.dart";
import 'db.dart';
enum NoteDbEventType { Insert, Delete, Update, GetAll }
class NoteDbEvent {
NoteDbEventType type;
Note value;
NoteDbEvent({this.type, this.value});
}
class DatabaseBloc extends Bloc<NoteDbEvent, List<Note>> {
final db = DBProvider();
@override
List<Note> get initialState => [];
@override
Stream<List<Note>> mapEventToState(List<Note> currentState, NoteDbEvent event) async* {
switch (event.type) {
case NoteDbEventType.Delete:
db.deleteNote(event.value.id);
currentState.removeWhere((item) => item.id == event.value.id);
yield List.from(currentState);
break;
case NoteDbEventType.Insert:
int id = await db.newNote(event.value);
event.value.id = id;
currentState.add(event.value);
yield List.from(currentState);
break;
case NoteDbEventType.Update:
yield currentState;
break;
case NoteDbEventType.GetAll:
var list = await db.getAllNotes();
yield currentState = list;
break;
}
}
}