当前位置:网站首页>Flutter 退出当前操作二次确认怎么做才更优雅?
Flutter 退出当前操作二次确认怎么做才更优雅?
2022-07-03 09:20:00 【InfoQ】
前言
BlocListenerlistenerBlocListenerBlocListener登录状态
LoginStatus- logon:已登录
- logout:已退出登录
- logoutConfirm:退出登录确认
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);
}
业务逻辑
- 已登录:显示为退出登录;
- 已退出登录:显示为登录。

代码实现
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 示例'),
),
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 ? '已退出登录' : '登录成功'),
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 ? '退出登录' : '登录',
style: TextStyle(
fontSize: 24.0,
),
),
onPressed: () {
if (loginSatus == LoginStatus.logon) {
context.read<LoginCubit>().logoutConfirm();
} else {
context.read<LoginCubit>().login();
}
},
),
),
),
),
);
}
SnackBartruefalsetrueLoginCubitlogout
总结
BlocListener
边栏推荐
猜你喜欢

Basic knowledge of communication interface

Discrete-event system

Interruption system of 51 single chip microcomputer

El table X-axis direction (horizontal) scroll bar slides to the right by default

One click generate traffic password (exaggerated advertisement title)

2.1 Dynamic programming and case study: Jack‘s car rental

LeetCode - 706 设计哈希映射(设计) *

I think all friends should know that the basic law of learning is: from easy to difficult

CV learning notes - edge extraction

Leetcode-106:根据中后序遍历序列构造二叉树
随机推荐
QT setting suspension button
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Stm32 NVIC interrupt priority management
Timer and counter of 51 single chip microcomputer
It is difficult to quantify the extent to which a single-chip computer can find a job
Matplotlib drawing
Design of charging pile mqtt transplantation based on 4G EC20 module
yocto 技術分享第四期:自定義增加軟件包支持
My 4G smart charging pile gateway design and development related articles
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
Pycharm cannot import custom package
4G module board level control interface designed by charging pile
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
20220604数学:x的平方根
使用sed替换文件夹下文件
When the reference is assigned to auto
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
Screen display of charging pile design -- led driver ta6932
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
1. Finite Markov Decision Process