【问题标题】:How can I do if seen check on custom splash screen? If walkthrough screen is already seen如果在自定义初始屏幕上看到检查,我该怎么办?如果已经看到演练屏幕
【发布时间】:2019-09-29 13:41:45
【问题描述】:

我正在做一个启动屏幕第一个应用程序,如果用户第一次使用该应用程序,则后面是一个演练页面,如果已经看到演练屏幕,则转到欢迎页面登录/注册。

我的代码来自这个项目的 main.dart 文件:https://github.com/instaflutter/flutter-login-screen-firebase-auth-facebook-login 并修改为这个代码(来自闪屏教程 FlutterKart)

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:binder/ui/screens/root_screen.dart';
import 'package:binder/ui/screens/walk_screen.dart';


void main() {
   Firestore.instance.settings(timestampsInSnapshotsEnabled: true);
   SharedPreferences.getInstance().then((prefs) {
   SplashScreen(prefs: prefs);
});
}

   class SplashScreen extends StatefulWidget {  
     final SharedPreferences prefs;
     SplashScreen({Key key,this.prefs}): super(key: key);

     @override
     _SplashScreenState createState() => _SplashScreenState();
     }

   class _SplashScreenState extends State<SplashScreen> {

    @override
   void initState() {
   super.initState();
   Timer(Duration(seconds: 3), () => _handleCurrentScreen(context));
   }

    @override
  Widget build(BuildContext context) {
     final logowhite = Hero(
      tag: 'hero',
      child: //code insert flutterkart splashscreen
                )
              ],
            ),
          )
        ],
      )
    ],
  ),
);
}
 Widget _handleCurrentScreen (BuildContext context) {
bool seen = (widget.prefs.getBool('seen') ?? false);
if (seen) {
  return new RootScreen();
} else {
  return new WalkthroughScreen(prefs: widget.prefs);
}
}
}

我希望它首先显示启动画面,如果已经看到,则定向到根屏幕,如果第一次使用,则定向到演练屏幕。

【问题讨论】:

    标签: flutter dart sharedpreferences splash-screen


    【解决方案1】:

    您可能想要使用 shared_preferences 或类似的东西。像这样的:

    // add this static variable somewhere
    // could technically be initialized during splash screen and added to a Provider or something similar after
    static SharedPreferences prefs;
    
    // make `main` async if it is not already
    Future<void> main() async {
      prefs = await SharedPreferences.getInstance();
    
      ...
    }
    
    Future<void> onSplashScreenDone() async {
      if (prefs.getBool('isFirstTime') ?? true) {
        // you might want to put this at the end of your walkthrough, so they don't miss it if they close the app, for example
        await prefs.setBool('isFirstTime', false);
    
        // this is their first time, show walkthrough, etc.
        ...
      } else {
        // this is not their first time, do normal things.
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多