Flutter One time Intro Screen?

后端 未结 6 1279
无人共我
无人共我 2021-01-30 11:22

I have an intro screen for my app, but it shows every time I open the app, I need to show that for the 1st time only.

How to do that?

//         


        
6条回答
  •  醉酒成梦
    2021-01-30 12:08

    Use shared_preferences:

    Full code:

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      var prefs = await SharedPreferences.getInstance();
      var boolKey = 'isFirstTime';
      var isFirstTime = prefs.getBool(boolKey) ?? true;
    
      runApp(MaterialApp(home: isFirstTime ? IntroScreen(prefs, boolKey) : RegularScreen()));
    }
    
    class IntroScreen extends StatelessWidget {
      final SharedPreferences prefs;
      final String boolKey;
      IntroScreen(this.prefs, this.boolKey);
    
      Widget build(BuildContext context) {
        prefs.setBool(boolKey, false); // You might want to save this on a callback.
        return Scaffold();
      }
    }
    
    class RegularScreen extends StatelessWidget {
      Widget build(BuildContext context) => Scaffold();
    }
    

提交回复
热议问题