diff --git a/bloc_login_example/.idea/workspace.xml b/bloc_login_example/.idea/workspace.xml index 1e53eb3..7172a1a 100644 --- a/bloc_login_example/.idea/workspace.xml +++ b/bloc_login_example/.idea/workspace.xml @@ -23,17 +23,20 @@ - - + + + + + - - + + - - + + @@ -41,11 +44,11 @@ - - + + - - + + @@ -53,11 +56,11 @@ - - + + - - + + @@ -65,8 +68,8 @@ - - + + @@ -77,8 +80,20 @@ - - + + + + + + + + + + + + + + @@ -106,6 +121,7 @@ authBlo AuthBloc BlocPro + dispose Login @@ -116,12 +132,13 @@ @@ -137,7 +154,6 @@ - @@ -174,6 +190,7 @@ + @@ -291,15 +308,16 @@ - + - + + @@ -316,7 +334,6 @@ - @@ -334,6 +351,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -524,56 +598,69 @@ - + - + + + + - + - - + + - + - - + + + + + - + - - + + - + - - + + - + - - + + + + + + + + + \ No newline at end of file diff --git a/bloc_login_example/lib/blocs/auth_bloc.dart b/bloc_login_example/lib/blocs/auth_bloc.dart index 43f921e..4b8d08b 100644 --- a/bloc_login_example/lib/blocs/auth_bloc.dart +++ b/bloc_login_example/lib/blocs/auth_bloc.dart @@ -1,10 +1,15 @@ import 'dart:async'; +import 'package:flutter/material.dart'; + +import '../screens/login.dart'; +import '../screens/sample.dart'; enum Gender { male, female } class AuthBloc extends Object { + BuildContext _context; final _loggedIn = StreamController.broadcast(); final _doneLoading = StreamController.broadcast(); @@ -15,13 +20,37 @@ class AuthBloc extends Object { Function(bool) get setLoading => _doneLoading.sink.add; AuthBloc() { - init(); + loggedIn.listen((bool result) { + if (_context != null) { + Navigator.pushReplacement( + _context, + PageRouteBuilder( + pageBuilder: (_context1, animation1, animation2) { + return !result ? Login() : Sample(); + }, + transitionsBuilder: (_context, animation, _, child) { + return new SlideTransition( + child: child, + position: new Tween( + begin: !result ? const Offset(-1.0, 0.0) : const Offset(1.0, 0.0), + end: Offset.zero, + ).animate(animation), + ); + }, + transitionDuration: Duration(milliseconds: 500), + ), + ); + } + }, onError: (error) { + Scaffold.of(_context).showSnackBar(new SnackBar( + content: new Text(error.message), + )); + }); + setLoading(true); } - init() async { - print('init'); - await Future.delayed(Duration(seconds: 2)); - setLoading(true); + setContext(BuildContext context) { + _context = context; } dispose() { diff --git a/bloc_login_example/lib/screens/auth.dart b/bloc_login_example/lib/screens/auth.dart index 72a2c31..b09c116 100644 --- a/bloc_login_example/lib/screens/auth.dart +++ b/bloc_login_example/lib/screens/auth.dart @@ -20,18 +20,12 @@ class _AuthState extends State { _checkAuth(); } - @override - void dispose() { - BlocProvider.of(context).authBloc.dispose(); - super.dispose(); - } - @override Widget build(BuildContext context) { return StreamBuilder( - stream: BlocProvider.of(context).authBloc.loggedIn, + stream: BlocProvider.of(context).authBloc.doneLoading, builder: (context, snapshot) { - if (!snapshot.hasData) { + if (!snapshot.hasData || !snapshot.data) { return Container( color: Theme.Colors.blue, width: double.infinity, @@ -45,17 +39,29 @@ class _AuthState extends State { ), )), ); - } else if (snapshot.data) { - return Sample(); - } else { - return Login(); } + return Container( + color: Theme.Colors.blue, + width: double.infinity, + height: double.infinity, + child: Center(child: Text( + 'You should not see this.', + style: TextStyle( + fontSize: 32.0, + color: Colors.white, + decorationStyle: TextDecorationStyle.solid, + ), + )), + ); }, ); } - _checkAuth() async { - await Future.delayed(Duration(seconds: 3)); - BlocProvider.of(context).authBloc.setLoggedIn(true); + _checkAuth() { + Future.delayed(Duration.zero, () async { + BlocProvider.of(context).authBloc.setContext(context); + await Future.delayed(Duration(seconds: 3)); + BlocProvider.of(context).authBloc.setLoggedIn(true); + }); } } \ No newline at end of file diff --git a/bloc_login_example/lib/screens/login.dart b/bloc_login_example/lib/screens/login.dart index bba2e1a..5147d3d 100644 --- a/bloc_login_example/lib/screens/login.dart +++ b/bloc_login_example/lib/screens/login.dart @@ -172,6 +172,7 @@ class _LoginState extends State { } void _setLoggedIn() { + BlocProvider.of(context).authBloc.setContext(context); BlocProvider.of(context).authBloc.setLoggedIn(true); } diff --git a/bloc_login_example/lib/screens/sample.dart b/bloc_login_example/lib/screens/sample.dart index c7c641e..ff8e2e2 100644 --- a/bloc_login_example/lib/screens/sample.dart +++ b/bloc_login_example/lib/screens/sample.dart @@ -27,7 +27,10 @@ class Sample extends StatelessWidget { Text('SampleChild3'), SampleChild3(), FlatButton( - onPressed: () => BlocProvider.of(context).authBloc.setLoggedIn(false), + onPressed: () { + BlocProvider.of(context).authBloc.setContext(context); + BlocProvider.of(context).authBloc.setLoggedIn(false); + }, child: Text('Logout', style: TextStyle( fontSize: 18.0,