feat(bloc): add a lot of docs, fix onWrap, and add repo provider

This commit is contained in:
2022-07-17 15:25:57 +02:00
parent 5300e684de
commit b99664856d
32 changed files with 724 additions and 155 deletions
@@ -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;
}
}