Flutter hold splash screen for 3 Seconds. How to implement splash screen in Flutter?

前端 未结 9 2024
后悔当初
后悔当初 2021-02-16 00:16

How to show splash screen in flutter for 3 seconds and then go next my login screen.

I have tried.countdowntimer but import is unresolved

import \'pack         


        
相关标签:
9条回答
  • 2021-02-16 00:37

    This answer is applicable only in case you are using flutter-redux.

    Along with flutter-redux you need to use redux-persist library to show loading screen.

    redux-persist is used to store, rehydrate app state.

    Example:

    1.main.dart

    import 'package:flutter/material.dart';
    import 'package:flutter/foundation.dart';
    import 'package:flutter_redux/flutter_redux.dart';
    import 'package:redux_persist_flutter/redux_persist_flutter.dart';
    
    import 'package:flutter_redux_starter/presentation/platform_adaptive.dart';
    import 'package:flutter_redux_starter/screens/loading_screen.dart';
    import 'package:flutter_redux_starter/store/store.dart';
    import 'package:flutter_redux_starter/middleware/middleware.dart';
    import 'package:flutter_redux_starter/models/app_state.dart';
    import 'package:flutter_redux_starter/routes.dart';
    
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
        final store = createStore();
    
        MyApp();
    
        @override
        Widget build(BuildContext context) {
            return new PersistorGate(
                persistor: persistor,
                loading: new LoadingScreen(),
                builder: (context) => new StoreProvider<AppState>(
                    store: store,
                    child: new MaterialApp(
                        title: 'Flutter test App',
                        theme: defaultTargetPlatform == TargetPlatform.iOS
                            ? kIOSTheme
                            : kDefaultTheme,
                    routes: getRoutes(context, store),
                        initialRoute: '/login',
                    )
                ),
            );
        }
    
    }
    

    2.store.dart

    import 'package:redux/redux.dart';
    
    import 'package:flutter_redux_starter/reducers/app_reducer.dart';
    import 'package:flutter_redux_starter/models/app_state.dart';
    import 'package:flutter_redux_starter/middleware/middleware.dart';
    
    Store<AppState> createStore() { 
        Store<AppState> store = new Store(
            appReducer,
            initialState: new AppState(),
            middleware: createMiddleware(),
        );
        persistor.start(store);
    
        return store;
    }
    

    In createStore you can use Future.delayed to delay the creation of store for certain no of seconds.

    new Future.delayed(const Duration(seconds: 3), () {
     // 
    });
    
    0 讨论(0)
  • 2021-02-16 00:43

    You can execute code with a delay using Future.delayed

    new Future.delayed(const Duration(seconds: 3), () {
      Navigator.pushNamed(context, '/login');
    });
    

    update

    const delay = 3;
    widget.countdown = delay;
    
    StreamSubscription sub;
    sub = new Stream.periodic(const Duration(seconds: 1), (count) {
      setState(() => widget.countdown--);  
      if(widget.countdown <= 0) {
        sub.cancel();
        Navigator.pushNamed(context, '/login');
      }
    });     
    
    0 讨论(0)
  • 2021-02-16 00:45

    I think you need to clear the old activity from stack (Splash Screen), So you have to use pushNamedAndRemoveUntil instead of using pushNamed only.

      new Future.delayed(const Duration(seconds: 3), () {
      Navigator.pushNamedAndRemoveUntil(context, '/login', ModalRoute.withName('/'));
    });
    
    0 讨论(0)
提交回复
热议问题