当前位置:网站首页>What can I do to exit the current operation and confirm it twice?
What can I do to exit the current operation and confirm it twice?
2022-07-03 10:11:00 【InfoQ】
Preface
BlocListener
listener
BlocListener
BlocListener
The login status
LoginStatus
- logon: Logged in
- logout: Logged out
- logoutConfirm: Exit login confirmation
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);
}
Business logic
- Logged in : Displayed as logout ;
- Logged out : Displayed as login .

Code implementation
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 Example '),
),
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 ? ' Logged out ' : ' Login successful '),
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 ? ' Log out ' : ' Sign in ',
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

summary
BlocListener

边栏推荐
- LeetCode - 919. Full binary tree inserter (array)
- 4G module initialization of charge point design
- Octave instructions
- 4G module board level control interface designed by charging pile
- LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
- Dynamic layout management
- Wireshark use
- 【C 题集】of Ⅵ
- RESNET code details
- Circular queue related design and implementation reference 1
猜你喜欢
2021-10-28
pycharm 无法引入自定义包
LeetCode - 919. 完全二叉树插入器 (数组)
2.2 DP: Value Iteration & Gambler‘s Problem
2021-10-27
LeetCode - 705 设计哈希集合(设计)
【C 题集】of Ⅵ
Leetcode - 933 number of recent requests
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
随机推荐
03 fastjason solves circular references
After clicking the Save button, you can only click it once
Opencv histogram equalization
Positive and negative sample division and architecture understanding in image classification and target detection
Opencv interview guide
openCV+dlib實現給蒙娜麗莎換臉
Anaconda安装包 报错packagesNotFoundError: The following packages are not available from current channels:
4G module IMEI of charging pile design
LeetCode - 933 最近的请求次数
使用密钥对的形式连接阿里云服务器
Deep learning by Pytorch
Liquid crystal display
Google browser plug-in recommendation
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
01 business structure of imitation station B project
4G module at command communication package interface designed by charging pile
LeetCode - 919. 完全二叉树插入器 (数组)
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Dictionary tree prefix tree trie
LeetCode - 673. 最长递增子序列的个数