首页 > 解决方案 > 如何在颤振中使用展示案例视图?

问题描述

我在我的应用程序中使用showCaseView包,并且想展示一次(就在第一次启动之后),我怎样才能只做一次而不在下次启动时显示它?

  @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");
        }
        )
      ),
    ),
  );
}));
}

标签: flutterdartshowcaseview

解决方案


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

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;
  }
}

推荐阅读