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

边栏推荐
- Interview experience in summer camp of Central South University in 2022
- repo Manifest Format
- During FTP login, the error "530 login incorrect.login failed" is reported
- Introduction to the implementation principle of rxjs observable filter operator
- Display time with message interval of more than 1 minute in wechat applet discussion area
- PHP export word method (phpword)
- Use of atomicinteger
- 安裝electron失敗的解决辦法
- LeetCode 0556.下一个更大元素 III - 4步讲完
- Kubernetes three dozen probes and probe mode
猜你喜欢

Symlink(): solution to protocol error in PHP artisan storage:link on win10

MCDF Experiment 1

C language improvement article (wchar_t) character type

If you can't learn, you have to learn. Jetpack compose writes an im app (I)

Laravel time zone timezone

Itext7 uses iexternalsignature container for signature and signature verification

(construction notes) ADT and OOP

Pki/ca and digital certificate

Is BigDecimal safe to calculate the amount? Look at these five pits~~

为什么我的mysql容器启动不了呢
随机推荐
Use of QT OpenGL camera
The difference between lambda and anonymous inner class
PHP export word method (one MHT)
wpa_ cli
Lambda表达式
Vulnhub's Tomato (tomato)
Symlink(): solution to protocol error in PHP artisan storage:link on win10
(construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
抓包整理外篇fiddler———— 会话栏与过滤器[二]
Atomic atomic operation
How to deploy web pages to Alibaba cloud
1-1 token
Use of atomicinteger
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
Dart: self study system
347. Top k high frequency elements
DEJA_VU3D - Cesium功能集 之 054-模拟火箭发射全过程
SLF4J 日志门面
Visual studio 2022 downloading and configuring opencv4.5.5
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术