当前位置:网站首页>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

边栏推荐
- Stm32 NVIC interrupt priority management
- Positive and negative sample division and architecture understanding in image classification and target detection
- LeetCode - 933 最近的请求次数
- 2.2 DP: Value Iteration & Gambler‘s Problem
- LeetCode - 715. Range 模块(TreeSet) *****
- Dynamic layout management
- Window maximum and minimum settings
- Vgg16 migration learning source code
- openCV+dlib实现给蒙娜丽莎换脸
- Vscode markdown export PDF error
猜你喜欢
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
Opencv image rotation
Leetcode-112:路径总和
03 fastjason solves circular references
CV learning notes - clustering
ADS simulation design of class AB RF power amplifier
El table X-axis direction (horizontal) scroll bar slides to the right by default
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
Development of intelligent charging pile (I): overview of the overall design of the system
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
随机推荐
LeetCode - 900. RLE 迭代器
Drive and control program of Dianchuan charging board for charging pile design
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
Yocto Technology Sharing Phase 4: Custom add package support
yocto 技术分享第四期:自定义增加软件包支持
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Application of external interrupts
Leetcode 300 longest ascending subsequence
CV learning notes - image filter
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
CV learning notes - edge extraction
One click generate traffic password (exaggerated advertisement title)
Opencv interview guide
2.2 DP: Value Iteration & Gambler‘s Problem
Serial port programming
Google browser plug-in recommendation
openCV+dlib实现给蒙娜丽莎换脸