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
+6 -6
View File
@@ -4,7 +4,7 @@
# This file should be version controlled.
version:
revision: ee4e09cce01d6f2d7f4baebd247fde02e5008851
revision: f1875d570e39de09040c8f79aa13cc56baab8db1
channel: stable
project_type: app
@@ -13,11 +13,11 @@ project_type: app
migration:
platforms:
- platform: root
create_revision: ee4e09cce01d6f2d7f4baebd247fde02e5008851
base_revision: ee4e09cce01d6f2d7f4baebd247fde02e5008851
- platform: android
create_revision: ee4e09cce01d6f2d7f4baebd247fde02e5008851
base_revision: ee4e09cce01d6f2d7f4baebd247fde02e5008851
create_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
base_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
- platform: web
create_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
base_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
# User provided section
@@ -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(),
);
})),
),
],
),
),
);
}
}
@@ -5,15 +5,14 @@
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:bloc_helper_example/counter_repository_provider_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:bloc_helper_example/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
await tester.pumpWidget(const CounterRepositoryProviderPage());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
Binary file not shown.

After

Width:  |  Height:  |  Size: 917 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html>
<head>
<!--
If you are serving your web app in a path other than the root, change the
href value below to reflect the base path you are serving from.
The path provided below has to start and end with a slash "/" in order for
it to work correctly.
For more details:
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
This is a placeholder for base href that will be replaced by the value of
the `--base-href` argument provided to `flutter build`.
-->
<base href="$FLUTTER_BASE_HREF">
<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="A new Flutter project.">
<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="example">
<link rel="apple-touch-icon" href="icons/Icon-192.png">
<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>
<title>example</title>
<link rel="manifest" href="manifest.json">
<script>
// The value below is injected by flutter build, do not touch.
var serviceWorkerVersion = null;
</script>
<!-- This script adds the flutter initialization JS code -->
<script src="flutter.js" defer></script>
</head>
<body>
<script>
window.addEventListener('load', function(ev) {
// Download main.dart.js
_flutter.loader.loadEntrypoint({
serviceWorker: {
serviceWorkerVersion: serviceWorkerVersion,
}
}).then(function(engineInitializer) {
return engineInitializer.initializeEngine();
}).then(function(appRunner) {
return appRunner.runApp();
});
});
</script>
</body>
</html>
@@ -0,0 +1,35 @@
{
"name": "example",
"short_name": "example",
"start_url": ".",
"display": "standalone",
"background_color": "#0175C2",
"theme_color": "#0175C2",
"description": "A new Flutter project.",
"orientation": "portrait-primary",
"prefer_related_applications": false,
"icons": [
{
"src": "icons/Icon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "icons/Icon-512.png",
"sizes": "512x512",
"type": "image/png"
},
{
"src": "icons/Icon-maskable-192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "maskable"
},
{
"src": "icons/Icon-maskable-512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable"
}
]
}