feat(bloc): add provider, consumer and helpers

This commit is contained in:
2022-05-20 22:32:30 +02:00
parent 2ac2b4d2be
commit 9ef37633cb
45 changed files with 1435 additions and 0 deletions
@@ -0,0 +1,52 @@
// 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:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
abstract class BlocBaseConsumerScreen<B extends BlocBase<S>, S extends Object>
extends StatelessWidget {
const BlocBaseConsumerScreen({super.key});
/// Takes the previous `state` and the current `state` and is responsible for
/// returning a [bool] which determines whether or not to trigger
/// [onBuild] with the current `state`.
bool shouldBuildWhen(S previous, S current) => true;
/// Takes the previous `state` and the current `state` and is responsible for
/// returning a [bool] which determines whether or not to trigger
/// [onListen] with the current `state`.
bool shouldListenWhen(S previous, S current) => true;
/// The [onBuild] function which will be invoked on each widget build.
/// The [onBuild] takes the `BuildContext` and current `state` and
/// must return a widget.
Widget onBuild(BuildContext context, S state);
/// Takes the `BuildContext` along with the `state`
/// and is responsible for executing in response to `state` changes.
void onListen(BuildContext context, S state) {}
@override
Widget build(BuildContext context) {
return BlocConsumer<B, S>(
listenWhen: shouldListenWhen,
listener: onListen,
buildWhen: shouldBuildWhen,
builder: onBuild,
);
}
}
@@ -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:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
abstract class BlocBaseProviderScreen<B extends BlocBase<S>, S extends Object>
extends StatelessWidget {
const BlocBaseProviderScreen({super.key});
/// Creates the [Cubit] or [Bloc] to be used.
B create(BuildContext context);
/// Creates the child [Widget] to be used.
Widget buildChild(BuildContext context);
@override
Widget build(BuildContext context) {
return BlocProvider<B>(
create: (_) => create(context),
child: Builder(builder: buildChild),
);
}
}
@@ -0,0 +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:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_bloc_helper/src/bloc_base/bloc_base_consumer_screen.dart';
abstract class BlocBaseScreen<B extends BlocBase<S>, S extends Object>
extends BlocBaseConsumerScreen<B, S> {
const BlocBaseScreen({super.key});
/// Creates the [Cubit] or [Bloc] to be used.
B create(BuildContext context);
@override
Widget build(BuildContext context) {
return BlocProvider<B>(
create: (_) => create(context),
child: super.build(context),
);
}
}