Commit 6c5a7feb authored by Tim Smart's avatar Tim Smart

Add flutter package

parent d6df8739
# Files and directories created by pub
.dart_tool/
.packages
# Remove the following pattern if you wish to check in your lock file
pubspec.lock
# Conventional directory for build outputs
build/
# Directory created by dartdoc
doc/api/
Copyright 2019 ARISE Church
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# flutter_bloc_stream
This package provides two helpers to help reduce boilerplate when working with
bloc_stream:
- `BlocStreamProvider`
- `BlocStreamBuilder`
See example for usage.
# Defines a default set of lint rules enforced for
# projects at Google. For details and rationale,
# see https://github.com/dart-lang/pedantic#enabled-lints.
include: package:pedantic/analysis_options.yaml
# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
# Uncomment to specify additional rules.
# linter:
# rules:
# - camel_case_types
analyzer:
# exclude:
# - path/to/excluded/files/**
android/
ios/
.idea/
example.iml
README.md
\ No newline at end of file
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.
version:
revision: 57f2df76d75cff290cbe2765b07db1ad3e67b50d
channel: beta
project_type: app
import 'package:bloc_stream/bloc_stream.dart';
import 'package:flutter_bloc_stream/flutter_bloc_stream.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class CounterBloc extends BlocStream<int> {
@override
int get initialValue => 0;
void increment() {
add(value + 1);
}
void decrement() {
add(value - 1);
}
@override
Future<void> close() {
return super.close();
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [BlocStreamProvider(create: (_) => CounterBloc())],
child: BlocStreamBuilder<CounterBloc, int>(
builder: (context, s) {
return MaterialApp(
supportedLocales: const [Locale('en')],
localizationsDelegates: [
DefaultMaterialLocalizations.delegate,
DefaultWidgetsLocalizations.delegate,
_ExampleLocalizationsDelegate(s.data),
],
home: const MyHomePage(),
);
},
),
);
}
}
class ExampleLocalizations {
static ExampleLocalizations of(BuildContext context) =>
Localizations.of<ExampleLocalizations>(context, ExampleLocalizations);
const ExampleLocalizations(this._count);
final int _count;
String get title => 'Tapped $_count times';
}
class _ExampleLocalizationsDelegate
extends LocalizationsDelegate<ExampleLocalizations> {
const _ExampleLocalizationsDelegate(this.count);
final int count;
@override
bool isSupported(Locale locale) => locale.languageCode == 'en';
@override
Future<ExampleLocalizations> load(Locale locale) =>
SynchronousFuture(ExampleLocalizations(count));
@override
bool shouldReload(_ExampleLocalizationsDelegate old) => old.count != count;
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Title()),
body: const Center(child: CounterLabel()),
floatingActionButton: const IncrementCounterButton(),
);
}
}
class IncrementCounterButton extends StatelessWidget {
const IncrementCounterButton({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return FloatingActionButton(
onPressed: () {
BlocStreamProvider.of<CounterBloc>(context).increment();
},
tooltip: 'Increment',
child: const Icon(Icons.add),
);
}
}
class CounterLabel extends StatelessWidget {
const CounterLabel({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
BlocStreamBuilder<CounterBloc, int>(
builder: (context, s) => Text(
'${s.data}',
style: Theme.of(context).textTheme.display1,
),
),
],
);
}
}
class Title extends StatelessWidget {
const Title({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Text(ExampleLocalizations.of(context).title);
}
}
name: example
version: 1.0.0
homepage: https://gitlab.com/arisechurch/bloc_stream
environment:
sdk: ">=2.5.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
bloc_stream:
flutter_bloc_stream:
dev_dependencies:
flutter_test:
sdk: flutter
dependency_overrides:
bloc_stream:
path: ../../bloc_stream
flutter_bloc_stream:
path: ../
flutter:
uses-material-design: true
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// 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:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:example/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
library flutter_bloc_stream;
export 'src/bloc_stream_builder.dart';
export 'src/bloc_stream_provider.dart';
import 'package:flutter/widgets.dart';
import 'package:bloc_stream/bloc_stream.dart';
import 'bloc_stream_provider.dart';
class BlocStreamBuilder<B extends BlocStream<S>, S> extends StatelessWidget {
BlocStreamBuilder({
Key key,
@required this.builder,
this.bloc,
}) : super(key: key);
final B bloc;
final AsyncWidgetBuilder<S> builder;
@override
Widget build(BuildContext context) {
final actualBloc = bloc ?? BlocStreamProvider.of<B>(context);
return StreamBuilder<S>(
stream: actualBloc.distinct(),
initialData: actualBloc.value,
builder: builder,
);
}
}
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
import 'package:bloc_stream/bloc_stream.dart';
class BlocStreamProvider<T extends BlocStream<dynamic>>
extends ValueDelegateWidget<T> implements SingleChildCloneableWidget {
final Widget child;
BlocStreamProvider({
Key key,
@required ValueBuilder<T> create,
Widget child,
}) : this._(
key: key,
delegate: BuilderStateDelegate<T>(
create,
dispose: (_, stream) => stream?.close(),
),
child: child,
);
BlocStreamProvider.value({
Key key,
@required T value,
Widget child,
}) : this._(
key: key,
delegate: SingleValueDelegate<T>(value),
child: child,
);
BlocStreamProvider._({
Key key,
@required ValueStateDelegate<T> delegate,
this.child,
}) : super(key: key, delegate: delegate);
static T of<T extends BlocStream<dynamic>>(BuildContext context) {
try {
return Provider.of<T>(context, listen: false);
} on Object catch (_) {
throw FlutterError(
'''
BlocStreamProvider.of() called with a context that does not contain a BlocStream of type $T.
No ancestor could be found starting from the context that was passed to BlocStreamProvider.of<$T>().
The context used was: $context
''',
);
}
}
@override
Widget build(BuildContext context) {
return InheritedProvider<T>(
value: delegate.value,
child: child,
);
}
@override
BlocStreamProvider<T> cloneWithChild(Widget child) {
return BlocStreamProvider<T>._(
key: key,
delegate: delegate,
child: child,
);
}
}
name: flutter_bloc_stream
description:
version: 1.0.0
homepage: https://gitlab.com/arisechurch/bloc_stream
environment:
sdk: ">=2.5.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
bloc_stream: ">= 1.0.0 < 2.0.0"
provider: ">= 3.0.0 < 4.0.0"
dev_dependencies:
pedantic: ^1.8.0
test: ^1.6.0
import 'package:test/test.dart';
void main() {
group('A group of tests', () {});
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment