当前位置:网站首页>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
BlocListenerlistenerBlocListenerBlocListenerThe 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
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 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();
}
},
),
),
),
),
);
}
SnackBartruefalsetrueLoginCubitlogout
summary
BlocListener
边栏推荐
- LeetCode - 715. Range 模块(TreeSet) *****
- It is difficult to quantify the extent to which a single-chip computer can find a job
- (2) New methods in the interface
- 20220610其他:任务调度器
- 2.1 Dynamic programming and case study: Jack‘s car rental
- 2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
- Modelcheckpoint auto save model
- Opencv interview guide
- LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
- 20220531数学:快乐数
猜你喜欢

Working mode of 80C51 Serial Port

Leetcode bit operation

1. Finite Markov Decision Process

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*

Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*

LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)

51 MCU tmod and timer configuration

Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)

Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip

2. Elment UI date selector formatting problem
随机推荐
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
After clicking the Save button, you can only click it once
It is difficult to quantify the extent to which a single-chip computer can find a job
CV learning notes - deep learning
Qcombox style settings
2. Elment UI date selector formatting problem
Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
Working mode of 80C51 Serial Port
About windows and layout
Tensorflow2.0 save model
Modelcheckpoint auto save model
Tensorflow built-in evaluation
使用sed替换文件夹下文件
Vgg16 migration learning source code
Circular queue related design and implementation reference 1
20220608其他:逆波兰表达式求值
Opencv notes 17 template matching
Matplotlib drawing