当前位置:网站首页>Flutter 退出当前操作二次确认怎么做才更优雅?
Flutter 退出当前操作二次确认怎么做才更优雅?
2022-07-03 09:20:00 【InfoQ】
前言
BlocListener
listener
BlocListener
BlocListener
登录状态
LoginStatus
- logon:已登录
- logout:已退出登录
- logoutConfirm:退出登录确认
enum LoginStatus { logon, logout, logoutConfirm }
class LoginCubit extends Cubit<LoginStatus> {
LoginCubit({initial = LoginStatus.logout}) : super(initial);
void login() => emit(LoginStatus.logon);
void logout() => emit(LoginStatus.logout);
void logoutConfirm() => emit(LoginStatus.logoutConfirm);
}
业务逻辑
- 已登录:显示为退出登录;
- 已退出登录:显示为登录。

代码实现
BlocListener
BlocProvider
BlocListener
BlocBuilder
class BlocListenerWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => LoginCubit(),
child: BlocListenerDemo(),
);
}
}
class BlocListenerDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('BlocListener 示例'),
),
body: Center(
child: BlocListener<LoginCubit, LoginStatus>(
listener: (context, loginSatus) async {
if (loginSatus == LoginStatus.logout ||
loginSatus == LoginStatus.logon) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(SnackBar(
content:
Text(loginSatus == LoginStatus.logout ? '已退出登录' : '登录成功'),
duration: Duration(seconds: 1),
));
} else {
var confirmed = await _confirmLogout(context);
if (confirmed == true) {
context.read<LoginCubit>().logout();
}
}
},
child: BlocBuilder<LoginCubit, LoginStatus>(
builder: (context, loginSatus) => TextButton(
child: Text(
loginSatus == LoginStatus.logon ? '退出登录' : '登录',
style: TextStyle(
fontSize: 24.0,
),
),
onPressed: () {
if (loginSatus == LoginStatus.logon) {
context.read<LoginCubit>().logoutConfirm();
} else {
context.read<LoginCubit>().login();
}
},
),
),
),
),
);
}
SnackBar
true
false
true
LoginCubit
logout

总结
BlocListener

边栏推荐
- Opencv gray histogram, histogram specification
- STM32 general timer output PWM control steering gear
- 20220605数学:两数相除
- LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
- Deep learning by Pytorch
- 2021-10-27
- Google browser plug-in recommendation
- CV learning notes - camera model (Euclidean transformation and affine transformation)
- 4G module board level control interface designed by charging pile
- yocto 技術分享第四期:自定義增加軟件包支持
猜你喜欢
Interruption system of 51 single chip microcomputer
Not many people can finally bring their interests to college graduation
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Opencv Harris corner detection
Leetcode-106:根据中后序遍历序列构造二叉树
Opencv image rotation
Vgg16 migration learning source code
Retinaface: single stage dense face localization in the wild
LeetCode - 5 最长回文子串
LeetCode - 706 设计哈希映射(设计) *
随机推荐
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
01 business structure of imitation station B project
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
LeetCode - 715. Range 模块(TreeSet) *****
Dictionary tree prefix tree trie
CV learning notes - feature extraction
My 4G smart charging pile gateway design and development related articles
Leetcode bit operation
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
MySQL root user needs sudo login
4.1 Temporal Differential of one step
Octave instructions
CV learning notes ransca & image similarity comparison hash
Serial communication based on 51 single chip microcomputer
CV learning notes - clustering
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
20220603数学:Pow(x,n)
4G module at command communication package interface designed by charging pile
CV learning notes convolutional neural network