Showing
3 changed files
with
71 additions
and
0 deletions
1 | +import 'dart:async'; | ||
2 | + | ||
3 | +import 'package:get/get.dart'; | ||
4 | +import 'package:async/async.dart'; | ||
5 | + | ||
6 | +class SplashService extends GetxService { | ||
7 | + final welcomeStr = ['GetX', 'Rules!']; | ||
8 | + final activeStr = 0.obs; | ||
9 | + | ||
10 | + final memo = AsyncMemoizer<void>(); | ||
11 | + Future<void> init() { | ||
12 | + return memo.runOnce(_initFunction); | ||
13 | + } | ||
14 | + | ||
15 | + void _changeActiveString() { | ||
16 | + activeStr.value = (activeStr.value + 1) % welcomeStr.length; | ||
17 | + } | ||
18 | + | ||
19 | + Future<void> _initFunction() async { | ||
20 | + final t = Timer.periodic( | ||
21 | + Duration(milliseconds: 500), | ||
22 | + (t) => _changeActiveString(), | ||
23 | + ); | ||
24 | + //simulate some long running operation | ||
25 | + await Future.delayed(Duration(seconds: 5)); | ||
26 | + //cancel the timer once we are done | ||
27 | + t.cancel(); | ||
28 | + } | ||
29 | +} |
1 | +import 'package:flutter/material.dart'; | ||
2 | + | ||
3 | +import 'package:get/get.dart'; | ||
4 | + | ||
5 | +import '../controllers/splash_service.dart'; | ||
6 | + | ||
7 | +class SplashView extends GetView<SplashService> { | ||
8 | + @override | ||
9 | + Widget build(BuildContext context) { | ||
10 | + return Scaffold( | ||
11 | + body: Center( | ||
12 | + child: Column( | ||
13 | + mainAxisSize: MainAxisSize.min, | ||
14 | + children: [ | ||
15 | + Obx( | ||
16 | + () => Text( | ||
17 | + controller.welcomeStr[controller.activeStr.value], | ||
18 | + style: TextStyle(fontSize: 20), | ||
19 | + ), | ||
20 | + ), | ||
21 | + CircularProgressIndicator(), | ||
22 | + ], | ||
23 | + ), | ||
24 | + ), | ||
25 | + ); | ||
26 | + } | ||
27 | +} |
1 | +import 'package:example_nav2/app/modules/splash/controllers/splash_service.dart'; | ||
2 | +import 'package:example_nav2/app/modules/splash/views/splash_view.dart'; | ||
1 | import 'package:flutter/material.dart'; | 3 | import 'package:flutter/material.dart'; |
2 | import 'package:get/get.dart'; | 4 | import 'package:get/get.dart'; |
3 | 5 | ||
@@ -10,10 +12,23 @@ void main() { | @@ -10,10 +12,23 @@ void main() { | ||
10 | title: "Application", | 12 | title: "Application", |
11 | initialBinding: BindingsBuilder( | 13 | initialBinding: BindingsBuilder( |
12 | () { | 14 | () { |
15 | + Get.put(SplashService()); | ||
13 | Get.put(AuthService()); | 16 | Get.put(AuthService()); |
14 | }, | 17 | }, |
15 | ), | 18 | ), |
16 | getPages: AppPages.routes, | 19 | getPages: AppPages.routes, |
20 | + builder: (context, child) { | ||
21 | + return FutureBuilder<void>( | ||
22 | + key: ValueKey('initFuture'), | ||
23 | + future: Get.find<SplashService>().init(), | ||
24 | + builder: (context, snapshot) { | ||
25 | + if (snapshot.connectionState == ConnectionState.done) { | ||
26 | + return child ?? SizedBox.shrink(); | ||
27 | + } | ||
28 | + return SplashView(); | ||
29 | + }, | ||
30 | + ); | ||
31 | + }, | ||
17 | // routeInformationParser: GetInformationParser( | 32 | // routeInformationParser: GetInformationParser( |
18 | // // initialRoute: Routes.HOME, | 33 | // // initialRoute: Routes.HOME, |
19 | // ), | 34 | // ), |
-
Please register or login to post a comment