Last active
January 21, 2019 22:53
-
-
Save felangel/ee7b33b4895df12d8885890346b1297d to your computer and use it in GitHub Desktop.
[flutter_login] main
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:bloc/bloc.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
import 'package:user_repository/user_repository.dart'; | |
import 'package:flutter_login/authentication/authentication.dart'; | |
import 'package:flutter_login/splash/splash.dart'; | |
import 'package:flutter_login/login/login.dart'; | |
import 'package:flutter_login/home/home.dart'; | |
import 'package:flutter_login/common/common.dart'; | |
class SimpleBlocDelegate extends BlocDelegate { | |
@override | |
void onTransition(Transition transition) { | |
print(transition.toString()); | |
} | |
} | |
void main() { | |
BlocSupervisor().delegate = SimpleBlocDelegate(); | |
runApp(App(userRepository: UserRepository())); | |
} | |
class App extends StatefulWidget { | |
final UserRepository userRepository; | |
App({Key key, @required this.userRepository}) : super(key: key); | |
@override | |
State<App> createState() => _AppState(); | |
} | |
class _AppState extends State<App> { | |
AuthenticationBloc authenticationBloc; | |
UserRepository get userRepository => widget.userRepository; | |
@override | |
void initState() { | |
authenticationBloc = AuthenticationBloc(userRepository: userRepository); | |
authenticationBloc.dispatch(AppStarted()); | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
authenticationBloc.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return BlocProvider<AuthenticationBloc>( | |
bloc: authenticationBloc, | |
child: MaterialApp( | |
home: BlocBuilder<AuthenticationEvent, AuthenticationState>( | |
bloc: authenticationBloc, | |
builder: (BuildContext context, AuthenticationState state) { | |
if (state is AuthenticationUninitialized) { | |
return SplashPage(); | |
} | |
if (state is AuthenticationAuthenticated) { | |
return HomePage(); | |
} | |
if (state is AuthenticationUnauthenticated) { | |
return LoginPage(userRepository: userRepository); | |
} | |
if (state is AuthenticationLoading) { | |
return LoadingIndicator(); | |
} | |
}, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment