feat(bloc): add a lot of docs, fix onWrap, and add repo provider
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:bloc_helper_example/counter/repository/counter_repository.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
@@ -21,14 +22,16 @@ part 'counter_event.dart';
|
||||
part 'counter_state.dart';
|
||||
|
||||
class CounterBloc extends Bloc<CounterEvent, CounterState> {
|
||||
CounterBloc() : super(const CounterInitial()) {
|
||||
final CounterRepository counterRepository;
|
||||
|
||||
CounterBloc(this.counterRepository) : super(const CounterInitial()) {
|
||||
|
||||
on<CounterIncrement>((event, emit) {
|
||||
emit(CounterModified(state.count + 1));
|
||||
emit(CounterModified(counterRepository.increment(state.count)));
|
||||
});
|
||||
|
||||
on<CounterDecrement>((event, emit) {
|
||||
emit(CounterModified(state.count - 1));
|
||||
emit(CounterModified(counterRepository.decrement(state.count)));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:bloc_helper_example/counter/bloc/counter_bloc.dart';
|
||||
import 'package:bloc_helper_example/counter/repository/counter_repository.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wyatt_bloc_helper/wyatt_bloc_helper.dart';
|
||||
|
||||
@@ -23,7 +24,7 @@ class CounterBlocPage
|
||||
const CounterBlocPage({super.key});
|
||||
|
||||
@override
|
||||
CounterBloc create(BuildContext context) => CounterBloc();
|
||||
CounterBloc create(BuildContext context) => CounterBloc(repo<CounterRepository>(context));
|
||||
|
||||
@override
|
||||
Widget onBuild(BuildContext context, CounterState state) {
|
||||
|
||||
@@ -35,14 +35,16 @@ class CounterConsumerPage
|
||||
children: <Widget>[
|
||||
FloatingActionButton(
|
||||
heroTag: null,
|
||||
key: const Key('counterConsumerView_increment_floatingActionButton'),
|
||||
key:
|
||||
const Key('counterConsumerView_increment_floatingActionButton'),
|
||||
child: const Icon(Icons.add),
|
||||
onPressed: () => bloc(context).increment(),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
FloatingActionButton(
|
||||
heroTag: null,
|
||||
key: const Key('counterConsumerView_decrement_floatingActionButton'),
|
||||
key:
|
||||
const Key('counterConsumerView_decrement_floatingActionButton'),
|
||||
child: const Icon(Icons.remove),
|
||||
onPressed: () => bloc(context).decrement(),
|
||||
),
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:bloc_helper_example/counter/cubit/counter_cubit.dart';
|
||||
import 'package:bloc_helper_example/counter/repository/counter_repository.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wyatt_bloc_helper/wyatt_bloc_helper.dart';
|
||||
|
||||
@@ -23,7 +24,7 @@ class CounterCubitPage
|
||||
const CounterCubitPage({super.key});
|
||||
|
||||
@override
|
||||
CounterCubit create(BuildContext context) => CounterCubit();
|
||||
CounterCubit create(BuildContext context) => CounterCubit(repo<CounterRepository>(context));
|
||||
|
||||
@override
|
||||
Widget onBuild(BuildContext context, CounterState state) {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:bloc_helper_example/counter/bloc/counter_bloc.dart';
|
||||
import 'package:bloc_helper_example/counter/repository/counter_repository.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_bloc_helper/wyatt_bloc_helper.dart';
|
||||
@@ -24,7 +25,11 @@ class CounterProviderPage
|
||||
const CounterProviderPage({super.key});
|
||||
|
||||
@override
|
||||
Widget buildChild(BuildContext context) {
|
||||
CounterBloc create(BuildContext context) =>
|
||||
CounterBloc(repo<CounterRepository>(context));
|
||||
|
||||
@override
|
||||
Widget builder(BuildContext context) {
|
||||
final textTheme = Theme.of(context).textTheme;
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Counter with Provider')),
|
||||
@@ -39,14 +44,16 @@ class CounterProviderPage
|
||||
children: <Widget>[
|
||||
FloatingActionButton(
|
||||
heroTag: null,
|
||||
key: const Key('counterProviderView_increment_floatingActionButton'),
|
||||
key:
|
||||
const Key('counterProviderView_increment_floatingActionButton'),
|
||||
child: const Icon(Icons.add),
|
||||
onPressed: () => add(context, CounterIncrement()),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
FloatingActionButton(
|
||||
heroTag: null,
|
||||
key: const Key('counterProviderView_decrement_floatingActionButton'),
|
||||
key:
|
||||
const Key('counterProviderView_decrement_floatingActionButton'),
|
||||
child: const Icon(Icons.remove),
|
||||
onPressed: () => add(context, CounterDecrement()),
|
||||
),
|
||||
@@ -54,7 +61,4 @@ class CounterProviderPage
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
CounterBloc create(BuildContext context) => CounterBloc();
|
||||
}
|
||||
|
||||
@@ -1,32 +1,35 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:bloc_helper_example/counter/repository/counter_repository.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
part 'counter_state.dart';
|
||||
|
||||
class CounterCubit extends Cubit<CounterState> {
|
||||
CounterCubit() : super(const CounterInitial());
|
||||
final CounterRepository counterRepository;
|
||||
|
||||
CounterCubit(this.counterRepository) : super(const CounterInitial());
|
||||
|
||||
void increment() {
|
||||
emit(CounterModified(state.count + 1));
|
||||
emit(CounterModified(counterRepository.increment(state.count)));
|
||||
}
|
||||
|
||||
void decrement() {
|
||||
emit(CounterModified(state.count - 1));
|
||||
emit(CounterModified(counterRepository.decrement(state.count)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
class CounterRepository {
|
||||
int increment(int before) {
|
||||
final after = before + 1;
|
||||
return after;
|
||||
}
|
||||
|
||||
int decrement(int before) {
|
||||
final after = before - 1;
|
||||
return after;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:bloc_helper_example/counter/repository/counter_repository.dart';
|
||||
import 'package:bloc_helper_example/main_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wyatt_bloc_helper/wyatt_bloc_helper.dart';
|
||||
|
||||
class CounterRepositoryProviderPage
|
||||
extends RepositoryProviderScreen<CounterRepository> {
|
||||
const CounterRepositoryProviderPage({super.key});
|
||||
|
||||
@override
|
||||
CounterRepository create(BuildContext context) => CounterRepository();
|
||||
|
||||
@override
|
||||
Widget builder(BuildContext context) => MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: const MainPage(),
|
||||
);
|
||||
}
|
||||
@@ -14,77 +14,12 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:bloc_helper_example/counter/counter_bloc_page.dart';
|
||||
import 'package:bloc_helper_example/counter/counter_consumer_page.dart';
|
||||
import 'package:bloc_helper_example/counter/counter_cubit_page.dart';
|
||||
import 'package:bloc_helper_example/counter/counter_provider_page.dart';
|
||||
import 'package:bloc_helper_example/counter/cubit/counter_cubit.dart';
|
||||
import 'package:bloc_helper_example/counter_observer.dart';
|
||||
import 'package:bloc_helper_example/counter_repository_provider_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
void main() {
|
||||
BlocOverrides.runZoned(() => runApp(const MyApp()),
|
||||
BlocOverrides.runZoned(() => runApp(const CounterRepositoryProviderPage()),
|
||||
blocObserver: CounterObserver());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
// This widget is the root of your application.
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: const MainPage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MainPage extends StatelessWidget {
|
||||
const MainPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Main Page'),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
ElevatedButton(
|
||||
child: const Text('Counter with BlocScreen'),
|
||||
onPressed: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => const CounterBlocPage())),
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text('Counter with CubitScreen'),
|
||||
onPressed: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => const CounterCubitPage())),
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text('Counter with BlocProviderScreen'),
|
||||
onPressed: () => Navigator.of(context).push(MaterialPageRoute(
|
||||
builder: (_) => const CounterProviderPage())),
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text('Counter with BlocConsumerScreen'),
|
||||
onPressed: () => Navigator.of(context)
|
||||
.push(MaterialPageRoute(builder: (context) {
|
||||
return BlocProvider<CounterCubit>(
|
||||
create: (context) => CounterCubit(),
|
||||
child: const CounterConsumerPage(),
|
||||
);
|
||||
})),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:bloc_helper_example/counter/counter_bloc_page.dart';
|
||||
import 'package:bloc_helper_example/counter/counter_consumer_page.dart';
|
||||
import 'package:bloc_helper_example/counter/counter_cubit_page.dart';
|
||||
import 'package:bloc_helper_example/counter/counter_provider_page.dart';
|
||||
import 'package:bloc_helper_example/counter/cubit/counter_cubit.dart';
|
||||
import 'package:bloc_helper_example/counter/repository/counter_repository.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class MainPage extends StatelessWidget {
|
||||
const MainPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Main Page'),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
ElevatedButton(
|
||||
child: const Text('Counter with BlocScreen'),
|
||||
onPressed: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => const CounterBlocPage())),
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text('Counter with CubitScreen'),
|
||||
onPressed: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => const CounterCubitPage())),
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text('Counter with BlocProviderScreen'),
|
||||
onPressed: () => Navigator.of(context).push(MaterialPageRoute(
|
||||
builder: (_) => const CounterProviderPage())),
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text('Counter with BlocConsumerScreen'),
|
||||
onPressed: () => Navigator.of(context)
|
||||
.push(MaterialPageRoute(builder: (context) {
|
||||
return BlocProvider<CounterCubit>(
|
||||
create: (context) => CounterCubit(context.read<CounterRepository>()),
|
||||
child: const CounterConsumerPage(),
|
||||
);
|
||||
})),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user