flutter - 如何在颤振中使用展示案例视图?
问题描述
我在我的应用程序中使用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");
}
)
),
),
);
}));
}
解决方案
您可以使用包轻松地做到这一点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;
}
}
推荐阅读
- python - 对于 python 版本 3.7 中的 Keras 库下载问题
- php - 用于 Rocket 聊天集成的 Access-Control-Allow-Origin
- android - 录制时在视频上添加水印
- node.js - 在heroku上部署应用程序时出现过时的纱线锁文件错误
- python-3.x - 使用来自 URL 响应的 PIL 打开特定图像的问题
- ios - 由于 heightAnchor,无法垂直滚动 collectionview
- api - 在 API 平台中存储来自 POST 请求的相关数据
- kubernetes - ELB(经典负载均衡器)代理协议不适用于 Kubernetes 集群
- ruby - 如何限制用户在 Shopify Plus 中购物超过礼品卡金额?
- javascript - 使用 JSONP 时的 CORB