-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💡 Implement dependency provider inherited widget
- Loading branch information
1 parent
0c0b89f
commit 80e7324
Showing
7 changed files
with
101 additions
and
38 deletions.
There are no files selected for viewing
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
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(), | ||
); | ||
} | ||
} |
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
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}); | ||
} |
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
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(), | ||
); | ||
} | ||
} |
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
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
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
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
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(), | ||
); | ||
} | ||
} |