mirror of https://github.com/AMT-Cheif/drift.git
example: Use provider package
This commit is contained in:
parent
f4c05f44c7
commit
91e4229049
|
@ -1,31 +1,15 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:moor_example/bloc.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'widgets/homescreen.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
@override
|
||||
MyAppState createState() {
|
||||
return MyAppState();
|
||||
}
|
||||
}
|
||||
|
||||
// We use this widget to set up the material app and provide an InheritedWidget that
|
||||
// the rest of this simple app can then use to access the database
|
||||
class MyAppState extends State<MyApp> {
|
||||
TodoAppBloc bloc;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
bloc = TodoAppBloc();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
bloc: bloc,
|
||||
return Provider(
|
||||
create: (_) => TodoAppBloc(),
|
||||
child: MaterialApp(
|
||||
title: 'moor Demo',
|
||||
theme: ThemeData(
|
||||
|
@ -42,17 +26,3 @@ class MyAppState extends State<MyApp> {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BlocProvider extends InheritedWidget {
|
||||
final TodoAppBloc bloc;
|
||||
|
||||
BlocProvider({@required this.bloc, Widget child}) : super(child: child);
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(BlocProvider oldWidget) {
|
||||
return oldWidget.bloc != bloc;
|
||||
}
|
||||
|
||||
static TodoAppBloc provideBloc(BuildContext ctx) =>
|
||||
(ctx.inheritFromWidgetOfExactType(BlocProvider) as BlocProvider).bloc;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:moor_example/main.dart';
|
||||
import 'package:moor_example/bloc.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class AddCategoryDialog extends StatefulWidget {
|
||||
@override
|
||||
|
@ -49,7 +50,7 @@ class _AddCategoryDialogState extends State<AddCategoryDialog> {
|
|||
|
||||
void _addEntry() {
|
||||
if (_controller.text.isNotEmpty) {
|
||||
BlocProvider.provideBloc(context).addCategory(_controller.text);
|
||||
Provider.of<TodoAppBloc>(context).addCategory(_controller.text);
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:moor_example/bloc.dart';
|
||||
import 'package:moor_example/main.dart';
|
||||
import 'package:moor_example/widgets/add_category_dialog.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class CategoriesDrawer extends StatelessWidget {
|
||||
@override
|
||||
|
@ -22,7 +22,7 @@ class CategoriesDrawer extends StatelessWidget {
|
|||
),
|
||||
Flexible(
|
||||
child: StreamBuilder<List<CategoryWithActiveInfo>>(
|
||||
stream: BlocProvider.provideBloc(context).categories,
|
||||
stream: Provider.of<TodoAppBloc>(context).categories,
|
||||
builder: (context, snapshot) {
|
||||
final categories = snapshot.data ?? <CategoryWithActiveInfo>[];
|
||||
|
||||
|
@ -70,7 +70,7 @@ class _CategoryDrawerEntry extends StatelessWidget {
|
|||
}
|
||||
|
||||
final isActive = entry.isActive;
|
||||
final bloc = BlocProvider.provideBloc(context);
|
||||
final bloc = Provider.of<TodoAppBloc>(context);
|
||||
|
||||
final rowContent = [
|
||||
Text(
|
||||
|
|
|
@ -2,9 +2,9 @@ import 'package:flutter/material.dart' hide Column;
|
|||
import 'package:flutter/widgets.dart' as f show Column;
|
||||
import 'package:moor_example/bloc.dart';
|
||||
import 'package:moor_example/database/database.dart';
|
||||
import 'package:moor_example/main.dart';
|
||||
import 'package:moor_example/widgets/categories_drawer.dart';
|
||||
import 'package:moor_example/widgets/todo_card.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
|
||||
|
@ -20,7 +20,7 @@ class HomeScreenState extends State<HomeScreen> {
|
|||
// we only use this to reset the input field at the bottom when a entry has been added
|
||||
final TextEditingController controller = TextEditingController();
|
||||
|
||||
TodoAppBloc get bloc => BlocProvider.provideBloc(context);
|
||||
TodoAppBloc get bloc => Provider.of<TodoAppBloc>(context);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:moor_example/bloc.dart';
|
||||
import 'package:moor_example/database/database.dart';
|
||||
import 'package:moor_example/main.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:moor_example/widgets/todo_edit_dialog.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
final DateFormat _format = DateFormat.yMMMd();
|
||||
|
||||
|
@ -65,7 +66,7 @@ class TodoCard extends StatelessWidget {
|
|||
onPressed: () {
|
||||
// We delete the entry here. Again, notice how we don't have to call setState() or
|
||||
// inform the parent widget. The animated list will take care of this automatically.
|
||||
BlocProvider.provideBloc(context).deleteEntry(entry);
|
||||
Provider.of<TodoAppBloc>(context).deleteEntry(entry);
|
||||
},
|
||||
)
|
||||
],
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:moor_example/bloc.dart';
|
||||
import 'package:moor_example/database/database.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:moor_example/main.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
final _dateFormat = DateFormat.yMMMd();
|
||||
|
||||
|
@ -95,7 +96,7 @@ class _TodoEditDialogState extends State<TodoEditDialog> {
|
|||
targetDate: _dueDate,
|
||||
);
|
||||
|
||||
BlocProvider.provideBloc(context).updateEntry(entry);
|
||||
Provider.of<TodoAppBloc>(context).updateEntry(entry);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
|
|
|
@ -9,8 +9,8 @@ environment:
|
|||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
intl:
|
||||
cupertino_icons: ^0.1.2
|
||||
provider: ^3.2.0
|
||||
intl: ^0.16.0
|
||||
rxdart: 0.21.0
|
||||
moor_flutter: ^2.0.0
|
||||
|
||||
|
|
Loading…
Reference in New Issue