如何在Flutter中使用展示案例视图? [英] How can i use show case view in flutter?

查看:70
本文介绍了如何在Flutter中使用展示案例视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用 showCaseView 包,并希望展示一次(仅在首先开始),我该怎么做才一次,而不会在下次启动时显示呢?

I use showCaseView package in my app, and want to showcase for one time (just after the first start), How can I do this only once and not show it on the next launches?

  @override
  void initState() {
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback(
          (_) {
        ShowCaseWidget.of(myContext).startShowCase([_one]);
      }
  );
  }
  @override
  Widget build(BuildContext context) {
  return ShowCaseWidget(
   // onFinish: ,
    builder:
  Builder(builder: (context) {
  myContext = context;
  return Scaffold(
    floatingActionButton: Showcase(
      key: _one,
      title: 'Title',
      description: 'Desc',
      child: InkWell(
        onTap: () {},
        child: FloatingActionButton(
         onPressed: (){
          print("floating");
        }
        )
      ),
    ),
  );
}));
}

推荐答案

您可以使用 shared_preferences 包轻松地做到这一点:

You can easily do this with the shared_preferences package:

class IsFirstLaunchPage extends StatefulWidget {
  static const PREFERENCES_IS_FIRST_LAUNCH_STRING = "PREFERENCES_IS_FIRST_LAUNCH_STRING";

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

class _IsFirstLaunchPageState extends State<IsFirstLaunchPage> {
  GlobalKey _one = GlobalKey();
  BuildContext myContext;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback(
            (_) {
          _isFirstLaunch().then((result){
            if(result)
              ShowCaseWidget.of(myContext).startShowCase([_one]);
          });
        }
    );
  }

  @override
  Widget build(BuildContext context) {
    return ShowCaseWidget(
      // onFinish: ,
        builder:
        Builder(builder: (context) {
          myContext = context;
          return Scaffold(
            floatingActionButton: Showcase(
              key: _one,
              title: 'Title',
              description: 'Desc',
              child: InkWell(
                  onTap: () {},
                  child: FloatingActionButton(
                      onPressed: () {
                        print("floating");
                      }
                  )
              ),
            ),
          );
        }));
  }

  Future<bool> _isFirstLaunch() async{
    final sharedPreferences = await SharedPreferences.getInstance();
    bool isFirstLaunch = sharedPreferences.getBool(IsFirstLaunchPage.PREFERENCES_IS_FIRST_LAUNCH_STRING) ?? true;

    if(isFirstLaunch)
      sharedPreferences.setBool(IsFirstLaunchPage.PREFERENCES_IS_FIRST_LAUNCH_STRING, false);

    return isFirstLaunch;
  }
}

这篇关于如何在Flutter中使用展示案例视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆