diff --git a/lib/base/base_material_app.dart b/lib/base/base_material_app.dart new file mode 100644 index 0000000..8aa3e75 --- /dev/null +++ b/lib/base/base_material_app.dart @@ -0,0 +1,20 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:flutter_workshop/config/l10n.dart'; +import 'package:flutter_workshop/feature/home/home.dart'; + +class BaseMaterialApp extends StatelessWidget { + @override + Widget build(BuildContext context) { + return MaterialApp( + debugShowCheckedModeBanner: false, + localizationsDelegates: [ + const StringLocalizationsDelegate(), + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + ], + supportedLocales: AppLocales.supportedLocales, + home: Home(), + ); + } +} diff --git a/lib/base/dependency_provider.dart b/lib/base/dependency_provider.dart new file mode 100644 index 0000000..d9cf1cd --- /dev/null +++ b/lib/base/dependency_provider.dart @@ -0,0 +1,29 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_workshop/feature/home/home_bloc.dart'; +import 'package:flutter_workshop/feature/login/login_bloc.dart'; + +class DependencyProvider extends InheritedWidget { + const DependencyProvider({ + Key key, + @required Widget child, + this.dependencies, + }) : assert(child != null), + super(key: key, child: child); + + final AppDependencies dependencies; + + static DependencyProvider of(BuildContext context) { + return context.inheritFromWidgetOfExactType(DependencyProvider) + as DependencyProvider; + } + + @override + bool updateShouldNotify(DependencyProvider old) => true; +} + +class AppDependencies { + final LoginBloc loginBloc; + final HomeBloc homeBloc; + + AppDependencies({this.loginBloc, this.homeBloc}); +} diff --git a/lib/base/my_app.dart b/lib/base/my_app.dart new file mode 100644 index 0000000..758bef1 --- /dev/null +++ b/lib/base/my_app.dart @@ -0,0 +1,38 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_workshop/base/base_material_app.dart'; +import 'package:flutter_workshop/base/dependency_provider.dart'; +import 'package:flutter_workshop/feature/home/home_bloc.dart'; +import 'package:flutter_workshop/feature/login/login_bloc.dart'; + +class MyApp extends StatefulWidget { + @override + _MyAppState createState() => _MyAppState(); +} + +class _MyAppState extends State { + AppDependencies _appDependencies; + + @override + void initState() { + super.initState(); + _appDependencies = AppDependencies( + loginBloc: LoginBloc(), + homeBloc: HomeBloc() + ); + } + + @override + void dispose() { + _appDependencies.loginBloc.dispose(); + _appDependencies.homeBloc.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return DependencyProvider( + dependencies: _appDependencies, + child: BaseMaterialApp(), + ); + } +} diff --git a/lib/feature/home/home.dart b/lib/feature/home/home.dart index 0c03f54..878b04a 100644 --- a/lib/feature/home/home.dart +++ b/lib/feature/home/home.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter_workshop/base/dependency_provider.dart'; import 'package:flutter_workshop/config/l10n.dart'; import 'package:flutter_workshop/custom/custom_app_bar.dart'; import 'package:flutter_workshop/feature/detail/detail.dart'; @@ -16,18 +17,12 @@ class _HomeState extends State { HomeBloc _bloc; @override - void initState() { - super.initState(); - _bloc = HomeBloc(); + void didChangeDependencies() { + super.didChangeDependencies(); + _bloc = DependencyProvider.of(context).dependencies.homeBloc; _bloc.loadDonations(); } - @override - void dispose() { - _bloc.dispose(); - super.dispose(); - } - @override Widget build(BuildContext context) { return Scaffold( diff --git a/lib/feature/login/login.dart b/lib/feature/login/login.dart index cbb47a1..9c35952 100644 --- a/lib/feature/login/login.dart +++ b/lib/feature/login/login.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter_workshop/base/dependency_provider.dart'; import 'package:flutter_workshop/config/l10n.dart'; import 'package:flutter_workshop/custom/custom_app_bar.dart'; import 'package:flutter_workshop/feature/login/login_bloc.dart'; @@ -8,6 +9,8 @@ import 'package:flutter_workshop/util/custom_form_field_validator.dart'; import 'package:flutter_workshop/util/http_event.dart'; class Login extends StatefulWidget { + static const submitButtonKey = 'login_submit_button'; + @override _LoginState createState() => _LoginState(); } @@ -20,15 +23,9 @@ class _LoginState extends State { LoginBloc _bloc; @override - void initState() { - super.initState(); - _bloc = LoginBloc(); - } - - @override - void dispose() { - _bloc.dispose(); - super.dispose(); + void didChangeDependencies() { + super.didChangeDependencies(); + _bloc = DependencyProvider.of(context).dependencies.loginBloc; } @override @@ -104,6 +101,7 @@ class _LoginState extends State { height: 48.0, minWidth: double.maxFinite, child: FlatButton( + key: Key(Login.submitButtonKey), child: child, color: Theme.of(context).primaryColor, textColor: Colors.white, diff --git a/lib/feature/login/login_bloc.dart b/lib/feature/login/login_bloc.dart index 541e42c..3f0e115 100644 --- a/lib/feature/login/login_bloc.dart +++ b/lib/feature/login/login_bloc.dart @@ -7,7 +7,7 @@ import 'package:flutter_workshop/util/http_event.dart'; class LoginBloc { final StreamController _controller = - StreamController>(); + StreamController>.broadcast(); get stream => _controller.stream; @@ -17,6 +17,7 @@ class LoginBloc { try { _controller.sink.add(HttpEvent(state: EventState.loading)); final loginResponse = await LoginApi().login(request); + print('token: ${loginResponse.token}'); _controller.sink.add(HttpEvent( state: EventState.done, data: loginResponse)); } catch (error) { diff --git a/lib/main.dart b/lib/main.dart index c697024..e1154a7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,22 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:flutter_localizations/flutter_localizations.dart'; -import 'package:flutter_workshop/config/l10n.dart'; -import 'package:flutter_workshop/feature/home/home.dart'; +import 'package:flutter_workshop/base/my_app.dart'; void main() => runApp(MyApp()); - -class MyApp extends StatelessWidget { - @override - Widget build(BuildContext context) { - return MaterialApp( - debugShowCheckedModeBanner: false, - localizationsDelegates: [ - const StringLocalizationsDelegate(), - GlobalMaterialLocalizations.delegate, - GlobalWidgetsLocalizations.delegate, - ], - supportedLocales: AppLocales.supportedLocales, - home: Home(), - ); - } -}