Skip to content

Commit aef5eda

Browse files
committed
Make use of rxdart
1 parent 3936777 commit aef5eda

File tree

6 files changed

+68
-22
lines changed

6 files changed

+68
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import 'package:flutter/material.dart';
2-
import './screens/login_screen.dart';
2+
import 'package:login_bloc/src/blocs/provider.dart';
3+
import 'package:login_bloc/src/screens/login_screen.dart';
34

45
class App extends StatelessWidget {
56
@override
67
Widget build(BuildContext context) {
7-
return MaterialApp(
8-
title: 'Log me In',
9-
home: Scaffold(
10-
body: LoginScreen(),
11-
)
8+
return Provider(
9+
child: MaterialApp(
10+
title: 'Log me In',
11+
home: Scaffold(
12+
body: LoginScreen(),
13+
),
14+
),
1215
);
1316
}
14-
}
17+
}

Courses/Dart And Flutter The Complete Developers Guide/login_bloc/lib/src/blocs/bloc.dart

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
import 'dart:async';
22

3+
import 'package:rxdart/rxdart.dart';
4+
35
import 'validators.dart';
46

57
class Bloc with Validators {
6-
final _emailController = StreamController<String>();
8+
final _emailController = BehaviorSubject<String>();
79

8-
final _passwordController = StreamController<String>();
10+
final _passwordController = BehaviorSubject<String>();
911

1012
// Add data to stream
1113
Stream<String> get email => _emailController.stream.transform(validateEmail);
12-
14+
1315
Stream<String> get password =>
1416
_passwordController.stream.transform(validatePassword);
1517

18+
Stream<bool> get isValid =>
19+
Observable.combineLatest2(email, password, (e, p) => true);
20+
1621
// Change data
1722
Function(String) get changeEmail => _emailController.sink.add;
1823

1924
Function(String) get changePassword => _passwordController.sink.add;
2025

26+
submit() {
27+
final validEmail = _emailController.value;
28+
final validPassword = _passwordController.value;
29+
30+
print('Email is $validEmail and password is $validPassword');
31+
}
32+
2133
// Clean up
2234
dispose() {
2335
_emailController.close();
2436
_passwordController.close();
2537
}
2638
}
27-
28-
final bloc = Bloc();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:login_bloc/src/blocs/bloc.dart';
3+
4+
class Provider extends InheritedWidget {
5+
final bloc = new Bloc();
6+
7+
Provider({Key key, Widget child}) : super(key: key, child: child);
8+
9+
@override
10+
bool updateShouldNotify(InheritedWidget oldWidget) {
11+
return true;
12+
}
13+
14+
static Bloc of(BuildContext context) {
15+
return (context.inheritFromWidgetOfExactType(Provider) as Provider).bloc;
16+
}
17+
}

Courses/Dart And Flutter The Complete Developers Guide/login_bloc/lib/src/screens/login_screen.dart

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import 'package:flutter/material.dart';
22
import 'package:login_bloc/src/blocs/bloc.dart';
3+
import 'package:login_bloc/src/blocs/provider.dart';
34

45
class LoginScreen extends StatelessWidget {
56
@override
67
Widget build(BuildContext context) {
8+
final bloc = Provider.of(context);
9+
710
return Container(
811
margin: EdgeInsets.all(20.0),
912
child: Column(
1013
children: <Widget>[
11-
emailField(),
12-
passwordField(),
14+
emailField(bloc),
15+
passwordField(bloc),
1316
Container(
1417
margin: EdgeInsets.only(top: 25.0),
1518
),
16-
submitButton(),
19+
submitButton(bloc),
1720
],
1821
),
1922
);
2023
}
2124

22-
Widget emailField() {
25+
Widget emailField(Bloc bloc) {
2326
return StreamBuilder<Object>(
2427
stream: bloc.email,
2528
builder: (context, snapshot) {
@@ -36,7 +39,7 @@ class LoginScreen extends StatelessWidget {
3639
);
3740
}
3841

39-
Widget passwordField() {
42+
Widget passwordField(Bloc bloc) {
4043
return StreamBuilder<Object>(
4144
stream: bloc.password,
4245
builder: (context, snapshot) {
@@ -53,11 +56,15 @@ class LoginScreen extends StatelessWidget {
5356
);
5457
}
5558

56-
Widget submitButton() {
57-
return RaisedButton(
58-
child: Text('Login'),
59-
color: Colors.blue,
60-
onPressed: () {},
59+
Widget submitButton(Bloc bloc) {
60+
return StreamBuilder<Object>(
61+
stream: bloc.isValid,
62+
builder: (context, snapshot) {
63+
return RaisedButton(
64+
child: Text('Login'),
65+
color: Colors.blue,
66+
onPressed: snapshot.hasData ? bloc.submit : null);
67+
},
6168
);
6269
}
6370
}

Courses/Dart And Flutter The Complete Developers Guide/login_bloc/pubspec.lock

+7
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ packages:
8181
url: "https://pub.dartlang.org"
8282
source: hosted
8383
version: "2.0.1"
84+
rxdart:
85+
dependency: "direct main"
86+
description:
87+
name: rxdart
88+
url: "https://pub.dartlang.org"
89+
source: hosted
90+
version: "0.21.0"
8491
sky_engine:
8592
dependency: transitive
8693
description: flutter

Courses/Dart And Flutter The Complete Developers Guide/login_bloc/pubspec.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ dependencies:
2424
# Use with the CupertinoIcons class for iOS style icons.
2525
cupertino_icons: ^0.1.2
2626

27+
rxdart: ^0.21.0
28+
2729
dev_dependencies:
2830
flutter_test:
2931
sdk: flutter

0 commit comments

Comments
 (0)