Flutter / GetX Basics / STEP2: Splash Screen using getX
STEP2: Splash Screen
-
Steps
UI
import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:getx_restaurant/controllers/splash_controller.dart'; class SplashScreen extends StatelessWidget { SplashScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { Get.put(SplashController()); return Scaffold( body: Center( child: Text( 'Add', style: TextStyle(fontSize: 20.0), ), ), ); } } Controller
import 'package:flutter/cupertino.dart'; import 'package:get/get.dart'; import 'package:getx_restaurant/home.dart'; import 'dart:async'; class SplashController extends GetxController { @override void onInit() { super.onInit(); Future.delayed(const Duration(seconds: 2), () { Get.off( Home() ); }); } } Main dart
import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:get_storage/get_storage.dart'; import 'package:getx_restaurant/app_theme.dart'; import 'package:getx_restaurant/controllers/store_controller.dart'; import 'package:getx_restaurant/controllers/splash_controller.dart'; import 'package:getx_restaurant/controllers/theme_controller.dart'; import 'package:getx_restaurant/home.dart'; import 'package:getx_restaurant/screens/add_followers.dart'; import 'package:getx_restaurant/screens/add_reviews.dart'; import 'package:getx_restaurant/screens/add_follower_count.dart'; import 'package:getx_restaurant/screens/update_store_name.dart'; import 'package:getx_restaurant/screens/store_status.dart'; import 'package:getx_restaurant/screens/update_menu.dart'; import 'package:getx_restaurant/services/storage_service.dart'; import 'package:getx_restaurant/store_binding.dart'; import 'package:getx_restaurant/screens/splash.dart'; void main() async { await GetStorage.init(); await Get.putAsync(() => StorageService().init()); runApp(MyApp()); } class MyApp extends StatelessWidget { MyApp({Key? key}) : super(key: key); final themeController = Get.put(ThemeController()); @override Widget build(BuildContext context) { return GetMaterialApp( debugShowCheckedModeBanner: false, title: 'GetX Store', initialBinding: StoreBinding(), theme: Themes.lightTheme, darkTheme: Themes.darkTheme, themeMode: themeController.theme, initialRoute: '/', getPages: [ GetPage( name: '/', page: () => Home(), ), GetPage(name: '/edit_name', page: () => UpdateStoreName()), GetPage(name: '/add_followers', page: () => AddFollowers()), GetPage(name: '/toggle_status', page: () => StoreStatus()), GetPage(name: '/edit_follower_count', page: () => AddFollowerCount()), GetPage(name: '/add_reviews', page: () => AddReviews()), GetPage(name: '/update_menu', page: () => const UpdateMenu()), ], home: SplashScreen(), ); } } 1. main.dart call SplashScreen() in the first loading
2. redirect to Home() using Future.delayed() in splash_controller.dart
3. inject SplashController into splash.dart UI