refactor(bloc_helper): general code update

This commit is contained in:
Malo Léon 2022-07-14 11:11:02 +01:00
parent 58fec4bf48
commit e00f4f55b7
5 changed files with 21 additions and 29 deletions

View File

@ -41,12 +41,10 @@ abstract class BlocBaseConsumerScreen<B extends BlocBase<S>, S extends Object>
void onListen(BuildContext context, S state) {} void onListen(BuildContext context, S state) {}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => BlocConsumer<B, S>(
return BlocConsumer<B, S>( listenWhen: shouldListenWhen,
listenWhen: shouldListenWhen, listener: onListen,
listener: onListen, buildWhen: shouldBuildWhen,
buildWhen: shouldBuildWhen, builder: onBuild,
builder: onBuild, );
);
}
} }

View File

@ -28,10 +28,8 @@ abstract class BlocBaseProviderScreen<B extends BlocBase<S>, S extends Object>
Widget buildChild(BuildContext context); Widget buildChild(BuildContext context);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => BlocProvider<B>(
return BlocProvider<B>( create: (_) => create(context),
create: (_) => create(context), child: Builder(builder: buildChild),
child: Builder(builder: buildChild), );
);
}
} }

View File

@ -26,10 +26,8 @@ abstract class BlocBaseScreen<B extends BlocBase<S>, S extends Object>
B create(BuildContext context); B create(BuildContext context);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => BlocProvider<B>(
return BlocProvider<B>( create: (_) => create(context),
create: (_) => create(context), child: super.build(context),
child: super.build(context), );
);
}
} }

View File

@ -21,8 +21,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
/// [Bloc] and [Cubit] widgets. /// [Bloc] and [Cubit] widgets.
mixin BlocBaseProviderMixin<B extends BlocBase<Object>> { mixin BlocBaseProviderMixin<B extends BlocBase<Object>> {
/// Returns the [BlocBase] used by this [BlocBaseProviderMixin]. /// Returns the [BlocBase] used by this [BlocBaseProviderMixin].
B bloc(BuildContext context) => BlocProvider.of<B>(context); B bloc(BuildContext context) => context.read<B>();
/// Returns the [BlocBase] used by this [BlocBaseProviderMixin]. /// Returns the [BlocBase] used by this [BlocBaseProviderMixin].
R repo<R>(BuildContext context) => RepositoryProvider.of<R>(context); R repo<R>(BuildContext context) => context.read<R>();
} }

View File

@ -1,26 +1,24 @@
// Copyright (C) 2022 WYATT GROUP // Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details. // Please see the AUTHORS file for details.
// //
// This program is free software: you can redistribute it and/or modify // 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 // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// any later version. // any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
/// [Bloc] specific mixin that provides implementation /// [Bloc] specific mixin that provides implementation
/// of helper methods for events. /// of helper methods for events.
mixin BlocProviderMixin<B extends Bloc<E, Object>, E> { mixin BlocProviderMixin<B extends Bloc<E, Object>, E> {
void add(BuildContext context, E event) { void add(BuildContext context, E event) => context.read<B>().add(event);
BlocProvider.of<B>(context).add(event);
}
} }