当前位置:网站首页>What is more elegant for flutter to log out and confirm again?
What is more elegant for flutter to log out and confirm again?
2022-07-03 12:23:00 【Ma Nong @ official account on the island with the same name】
Preface
stay flutter_bloc Provides a status monitoring component BlocListener, When the state changes, it will call listener Callback function given by parameter , This method does not return a value , It can be used for us to deal with some reminders , For example, display pop-up reminder or confirmation , Display status information, etc . With BlocListener, It is equivalent to providing us with an additional entry to deal with object changes . Next we pass BlocListener Implement some App Second confirmation before logging out .
The login status
To simplify logic , Our login data has only one enumeration LoginStatus, There are three states :
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
We put a button in the middle of the screen , Switch button text according to login status :
Logged in : Displayed as logout ; Logged out : Displayed as login .
When you click the exit login button , The second confirmation dialog box pops up , After confirmation, change the status to logged out , Otherwise, keep the login status unchanged , The dialog box is shown below . 
Code implementation
Because of our buttons and BlocListener All need to use status data , Therefore use BlocProvider Placed on the upper layer is BlocListener and BlocBuilder Provide status data at the same time .
class BlocListenerWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => LoginCubit(),
child: BlocListenerDemo(),
);
}
}
BlocListener Part of the code is as follows :
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();
}
},
),
),
),
),
);
}
When the status is logged in and logged out, a SnackBar Prompt results , And if it's confirm login , A dialog box pops up . The dialog box will return true or false, If it is true It means confirm to exit , Call again at this point LoginCubit Of logout Log out . The source code has been submitted to :BLoC State management source code , The operation effect is as follows :

summary
You can see , With BlocListener, We can achieve the effect similar to the post interceptor , Do some extra processing after the state changes , For example, tips , Or upload data 、 Offline storage, etc . Deal with... In this way , It can reduce the coupling of business code .

边栏推荐
- Visual studio 2022 downloading and configuring opencv4.5.5
- (构造笔记)MIT reading部分学习心得
- repo Manifest Format
- Kubernetes three dozen probes and probe mode
- DEJA_ Vu3d - cesium feature set 053 underground mode effect
- Redis 笔记 01:入门篇
- error: expected reference but got (raw string)
- ES6 standard
- Atomic atomic operation
- Cloud Computing future - native Cloud
猜你喜欢
随机推荐
Adult adult adult
Dart: self study system
Vulnhub's Nagini
LeetCode 0556.下一个更大元素 III - 4步讲完
Colleagues wrote a responsibility chain model, with countless bugs
init. RC service failed to start
抓包整理外篇fiddler———— 会话栏与过滤器[二]
111. Minimum depth of binary tree
实现验证码验证
网络通讯之Socket-Tcp(一)
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
232. Implement queue with stack
Lambda表达式
使用BLoC 构建 Flutter的页面实例
239. Sliding window maximum
SystemVerilog -- OOP -- copy of object
Flutter 退出登录二次确认怎么做才更优雅?
typeScript
(数据库提权——Redis)Redis未授权访问漏洞总结
[combinatorics] permutation and combination (example of permutation and combination)









