flutter_integrated_whb/lib/pages/login_page.dart

452 lines
16 KiB
Dart
Raw Normal View History

2025-08-19 11:06:16 +08:00
import 'package:qhd_prevention/customWidget/custom_alert_dialog.dart';
2025-07-15 08:32:50 +08:00
import 'package:flutter/gestures.dart';
2025-07-11 11:03:21 +08:00
import 'package:flutter/material.dart';
2025-07-15 08:32:50 +08:00
import 'package:fluttertoast/fluttertoast.dart';
2025-08-19 11:06:16 +08:00
import 'package:qhd_prevention/customWidget/toast_util.dart';
2025-09-09 20:08:10 +08:00
import 'package:qhd_prevention/pages/mine/mine_set_pwd_page.dart';
import 'package:qhd_prevention/services/auth_service.dart';
2025-09-18 15:40:01 +08:00
import 'package:qhd_prevention/tools/tools.dart';
import 'package:qhd_prevention/tools/update/update_dialogs.dart';
2025-09-15 17:44:12 +08:00
import 'package:shared_preferences/shared_preferences.dart';
2025-07-15 08:32:50 +08:00
import '../tools/tools.dart';
2025-07-11 11:03:21 +08:00
import 'main_tab.dart';
import 'mine/webViewPage.dart';
2025-07-11 11:03:21 +08:00
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '登录页面',
theme: ThemeData(
primarySwatch: Colors.blue,
inputDecorationTheme: const InputDecorationTheme(
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(horizontal: 8),
),
),
home: const LoginPage(),
debugShowCheckedModeBanner: false,
);
}
}
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
2025-08-19 11:06:16 +08:00
final TextEditingController _phoneController = TextEditingController(
2025-09-02 16:22:17 +08:00
// text: '13293211008',
2025-08-19 11:06:16 +08:00
);
final TextEditingController _passwordController = TextEditingController(
2025-09-02 16:22:17 +08:00
// text: 'Zsaq@123456',
2025-08-19 11:06:16 +08:00
);
2025-07-11 11:03:21 +08:00
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
String _errorMessage = '';
bool _isLoading = false;
bool _obscurePassword = true;
2025-07-15 08:32:50 +08:00
bool _agreed = false;
2025-07-11 11:03:21 +08:00
2025-08-19 11:06:16 +08:00
@override
void initState() {
super.initState();
2025-09-05 09:16:54 +08:00
_phoneController.addListener(_onTextChanged);
2025-09-12 21:04:05 +08:00
2025-09-15 17:44:12 +08:00
_getData();
// _phoneController.text= SessionService.instance.loginPhone ?? "";
// _passwordController.text= SessionService.instance.loginPass?? "";
2025-08-19 11:06:16 +08:00
_checkUpdata();
}
2025-09-15 17:44:12 +08:00
Future<void> _getData() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_phoneController.text= prefs.getString('savePhone') ?? '';
_passwordController.text=prefs.getString('savePass') ?? '';
});
}
Future<void> _saveData(String phone,String pass) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString("savePhone", phone);
await prefs.setString("savePass", pass);
}
2025-09-05 09:16:54 +08:00
void dispose() {
_phoneController.removeListener(_onTextChanged);
_phoneController.dispose();
super.dispose();
}
void _onTextChanged() {
setState(() {}); // 文本变化时刷新UI
}
2025-08-19 11:06:16 +08:00
Future<void> _checkUpdata() async {
2025-09-15 15:54:03 +08:00
try{
final result = await AuthService.checkUpdate();
if (FormUtils.hasValue(result, 'pd')) {
Map pd = result['pd'];
2025-09-18 15:40:01 +08:00
final versionInfo = await getAppVersion();
2025-09-18 21:45:41 +08:00
bool isWifi = true;
2025-09-18 15:40:01 +08:00
if (versionInfo.versionName != pd['VERSION']) {
//有更新 提示更新
final ok = await CustomAlertDialog.showConfirm(
context,
2025-09-18 21:45:41 +08:00
barrierDismissible:false,
2025-09-18 15:40:01 +08:00
title: '更新通知',
content: isWifi ? '发现新版本,是否更新?为了更好的体验,请更新到最新版本。' : '发现新版本,检查到您当前使用的是移动网络,是否更新?更新时请注意流量消耗。为了更好的体验,请更新到最新版本。',
cancelText: pd['ISUPDATE'] == '1' ? '' : '稍后更新',
confirmText: '立即更新'
);
if (ok) {
2025-09-18 21:45:41 +08:00
final apkUrl = pd['FILEURL'] ?? '';
await showUpdateConfirm(context, apkUrl: apkUrl);
2025-09-18 15:40:01 +08:00
}
return;
}
2025-09-15 15:54:03 +08:00
}
}catch(_) {}
2025-08-19 11:06:16 +08:00
}
2025-07-11 11:03:21 +08:00
@override
Widget build(BuildContext context) {
return Scaffold(
2025-07-15 08:32:50 +08:00
resizeToAvoidBottomInset: false,
body: Container(
width: double.infinity,
height: double.infinity,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/bg-login.png'),
fit: BoxFit.cover,
),
),
child: Form(
key: _formKey,
child: Stack(
children: [
Positioned.fill(
child: Image.asset(
'assets/images/bg-login.png',
fit: BoxFit.cover,
2025-07-11 11:03:21 +08:00
),
2025-07-15 08:32:50 +08:00
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 30),
2025-09-15 15:53:47 +08:00
// child: SingleChildScrollView(
2025-07-11 11:03:21 +08:00
child: Column(
children: [
2025-09-15 15:53:47 +08:00
const SizedBox(height: 70),
2025-07-15 08:32:50 +08:00
Row(
children: [
Image.asset('assets/image/logo.png', height: 50),
const SizedBox(width: 15),
const Expanded(
child: Text(
'欢迎使用,\n智守安全云平台!',
style: TextStyle(
2025-09-15 15:53:47 +08:00
fontSize: 20,
2025-07-15 08:32:50 +08:00
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
],
),
const SizedBox(height: 50),
2025-07-11 11:03:21 +08:00
_buildInputSection(
label: "手机号码",
controller: _phoneController,
hintText: "请输入手机号...",
keyboardType: TextInputType.phone,
validator: (value) {
2025-08-19 11:06:16 +08:00
if (value == null || value.isEmpty) return '请输入手机号';
2025-07-15 08:32:50 +08:00
// if (!RegExp(r'^1[3-9]\d{9}\$').hasMatch(value))
// return '请输入有效的手机号';
2025-07-11 11:03:21 +08:00
return null;
},
2025-09-05 09:16:54 +08:00
suffixIcon: _phoneController.text.isEmpty
? const SizedBox(height: 20,)
: IconButton(
icon: Icon(Icons.cancel, size: 20,color: Colors.white,),
onPressed: () => _phoneController.clear(),
),
2025-07-11 11:03:21 +08:00
),
2025-09-05 09:16:54 +08:00
2025-07-11 11:03:21 +08:00
_buildInputSection(
label: "密码",
controller: _passwordController,
hintText: "请输入密码...",
obscureText: _obscurePassword,
suffixIcon: IconButton(
icon: Icon(
_obscurePassword
2025-07-15 08:32:50 +08:00
? Icons.visibility_off
: Icons.visibility,
color: Colors.white,
2025-07-11 11:03:21 +08:00
),
2025-07-15 08:32:50 +08:00
onPressed:
2025-08-19 11:06:16 +08:00
() => setState(
() => _obscurePassword = !_obscurePassword,
2025-07-15 08:32:50 +08:00
),
2025-07-11 11:03:21 +08:00
),
validator: (value) {
2025-08-19 11:06:16 +08:00
if (value == null || value.isEmpty) return '请输入密码';
2025-07-11 11:03:21 +08:00
return null;
},
),
2025-09-05 09:16:54 +08:00
2025-07-15 08:32:50 +08:00
const SizedBox(height: 30),
if (_errorMessage.isNotEmpty)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25),
child: Text(
_errorMessage,
style: const TextStyle(color: Colors.blue),
),
),
2025-09-15 15:53:47 +08:00
2025-07-11 11:03:21 +08:00
Padding(
2025-09-15 15:53:47 +08:00
padding: const EdgeInsets.symmetric(vertical: 0),
2025-07-11 11:03:21 +08:00
child: SizedBox(
width: double.infinity,
height: 48,
child: ElevatedButton(
2025-07-15 08:32:50 +08:00
onPressed:
2025-09-12 21:04:05 +08:00
_handleLogin,
// (!_isLoading && _agreed) ? _handleLogin : null,
2025-07-11 11:03:21 +08:00
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
2025-08-19 11:06:16 +08:00
child: const Text(
2025-07-11 11:03:21 +08:00
'登录',
style: TextStyle(
fontSize: 18,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
),
2025-07-15 08:32:50 +08:00
2025-09-15 15:53:47 +08:00
Spacer(),
2025-07-15 08:32:50 +08:00
Padding(
2025-09-15 15:53:47 +08:00
padding: const EdgeInsets.only(bottom: 10),
2025-07-15 08:32:50 +08:00
child: Row(
children: [
Checkbox(
value: _agreed,
activeColor: Colors.white,
checkColor: Colors.blueAccent,
side: const BorderSide(color: Colors.white),
2025-09-12 21:04:05 +08:00
onChanged:(value) {
setState(() {
_agreed = value??false;
});
},
// (v) => setState(() => _agreed = v ?? false),
2025-07-15 08:32:50 +08:00
),
Expanded(
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: '我已阅读并同意',
style: const TextStyle(
color: Colors.white,
2025-09-15 15:53:47 +08:00
fontSize: 12,
2025-07-15 08:32:50 +08:00
),
),
TextSpan(
text: '《服务协议》',
2025-07-15 08:32:50 +08:00
style: const TextStyle(
color: Color(0xFF0D1D8C),
2025-09-15 15:53:47 +08:00
fontSize: 12,
2025-07-15 08:32:50 +08:00
),
recognizer:
2025-08-19 11:06:16 +08:00
TapGestureRecognizer()
..onTap = () {
2025-08-29 20:33:23 +08:00
pushPage(
const WebViewPage(
name: "用户服务协议",
url:
'http://47.92.102.56:7811/file/xieyi/zsyhxy.htm', // 替换为目标网址
),
context,
);
2025-08-19 11:06:16 +08:00
},
2025-07-15 08:32:50 +08:00
),
TextSpan(
text: '',
style: const TextStyle(
color: Colors.white,
2025-09-15 15:53:47 +08:00
fontSize: 12,
2025-07-15 08:32:50 +08:00
),
),
TextSpan(
text: '《隐私政策》',
style: const TextStyle(
color: Color(0xFF0D1D8C),
2025-09-15 15:53:47 +08:00
fontSize: 12,
2025-07-15 08:32:50 +08:00
),
recognizer:
2025-08-19 11:06:16 +08:00
TapGestureRecognizer()
..onTap = () {
2025-08-29 20:33:23 +08:00
pushPage(
const WebViewPage(
name: "隐私政策",
url:
'http://47.92.102.56:7811/file/xieyi/zsysq.htm', // 替换为目标网址
),
context,
);
2025-08-19 11:06:16 +08:00
},
2025-07-15 08:32:50 +08:00
),
],
),
),
),
],
),
),
2025-07-11 11:03:21 +08:00
],
),
2025-09-15 15:53:47 +08:00
// ),
2025-07-11 11:03:21 +08:00
),
],
),
),
2025-07-15 08:32:50 +08:00
),
2025-07-11 11:03:21 +08:00
);
}
2025-07-15 08:32:50 +08:00
2025-07-11 11:03:21 +08:00
Widget _buildInputSection({
required String label,
required TextEditingController controller,
required String hintText,
bool obscureText = false,
TextInputType keyboardType = TextInputType.text,
Widget? suffixIcon,
String? Function(String?)? validator,
}) {
return Padding(
2025-07-15 08:32:50 +08:00
padding: const EdgeInsets.symmetric(vertical: 12),
2025-07-11 11:03:21 +08:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: const TextStyle(
2025-09-15 15:53:47 +08:00
fontSize: 15,
2025-07-11 11:03:21 +08:00
fontWeight: FontWeight.w500,
2025-07-15 08:32:50 +08:00
color: Colors.white,
2025-07-11 11:03:21 +08:00
),
),
2025-09-05 09:16:54 +08:00
const SizedBox(height: 0),
2025-07-11 11:03:21 +08:00
TextFormField(
controller: controller,
obscureText: obscureText,
keyboardType: keyboardType,
validator: validator,
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
hintText: hintText,
2025-07-15 08:32:50 +08:00
hintStyle: const TextStyle(color: Colors.white70),
2025-07-11 11:03:21 +08:00
suffixIcon: suffixIcon,
isDense: true,
contentPadding: EdgeInsets.zero,
),
2025-07-15 08:32:50 +08:00
style: const TextStyle(color: Colors.white),
2025-07-11 11:03:21 +08:00
),
2025-09-05 09:16:54 +08:00
const Divider(
height: 1,
thickness: 1,
color: Colors.white70,
),
2025-07-11 11:03:21 +08:00
],
),
);
}
2025-07-15 08:32:50 +08:00
Future<void> _handleLogin() async {
2025-09-12 21:04:05 +08:00
if(_isLoading){
return;
}
2025-07-15 08:32:50 +08:00
if (!(_formKey.currentState?.validate() ?? false)) return;
2025-09-12 21:04:05 +08:00
if(!_agreed){
ToastUtil.showNormal(context, "请先阅读并同意《服务协议》和《隐私政策》");
return;
}
2025-07-15 08:32:50 +08:00
final userName = _phoneController.text.trim();
final userPwd = _passwordController.text;
2025-07-11 11:03:21 +08:00
2025-09-15 17:44:12 +08:00
_saveData(userName,userPwd);
// SessionService.instance.setSavePhone(userName);
// SessionService.instance.setSavePass(userPwd);
2025-09-12 21:04:05 +08:00
2025-07-15 08:32:50 +08:00
setState(() => _isLoading = true);
showDialog(
context: context,
barrierDismissible: false,
builder: (_) => const Center(child: CircularProgressIndicator()),
2025-07-11 11:03:21 +08:00
);
2025-07-15 08:32:50 +08:00
try {
2025-09-09 20:08:10 +08:00
final data = await AuthService.login(userName, userPwd);
2025-07-15 08:32:50 +08:00
2025-08-19 11:06:16 +08:00
Navigator.of(context).pop(); // 关loading
2025-07-15 08:32:50 +08:00
setState(() => _isLoading = false);
2025-09-09 20:08:10 +08:00
if(data.isEmpty){
return;
}
if (data['result'] == 'success') {
if(data['WEAK_PASSWORD']=='1'){
pushPage(const MineSetPwdPage("1"), context);
}else if(data['LONG_TERM_PASSWORD_NOT_CHANGED']=='1'){
pushPage(const MineSetPwdPage("2"), context);
}else{
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (_) => const MainPage()),
);
}
2025-08-19 11:06:16 +08:00
}
2025-07-15 08:32:50 +08:00
} catch (e) {
Navigator.of(context).pop();
setState(() => _isLoading = false);
2025-08-19 11:06:16 +08:00
Fluttertoast.showToast(msg: '登录失败: $e');
2025-07-15 08:32:50 +08:00
}
2025-07-11 11:03:21 +08:00
}
2025-08-19 11:06:16 +08:00
}