当前位置:网站首页>Flutter 退出当前操作二次确认怎么做才更优雅?
Flutter 退出当前操作二次确认怎么做才更优雅?
2022-07-03 09:20:00 【InfoQ】
前言
BlocListenerlistenerBlocListenerBlocListener登录状态
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);
}
业务逻辑
- 已登录:显示为退出登录;
- 已退出登录:显示为登录。

代码实现
BlocListenerBlocProviderBlocListenerBlocBuilderclass 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();
}
},
),
),
),
),
);
}
SnackBartruefalsetrueLoginCubitlogout
总结
BlocListener
边栏推荐
- Modelcheckpoint auto save model
- Wireshark use
- Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- 01 business structure of imitation station B project
- Adaptiveavgpool1d internal implementation
- Retinaface: single stage dense face localization in the wild
- 2021-10-27
- 20220610其他:任务调度器
- Screen display of charging pile design -- led driver ta6932
猜你喜欢

QT is a method of batch modifying the style of a certain type of control after naming the control

LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))

CV learning notes convolutional neural network

One click generate traffic password (exaggerated advertisement title)

ADS simulation design of class AB RF power amplifier

LeetCode - 706 设计哈希映射(设计) *

Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)

Discrete-event system

Opencv note 21 frequency domain filtering

Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
随机推荐
When the reference is assigned to auto
The data read by pandas is saved to the MySQL database
Simulate mouse click
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
【C 题集】of Ⅵ
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
Opencv feature extraction sift
Leetcode - 933 number of recent requests
El table X-axis direction (horizontal) scroll bar slides to the right by default
LeetCode - 673. 最长递增子序列的个数
Design of charging pile mqtt transplantation based on 4G EC20 module
Development of intelligent charging pile (I): overview of the overall design of the system
1. Finite Markov Decision Process
CV learning notes - feature extraction
2021-10-27
CV learning notes - camera model (Euclidean transformation and affine transformation)
Vscode markdown export PDF error
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
03 fastjason solves circular references
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip