optimize auto login
parent
83b02b3e60
commit
9983222f4c
|
@ -31,27 +31,9 @@ final GoRouter router = GoRouter(
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: '/welcome',
|
path: '/welcome',
|
||||||
name: 'Welcome',
|
name: 'Welcome',
|
||||||
builder: (BuildContext context, GoRouterState state) =>
|
builder: (BuildContext context, GoRouterState state) {
|
||||||
const WelcomeScreen(),
|
String? isLogout = state.uri.queryParameters['isLogout'];
|
||||||
redirect: (context, state) async {
|
return WelcomeScreen(isLogout: isLogout);
|
||||||
await getIt.get<Token>().init();
|
|
||||||
if (getIt.get<Token>().token.isNotEmpty) {
|
|
||||||
print(1111111);
|
|
||||||
try {
|
|
||||||
Map<String, dynamic> res = await signinByToken();
|
|
||||||
if (res['code'] == 10200) {
|
|
||||||
await getIt.get<Token>().updateToken(res['token']);
|
|
||||||
getIt.get<UserAccount>().init(res['data']);
|
|
||||||
await HiveDatabase.init();
|
|
||||||
return '/chat';
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
GoRoute(
|
GoRoute(
|
||||||
|
@ -63,10 +45,12 @@ final GoRouter router = GoRouter(
|
||||||
child: const SignupScreen(),
|
child: const SignupScreen(),
|
||||||
transitionDuration: const Duration(milliseconds: 200),
|
transitionDuration: const Duration(milliseconds: 200),
|
||||||
reverseTransitionDuration: const Duration(milliseconds: 200),
|
reverseTransitionDuration: const Duration(milliseconds: 200),
|
||||||
transitionsBuilder: (BuildContext context,
|
transitionsBuilder: (
|
||||||
Animation<double> animation,
|
BuildContext context,
|
||||||
Animation<double> secondaryAnimation,
|
Animation<double> animation,
|
||||||
Widget child) {
|
Animation<double> secondaryAnimation,
|
||||||
|
Widget child,
|
||||||
|
) {
|
||||||
const begin = Offset(1.0, 0.0);
|
const begin = Offset(1.0, 0.0);
|
||||||
const end = Offset.zero;
|
const end = Offset.zero;
|
||||||
final tween = Tween(begin: begin, end: end);
|
final tween = Tween(begin: begin, end: end);
|
||||||
|
|
|
@ -69,7 +69,7 @@ class _ChatScreenState extends State<ChatScreen> with GetItStateMixin {
|
||||||
await _getUnreceivedMsg(userId);
|
await _getUnreceivedMsg(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Future(() => true);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _getUnreceivedMsg(String userId) async {
|
Future<void> _getUnreceivedMsg(String userId) async {
|
||||||
|
|
|
@ -36,7 +36,7 @@ class SettingScreen extends StatelessWidget {
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
await getIt.get<Token>().clear();
|
await getIt.get<Token>().clear();
|
||||||
// ignore: use_build_context_synchronously
|
// ignore: use_build_context_synchronously
|
||||||
context.go('/welcome');
|
context.goNamed('Welcome', queryParameters: {'isLogout': '1'});
|
||||||
// ignore: use_build_context_synchronously
|
// ignore: use_build_context_synchronously
|
||||||
Timer(
|
Timer(
|
||||||
const Duration(milliseconds: 200),
|
const Duration(milliseconds: 200),
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
@ -12,7 +14,12 @@ import '../../models/user_model.dart';
|
||||||
import '../../request/signup_signin.dart';
|
import '../../request/signup_signin.dart';
|
||||||
|
|
||||||
class WelcomeScreen extends StatefulWidget {
|
class WelcomeScreen extends StatefulWidget {
|
||||||
const WelcomeScreen({super.key});
|
const WelcomeScreen({
|
||||||
|
super.key,
|
||||||
|
this.isLogout,
|
||||||
|
});
|
||||||
|
|
||||||
|
final String? isLogout;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<WelcomeScreen> createState() => _WelcomeScreenState();
|
State<WelcomeScreen> createState() => _WelcomeScreenState();
|
||||||
|
@ -20,8 +27,12 @@ class WelcomeScreen extends StatefulWidget {
|
||||||
|
|
||||||
class _WelcomeScreenState extends State<WelcomeScreen> {
|
class _WelcomeScreenState extends State<WelcomeScreen> {
|
||||||
Future<int> _tryLoginUseToken() async {
|
Future<int> _tryLoginUseToken() async {
|
||||||
|
if (widget.isLogout != null) {
|
||||||
|
return 200;
|
||||||
|
}
|
||||||
await getIt.get<Token>().init();
|
await getIt.get<Token>().init();
|
||||||
if (getIt.get<Token>().token.isNotEmpty) {
|
if (getIt.get<Token>().token.isNotEmpty) {
|
||||||
|
EasyLoading.showInfo('自动登录中...');
|
||||||
try {
|
try {
|
||||||
Map<String, dynamic> res = await signinByToken();
|
Map<String, dynamic> res = await signinByToken();
|
||||||
if (res['code'] == 10200) {
|
if (res['code'] == 10200) {
|
||||||
|
@ -41,55 +52,56 @@ class _WelcomeScreenState extends State<WelcomeScreen> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_tryLoginUseToken().then((value) {
|
||||||
|
if (value == 10200) {
|
||||||
|
EasyLoading.showSuccess(
|
||||||
|
'登录成功!',
|
||||||
|
duration: const Duration(seconds: 3),
|
||||||
|
dismissOnTap: true,
|
||||||
|
);
|
||||||
|
context.pushNamed('Chat');
|
||||||
|
} else if (value == 9999) {
|
||||||
|
EasyLoading.showInfo(
|
||||||
|
'登录状态已过期,请重新登录!',
|
||||||
|
duration: const Duration(seconds: 3),
|
||||||
|
dismissOnTap: true,
|
||||||
|
);
|
||||||
|
} else if (value == 500) {
|
||||||
|
EasyLoading.showError(
|
||||||
|
'连接服务器失败,请稍后再试!',
|
||||||
|
duration: const Duration(seconds: 3),
|
||||||
|
dismissOnTap: true,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
EasyLoading.dismiss();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
body: SafeArea(
|
body: SafeArea(
|
||||||
child: FutureBuilder<int>(
|
child: Column(
|
||||||
future: _tryLoginUseToken(),
|
children: [
|
||||||
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
|
const SizedBox(
|
||||||
if (snapshot.hasData) {
|
height: kDefaultPadding,
|
||||||
int? data = snapshot.data;
|
),
|
||||||
if (data == 10200) {
|
Image.asset('assets/images/welcome_image.png'),
|
||||||
EasyLoading.showSuccess(
|
CommonElevatedButton(
|
||||||
'登录成功!',
|
onPressed: () => context.pushNamed('SignIn'),
|
||||||
duration: const Duration(milliseconds: 500),
|
text: '登录',
|
||||||
);
|
),
|
||||||
context.pushNamed('Chat');
|
CommonElevatedButton(
|
||||||
} else if (data == 9999) {
|
onPressed: () => context.pushNamed('SignUp'),
|
||||||
EasyLoading.showInfo(
|
text: '注册',
|
||||||
'登录状态已过期,请重新登录!',
|
color: kSecondaryColor,
|
||||||
duration: const Duration(milliseconds: 500),
|
),
|
||||||
);
|
],
|
||||||
} else if (data == 500) {
|
),
|
||||||
EasyLoading.showError(
|
|
||||||
'连接服务器失败,请稍后再试!',
|
|
||||||
duration: const Duration(milliseconds: 500),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
EasyLoading.dismiss();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
EasyLoading.show(status: '自动登录中...');
|
|
||||||
}
|
|
||||||
return Column(
|
|
||||||
children: [
|
|
||||||
const SizedBox(
|
|
||||||
height: kDefaultPadding,
|
|
||||||
),
|
|
||||||
Image.asset('assets/images/welcome_image.png'),
|
|
||||||
CommonElevatedButton(
|
|
||||||
onPressed: () => context.push('/signin'),
|
|
||||||
text: '登录',
|
|
||||||
),
|
|
||||||
CommonElevatedButton(
|
|
||||||
onPressed: () => context.push('/signup'),
|
|
||||||
text: '注册',
|
|
||||||
color: kSecondaryColor,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue