Skip to content

Commit

Permalink
💡 Implement dependency provider inherited widget
Browse files Browse the repository at this point in the history
  • Loading branch information
toureholder committed Jun 14, 2019
1 parent 0c0b89f commit 80e7324
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 38 deletions.
20 changes: 20 additions & 0 deletions lib/base/base_material_app.dart
Original file line number Diff line number Diff line change
@@ -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(),
);
}
}
29 changes: 29 additions & 0 deletions lib/base/dependency_provider.dart
Original file line number Diff line number Diff line change
@@ -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});
}
38 changes: 38 additions & 0 deletions lib/base/my_app.dart
Original file line number Diff line number Diff line change
@@ -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<MyApp> {
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(),
);
}
}
13 changes: 4 additions & 9 deletions lib/feature/home/home.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,18 +17,12 @@ class _HomeState extends State<Home> {
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(
Expand Down
16 changes: 7 additions & 9 deletions lib/feature/login/login.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
}
Expand All @@ -20,15 +23,9 @@ class _LoginState extends State<Login> {
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
Expand Down Expand Up @@ -104,6 +101,7 @@ class _LoginState extends State<Login> {
height: 48.0,
minWidth: double.maxFinite,
child: FlatButton(
key: Key(Login.submitButtonKey),
child: child,
color: Theme.of(context).primaryColor,
textColor: Colors.white,
Expand Down
3 changes: 2 additions & 1 deletion lib/feature/login/login_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:flutter_workshop/util/http_event.dart';

class LoginBloc {
final StreamController _controller =
StreamController<HttpEvent<LoginResponse>>();
StreamController<HttpEvent<LoginResponse>>.broadcast();

get stream => _controller.stream;

Expand All @@ -17,6 +17,7 @@ class LoginBloc {
try {
_controller.sink.add(HttpEvent<LoginResponse>(state: EventState.loading));
final loginResponse = await LoginApi().login(request);
print('token: ${loginResponse.token}');
_controller.sink.add(HttpEvent<LoginResponse>(
state: EventState.done, data: loginResponse));
} catch (error) {
Expand Down
20 changes: 1 addition & 19 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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(),
);
}
}

0 comments on commit 80e7324

Please sign in to comment.