Last active
December 9, 2019 04:52
-
-
Save CodyDunlap/8500589c5d846f84698593ba39f9afc6 to your computer and use it in GitHub Desktop.
Injecting services into Widget's using package:inject
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
@Injector(const [ServicesModule, ViewModelModule]) | |
abstract class AppComponent { | |
static Future<AppComponent> create( | |
ServicesModule servicesModule, | |
ViewModelModule viewModule, | |
) async { | |
return await g.AppComponent$Injector.create(servicesModule, viewModule); | |
} | |
@provide | |
MyApp get app; | |
} | |
typedef Provider<T> = T Function(); |
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
void main() async { | |
var container = await AppComponent.create(ServicesModule(), ViewModelModule()); | |
runApp(container.app); | |
} | |
@provide | |
class MyApp extends StatelessWidget { | |
final Router _router; | |
MyApp(this._router); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
initialRoute: '/', | |
onGenerateRoute: _router.generateRoute | |
); | |
} | |
} |
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
@provide | |
class Router { | |
final Provider<HomeView> _homePage; | |
final Provider<LoginView> _loginPage; | |
final Provider<SettingsView> _settingsPage; | |
Router( | |
this._homePage, | |
this._loginPage, | |
this._settingsPage, | |
); | |
Route<dynamic> generateRoute(RouteSettings settings) { | |
switch (settings.name) { | |
case '/': | |
return MaterialPageRoute(builder: (_) => _homePage()); | |
case '/login': | |
return MaterialPageRoute(builder: (_) => _loginPage()); | |
case '/settings': | |
return MaterialPageRoute(builder: (_) => _settingsPage()); | |
default: | |
return MaterialPageRoute( | |
builder: (_) => Scaffold( | |
body: Center( | |
child: Text('No route defined for ${settings.name}')), | |
)); | |
} | |
} | |
} |
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
@module | |
class ServicesModule { | |
@provide | |
@singleton | |
SomeService provideSomeService() => SomeService(); | |
} |
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
class SettingsView extends StatefulWidget { | |
SettingsView({Key key, this.someService}) : super(key: key); | |
final SomeService someService; | |
@override | |
_SettingsViewState createState() => _SettingsViewState(); | |
} | |
class _SettingsViewState extends State<SettingsView> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Settings"), | |
), | |
body: Center( | |
child: Text("Settings"), | |
), | |
); | |
} | |
} |
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
@module | |
class ViewModule { | |
@provide | |
MyWidgetView provideMyWidgetView(SomeService someService) => MyWidgetView({someService: someService}); | |
} |
Hey @CodyDunlap thanks for so much help, It is working now just one question remaining. How will you pass params in this kind of navigation? Do we need to create separate typedefs for separate screens?
let me know if you got the solution for this. I am also stucked at this point
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @CodyDunlap thanks for so much help, It is working now just one question remaining. How will you pass params in this kind of navigation? Do we need to create separate typedefs for separate screens?