我正在努力将块模式放入我的 Flutter 应用程序中。我使用这个库是为了让它对我来说更简单: https: //felangel.github.io/bloc/#/
\n\n我有一个主要问题。如何让我的 Bloc 在全球范围内都可访问,而不需要一次又一次地传递下去?
\n\n这是我的代码:
\n\n主要.dart:
\n\nimport \'package:flutter/material.dart\';\nimport \'package:flutter_bloc/flutter_bloc.dart\';\n\nimport \'db_bloc.dart\';\nimport \'NotesPage.dart\';\n\nvoid main() => runApp(MyApp());\n\nclass MyApp extends StatefulWidget {\n @override\n State<StatefulWidget> createState() => MyAppState();\n}\n\nclass MyAppState extends State<MyApp> {\n final DatabaseBloc _dbNoteBloc = DatabaseBloc();\n\n @override\n Widget build(BuildContext context) {\n return MaterialApp(\n title: \'Flutter Demo\',\n home: BlocProvider<DatabaseBloc>(\n bloc: _dbNoteBloc,\n child: NotesPage(),\n ),\n );\n }\n\n @override\n void dispose() {\n _dbNoteBloc.dispose();\n super.dispose();\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n注释页:
\n\nimport \'package:flutter/material.dart\';\nimport \'package:flutter_bloc/flutter_bloc.dart\';\n\nimport \'db_bloc.dart\';\nimport \'note_model.dart\';\n\nclass NotesPage extends StatefulWidget {\n _NotesPageState createState() => _NotesPageState();\n}\n\nclass _NotesPageState extends State<NotesPage> {\n @override\n Widget build(BuildContext context) {\n final DatabaseBloc _dbNoteBloc = BlocProvider.of<DatabaseBloc>(context);\n\n return BlocBuilder<NoteDbEvent, List<Note>>(\n bloc: _dbNoteBloc,\n builder: (BuildContext context, List<Note> state) {\n return Scaffold(\n appBar: AppBar(\n title: Text("bla"),\n actions: <Widget>[\n // action button\n IconButton(\n icon: Icon(Icons.refresh),\n onPressed: () => _dbNoteBloc\n .dispatch(NoteDbEvent(type: NoteDbEventType.GetAll)),\n ), // action button\n ],\n ),\n body: ListView.builder(\n itemCount: state.length,\n itemBuilder: (BuildContext context, int index) {\n return Column(\n children: <Widget>[\n ListTile(\n title: Text(\'${state[index].title}\'),\n onLongPress: () => showDialog(\n context: context,\n builder: (BuildContext context) {\n return AlertDialog(\n title: Text("L\xc3\xb6schen"),\n content: Text(\n "M\xc3\xb6chtest du diese Notiz wirklich l\xc3\xb6schen?"),\n actions: <Widget>[\n FlatButton(\n child: Text("Close"),\n onPressed: () {\n Navigator.of(context).pop();\n },\n ),\n FlatButton(\n child: Text("Delete"),\n onPressed: () {\n _dbNoteBloc.dispatch(NoteDbEvent(\n value: state[index],\n type: NoteDbEventType.Delete));\n Navigator.of(context).pop();\n },\n ),\n ],\n );\n },\n ),\n ),\n Divider(\n height: 0,\n ),\n ],\n );\n },\n ),\n );\n },\n );\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n正如您所看到的,我在小部件代码中也有显示对话框代码,我很想将其获取,但到目前为止,如果 _dbNoteBloc 不在 Bloc Builder 内部,则显示对话框代码无法访问 _dbNoteBloc。
\n\n感谢您的每一次帮助。
\n\nPS:如果有人需要这里的块代码,它是:
\n\nimport \'package:bloc/bloc.dart\';\nimport "note_model.dart";\nimport \'db.dart\';\n\nenum NoteDbEventType { Insert, Delete, Update, GetAll }\n\nclass NoteDbEvent {\n NoteDbEventType type;\n Note value;\n NoteDbEvent({this.type, this.value});\n}\n\nclass DatabaseBloc extends Bloc<NoteDbEvent, List<Note>> {\n final db = DBProvider();\n\n @override\n List<Note> get initialState => [];\n\n @override\n Stream<List<Note>> mapEventToState(List<Note> currentState, NoteDbEvent event) async* {\n switch (event.type) {\n case NoteDbEventType.Delete:\n db.deleteNote(event.value.id);\n currentState.removeWhere((item) => item.id == event.value.id);\n yield List.from(currentState);\n break;\n case NoteDbEventType.Insert:\n int id = await db.newNote(event.value);\n event.value.id = id;\n currentState.add(event.value);\n yield List.from(currentState);\n break;\n case NoteDbEventType.Update:\n yield currentState;\n break;\n case NoteDbEventType.GetAll:\n var list = await db.getAllNotes();\n yield currentState = list;\n break;\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
9293 次 |
| 最近记录: |