change the way of auto login to auto login at welcome screen with nice login prompt
parent
ecd1e8d052
commit
83b02b3e60
|
@ -5,7 +5,7 @@ import './constants.dart';
|
|||
|
||||
ThemeData lightThemeData(BuildContext context) {
|
||||
return ThemeData(
|
||||
// useMaterial3: true,
|
||||
useMaterial3: true,
|
||||
primaryColor: kPrimaryColor,
|
||||
scaffoldBackgroundColor: Colors.white,
|
||||
appBarTheme: appBarThemeLight,
|
||||
|
@ -33,7 +33,7 @@ ThemeData lightThemeData(BuildContext context) {
|
|||
|
||||
ThemeData darkThemeData(BuildContext context) {
|
||||
return ThemeData(
|
||||
// useMaterial3: true,
|
||||
useMaterial3: true,
|
||||
primaryColor: kPrimaryColor,
|
||||
scaffoldBackgroundColor: Colors.black,
|
||||
appBarTheme: appBarThemeDark,
|
||||
|
|
|
@ -54,11 +54,15 @@ class _SignTextFieldState extends State<SignTextField> {
|
|||
await hasAccountExisted(widget.type, widget.controller.text);
|
||||
if (widget.type == 'username') {
|
||||
setState(() {
|
||||
res['code'] == 10100 ? _errorText = null : _errorText = '用户名已存在';
|
||||
res['code'] == 10100
|
||||
? _errorText = null
|
||||
: _errorText = '用户名已存在';
|
||||
});
|
||||
} else {
|
||||
setState(() {
|
||||
res['code'] == 10100 ? _errorText = null : _errorText = '邮箱已被使用';
|
||||
res['code'] == 10100
|
||||
? _errorText = null
|
||||
: _errorText = '邮箱已被使用';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -190,41 +194,89 @@ class _SignTextFieldState extends State<SignTextField> {
|
|||
}
|
||||
}
|
||||
|
||||
TextField textField({
|
||||
Color color = kPrimaryColor,
|
||||
bool obscureText = false,
|
||||
required String labelText,
|
||||
required TextEditingController controller,
|
||||
}) {
|
||||
return TextField(
|
||||
controller: controller,
|
||||
obscureText: obscureText,
|
||||
cursorColor: kSecondaryColor,
|
||||
textAlignVertical: TextAlignVertical.bottom,
|
||||
decoration: InputDecoration(
|
||||
helperText: '5-10位',
|
||||
errorText: 'xxx',
|
||||
labelText: labelText,
|
||||
floatingLabelStyle: const TextStyle(color: kSecondaryColor),
|
||||
focusedBorder:
|
||||
UnderlineInputBorder(borderSide: BorderSide(color: color))),
|
||||
);
|
||||
class CommonTextField extends StatelessWidget {
|
||||
const CommonTextField({
|
||||
super.key,
|
||||
this.color = kPrimaryColor,
|
||||
this.obscureText = false,
|
||||
required this.labelText,
|
||||
required this.controller,
|
||||
});
|
||||
|
||||
final Color color;
|
||||
final bool obscureText;
|
||||
final String labelText;
|
||||
final TextEditingController controller;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return TextField(
|
||||
controller: controller,
|
||||
obscureText: obscureText,
|
||||
cursorColor: kSecondaryColor,
|
||||
textAlignVertical: TextAlignVertical.bottom,
|
||||
decoration: InputDecoration(
|
||||
helperText: '5-10位',
|
||||
errorText: 'xxx',
|
||||
labelText: labelText,
|
||||
floatingLabelStyle: const TextStyle(color: kSecondaryColor),
|
||||
focusedBorder:
|
||||
UnderlineInputBorder(borderSide: BorderSide(color: color))),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
ElevatedButton elevatedButton(
|
||||
{required VoidCallback onPressed,
|
||||
required String text,
|
||||
Color? color = kPrimaryColor}) {
|
||||
return ElevatedButton(
|
||||
onPressed: onPressed,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: color,
|
||||
elevation: 0,
|
||||
fixedSize: const Size(150, 20),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))),
|
||||
child: Text(
|
||||
text,
|
||||
style: const TextStyle(fontSize: 20.0, letterSpacing: 10),
|
||||
),
|
||||
);
|
||||
// TextField textField({
|
||||
// Color color = kPrimaryColor,
|
||||
// bool obscureText = false,
|
||||
// required String labelText,
|
||||
// required TextEditingController controller,
|
||||
// }) {
|
||||
// return TextField(
|
||||
// controller: controller,
|
||||
// obscureText: obscureText,
|
||||
// cursorColor: kSecondaryColor,
|
||||
// textAlignVertical: TextAlignVertical.bottom,
|
||||
// decoration: InputDecoration(
|
||||
// helperText: '5-10位',
|
||||
// errorText: 'xxx',
|
||||
// labelText: labelText,
|
||||
// floatingLabelStyle: const TextStyle(color: kSecondaryColor),
|
||||
// focusedBorder:
|
||||
// UnderlineInputBorder(borderSide: BorderSide(color: color))),
|
||||
// );
|
||||
// }
|
||||
|
||||
class CommonElevatedButton extends StatelessWidget {
|
||||
const CommonElevatedButton({
|
||||
super.key,
|
||||
required this.onPressed,
|
||||
required this.text,
|
||||
this.color = kPrimaryColor,
|
||||
});
|
||||
|
||||
final VoidCallback onPressed;
|
||||
final String text;
|
||||
final Color color;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ElevatedButton(
|
||||
onPressed: onPressed,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: color,
|
||||
elevation: 0,
|
||||
fixedSize: const Size(150, 20),
|
||||
shape:
|
||||
RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))),
|
||||
child: Text(
|
||||
text,
|
||||
style: const TextStyle(
|
||||
fontSize: 20.0,
|
||||
letterSpacing: 10,
|
||||
color: kContentColorDark,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,11 +1,15 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
|
||||
import 'package:together_mobile/common/theme.dart';
|
||||
import 'package:together_mobile/database/hive_database.dart';
|
||||
import 'package:together_mobile/router/router.dart';
|
||||
import 'package:together_mobile/models/init_get_it.dart';
|
||||
import 'notification_api.dart';
|
||||
|
||||
final easyLoading = EasyLoading.init();
|
||||
|
||||
void main() async {
|
||||
initGetIt();
|
||||
await NotificationAPI.init();
|
||||
|
@ -27,7 +31,8 @@ class Together extends StatelessWidget {
|
|||
// This builder is used to dismiss keyboard while tap anywhere of the
|
||||
// screen excluding the input widgets.
|
||||
builder: (context, child) {
|
||||
return GestureDetector(
|
||||
child = easyLoading(context, child);
|
||||
child = GestureDetector(
|
||||
onTap: () {
|
||||
if (FocusManager.instance.primaryFocus != null) {
|
||||
FocusManager.instance.primaryFocus!.unfocus();
|
||||
|
@ -35,6 +40,7 @@ class Together extends StatelessWidget {
|
|||
},
|
||||
child: child,
|
||||
);
|
||||
return child;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
@ -46,8 +46,6 @@ var request = Dio(baseOptions)
|
|||
}
|
||||
},
|
||||
onError: (DioException e, ErrorInterceptorHandler handler) {
|
||||
print('错误信息:' + e.response?.data);
|
||||
print('错误码: ${e.response?.statusCode}');
|
||||
handler.reject(e);
|
||||
},
|
||||
),
|
||||
|
|
|
@ -36,13 +36,18 @@ final GoRouter router = GoRouter(
|
|||
redirect: (context, state) async {
|
||||
await getIt.get<Token>().init();
|
||||
if (getIt.get<Token>().token.isNotEmpty) {
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
|
|||
|
||||
import 'package:cherry_toast/cherry_toast.dart';
|
||||
|
||||
import 'common_widgets.dart';
|
||||
import '../../../components/common_widgets.dart';
|
||||
import 'package:together_mobile/common/constants.dart';
|
||||
import 'package:together_mobile/request/signup_signin.dart';
|
||||
|
||||
|
@ -92,7 +92,7 @@ class _EmailSigninBodyState extends State<EmailSigninBody> {
|
|||
width: double.infinity,
|
||||
height: kDefaultPadding,
|
||||
),
|
||||
elevatedButton(
|
||||
CommonElevatedButton(
|
||||
onPressed: confirm,
|
||||
text: '确定',
|
||||
),
|
||||
|
|
|
@ -4,7 +4,7 @@ import 'package:flutter/material.dart';
|
|||
|
||||
import 'package:cherry_toast/cherry_toast.dart';
|
||||
|
||||
import 'common_widgets.dart';
|
||||
import '../../../components/common_widgets.dart';
|
||||
import 'package:together_mobile/common/constants.dart';
|
||||
import 'package:together_mobile/request/signup_signin.dart' as signup_request;
|
||||
|
||||
|
@ -135,7 +135,7 @@ class _SignupBodyState extends State<SignupBody> {
|
|||
],
|
||||
),
|
||||
),
|
||||
elevatedButton(
|
||||
CommonElevatedButton(
|
||||
onPressed: () {
|
||||
_confirm(context);
|
||||
},
|
||||
|
|
|
@ -3,10 +3,11 @@ import 'package:flutter/material.dart';
|
|||
import 'package:cherry_toast/cherry_toast.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
|
||||
import 'package:together_mobile/database/hive_database.dart';
|
||||
import 'package:together_mobile/models/token_model.dart';
|
||||
|
||||
import 'common_widgets.dart';
|
||||
import '../../../components/common_widgets.dart';
|
||||
import 'package:together_mobile/common/constants.dart';
|
||||
import 'package:together_mobile/request/signup_signin.dart';
|
||||
import 'package:together_mobile/models/init_get_it.dart';
|
||||
|
@ -49,19 +50,23 @@ class _UsernameSigninBodyState extends State<UsernameSigninBody> {
|
|||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Stack(
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
const SizedBox(
|
||||
width: double.infinity,
|
||||
height: 100,
|
||||
),
|
||||
if (_isProgressShow)
|
||||
const CircularProgressIndicator(
|
||||
color: kPrimaryColor,
|
||||
strokeWidth: 6.0,
|
||||
),
|
||||
],
|
||||
// Stack(
|
||||
// alignment: Alignment.center,
|
||||
// children: [
|
||||
// const SizedBox(
|
||||
// width: double.infinity,
|
||||
// height: 100,
|
||||
// ),
|
||||
// if (_isProgressShow)
|
||||
// const CircularProgressIndicator(
|
||||
// color: kPrimaryColor,
|
||||
// strokeWidth: 6.0,
|
||||
// ),
|
||||
// ],
|
||||
// ),
|
||||
const SizedBox(
|
||||
width: double.infinity,
|
||||
height: 100,
|
||||
),
|
||||
SizedBox(
|
||||
width: 250,
|
||||
|
@ -92,7 +97,7 @@ class _UsernameSigninBodyState extends State<UsernameSigninBody> {
|
|||
height: kDefaultPadding,
|
||||
width: double.infinity,
|
||||
),
|
||||
elevatedButton(
|
||||
CommonElevatedButton(
|
||||
onPressed: () {
|
||||
_signin(context);
|
||||
},
|
||||
|
@ -116,18 +121,29 @@ class _UsernameSigninBodyState extends State<UsernameSigninBody> {
|
|||
if (!_isProgressShow) _isProgressShow = true;
|
||||
});
|
||||
|
||||
EasyLoading.showInfo('正在登录中......');
|
||||
|
||||
late Map<String, dynamic> result;
|
||||
|
||||
if (_isProgressShow && !_isHttpSend) {
|
||||
_isHttpSend = true;
|
||||
result = await signinByUsername(
|
||||
usernameController.text, passwordController.text);
|
||||
try {
|
||||
result = await signinByUsername(
|
||||
usernameController.text,
|
||||
passwordController.text,
|
||||
);
|
||||
} catch (e) {
|
||||
EasyLoading.showError('连接服务器失败,请稍后重试...');
|
||||
return;
|
||||
} finally {
|
||||
setState(() {
|
||||
_isHttpSend = false;
|
||||
_isProgressShow = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isHttpSend = false;
|
||||
_isProgressShow = false;
|
||||
});
|
||||
|
||||
|
||||
if (result['code'] == 10200) {
|
||||
getIt<UserAccount>().init(result['data']);
|
||||
|
@ -141,6 +157,7 @@ class _UsernameSigninBodyState extends State<UsernameSigninBody> {
|
|||
getIt.get<Token>().updateToken(res['token']);
|
||||
|
||||
await HiveDatabase.init();
|
||||
EasyLoading.dismiss();
|
||||
// ignore: use_build_context_synchronously
|
||||
context.go('/chat');
|
||||
} else {
|
||||
|
|
|
@ -31,7 +31,11 @@ class _SigninScreenState extends State<SigninScreen> {
|
|||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () => context.pop(),
|
||||
),
|
||||
title: _method == 'username' ? const Text('用户名登录') : const Text('邮箱注册'),
|
||||
title: _method == 'username'
|
||||
? const Text(
|
||||
'用户名登录',
|
||||
)
|
||||
: const Text('邮箱登录'),
|
||||
centerTitle: true,
|
||||
actions: [
|
||||
TextButton(
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
|
||||
import 'package:together_mobile/common/constants.dart';
|
||||
import 'package:together_mobile/screens/signin_signup/components/common_widgets.dart'
|
||||
show elevatedButton;
|
||||
import '../../common/constants.dart';
|
||||
import '../../components/common_widgets.dart' show CommonElevatedButton;
|
||||
import '../../database/hive_database.dart';
|
||||
import '../../models/init_get_it.dart';
|
||||
import '../../models/token_model.dart';
|
||||
import '../../models/user_model.dart';
|
||||
import '../../request/signup_signin.dart';
|
||||
|
||||
class WelcomeScreen extends StatefulWidget {
|
||||
const WelcomeScreen({super.key});
|
||||
|
@ -14,27 +19,77 @@ class WelcomeScreen extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _WelcomeScreenState extends State<WelcomeScreen> {
|
||||
Future<int> _tryLoginUseToken() async {
|
||||
await getIt.get<Token>().init();
|
||||
if (getIt.get<Token>().token.isNotEmpty) {
|
||||
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 10200;
|
||||
} else {
|
||||
getIt.get<Token>().clear();
|
||||
return 9999;
|
||||
}
|
||||
} catch (e) {
|
||||
return 500;
|
||||
}
|
||||
} else {
|
||||
return 200;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: SafeArea(
|
||||
child: Column(
|
||||
children: [
|
||||
const SizedBox(
|
||||
height: kDefaultPadding,
|
||||
),
|
||||
Image.asset('assets/images/welcome_image.png'),
|
||||
elevatedButton(
|
||||
onPressed: () => context.push('/signin'),
|
||||
text: '登录',
|
||||
),
|
||||
elevatedButton(
|
||||
onPressed: () => context.push('/signup'),
|
||||
text: '注册',
|
||||
color: kSecondaryColor,
|
||||
),
|
||||
],
|
||||
),
|
||||
child: FutureBuilder<int>(
|
||||
future: _tryLoginUseToken(),
|
||||
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
int? data = snapshot.data;
|
||||
if (data == 10200) {
|
||||
EasyLoading.showSuccess(
|
||||
'登录成功!',
|
||||
duration: const Duration(milliseconds: 500),
|
||||
);
|
||||
context.pushNamed('Chat');
|
||||
} else if (data == 9999) {
|
||||
EasyLoading.showInfo(
|
||||
'登录状态已过期,请重新登录!',
|
||||
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,
|
||||
),
|
||||
],
|
||||
);
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
16
pubspec.lock
16
pubspec.lock
|
@ -390,6 +390,14 @@ packages:
|
|||
url: "https://pub.flutter-io.cn"
|
||||
source: hosted
|
||||
version: "1.0.3"
|
||||
flutter_easyloading:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: flutter_easyloading
|
||||
sha256: ba21a3c883544e582f9cc455a4a0907556714e1e9cf0eababfcb600da191d17c
|
||||
url: "https://pub.flutter-io.cn"
|
||||
source: hosted
|
||||
version: "3.0.5"
|
||||
flutter_image_compress:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -542,6 +550,14 @@ packages:
|
|||
url: "https://pub.flutter-io.cn"
|
||||
source: hosted
|
||||
version: "3.1.0"
|
||||
flutter_spinkit:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: flutter_spinkit
|
||||
sha256: b39c753e909d4796906c5696a14daf33639a76e017136c8d82bf3e620ce5bb8e
|
||||
url: "https://pub.flutter-io.cn"
|
||||
source: hosted
|
||||
version: "5.2.0"
|
||||
flutter_test:
|
||||
dependency: "direct dev"
|
||||
description: flutter
|
||||
|
|
|
@ -39,6 +39,7 @@ dependencies:
|
|||
fast_rsa: ^3.5.7
|
||||
flutter:
|
||||
sdk: flutter
|
||||
flutter_easyloading: ^3.0.5
|
||||
flutter_local_notifications: ^17.0.0
|
||||
flutter_pickers: ^2.1.9
|
||||
flutter_secure_storage: ^9.0.0
|
||||
|
|
Loading…
Reference in New Issue