pikapika/lib/screens/AccountScreen.dart

241 lines
6.6 KiB
Dart
Raw Normal View History

2022-06-29 19:02:01 +00:00
import 'dart:async';
2021-09-29 23:57:09 +00:00
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/basic/Common.dart';
import 'package:pikapika/basic/Method.dart';
2022-07-09 07:11:26 +00:00
import 'package:pikapika/basic/config/IsPro.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/basic/enum/ErrorTypes.dart';
2022-09-05 04:23:53 +00:00
import 'package:pikapika/screens/AboutScreen.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/screens/RegisterScreen.dart';
2022-05-14 13:45:00 +00:00
import 'package:pikapika/screens/SettingsScreen.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/screens/components/NetworkSetting.dart';
2021-09-29 23:57:09 +00:00
2023-01-31 10:50:51 +00:00
import '../basic/config/IconLoading.dart';
2022-09-05 03:05:46 +00:00
import '../basic/config/Version.dart';
2021-09-29 23:57:09 +00:00
import 'AppScreen.dart';
import 'DownloadListScreen.dart';
import 'ForgotPasswordScreen.dart';
2021-09-29 23:57:09 +00:00
import 'components/ContentLoading.dart';
// 账户设置
class AccountScreen extends StatefulWidget {
2022-03-17 03:31:25 +00:00
const AccountScreen({Key? key}) : super(key: key);
2021-09-29 23:57:09 +00:00
@override
_AccountScreenState createState() => _AccountScreenState();
}
class _AccountScreenState extends State<AccountScreen> {
late bool _logging = false;
late String _username = "";
late String _password = "";
2022-06-30 16:31:15 +00:00
late StreamSubscription<String?> _linkSubscription;
2021-09-29 23:57:09 +00:00
@override
void initState() {
2022-07-01 05:39:08 +00:00
_linkSubscription = linkSubscript(context);
2021-09-29 23:57:09 +00:00
_loadProperties();
super.initState();
2022-10-14 17:54:50 +00:00
Future.delayed(Duration.zero, () async {
versionPop(context);
versionEvent.subscribe(_versionSub);
});
2021-09-29 23:57:09 +00:00
}
2022-06-29 19:02:01 +00:00
@override
void dispose() {
_linkSubscription.cancel();
2022-09-05 03:05:46 +00:00
versionEvent.unsubscribe(_versionSub);
2022-06-29 19:02:01 +00:00
super.dispose();
}
2022-09-05 03:05:46 +00:00
_versionSub(_) {
versionPop(context);
}
2021-09-29 23:57:09 +00:00
Future _loadProperties() async {
var username = await method.getUsername();
var password = await method.getPassword();
setState(() {
_username = username;
_password = password;
});
}
@override
Widget build(BuildContext context) {
if (_logging) {
return _buildLogging();
}
return _buildGui();
}
Widget _buildLogging() {
2022-03-25 14:57:30 +00:00
return const Scaffold(
2021-09-29 23:57:09 +00:00
body: ContentLoading(label: '登录中'),
);
}
Widget _buildGui() {
return Scaffold(
appBar: AppBar(
2022-03-25 14:57:30 +00:00
title: const Text('配置选项'),
2021-09-29 23:57:09 +00:00
actions: [
2022-05-14 13:45:00 +00:00
IconButton(
onPressed: () {
Navigator.push(
context,
2023-01-31 10:50:51 +00:00
mixRoute(
2022-05-14 13:45:00 +00:00
builder: (context) => const SettingsScreen(
hiddenAccountInfo: true,
),
),
);
},
icon: const Text('设置'),
),
2022-09-05 04:23:53 +00:00
IconButton(
onPressed: () {
Navigator.push(
context,
2023-01-31 10:50:51 +00:00
mixRoute(
builder: (context) => const AboutScreen(),
2022-09-05 04:23:53 +00:00
),
);
},
icon: const Text('关于'),
),
2021-09-29 23:57:09 +00:00
IconButton(
onPressed: _toDownloadList,
2022-03-19 04:12:27 +00:00
icon: const Icon(Icons.download_rounded),
2021-09-29 23:57:09 +00:00
),
IconButton(
onPressed: _logIn,
2022-03-19 04:12:27 +00:00
icon: const Icon(Icons.save),
2021-09-29 23:57:09 +00:00
),
],
),
body: ListView(
children: [
ListTile(
2022-03-19 04:12:27 +00:00
title: const Text("账号"),
2021-09-29 23:57:09 +00:00
subtitle: Text(_username == "" ? "未设置" : _username),
onTap: () async {
String? input = await displayTextInputDialog(
context,
2021-12-15 04:22:40 +00:00
src: _username,
title: '账号',
hint: '请输入账号',
2021-09-29 23:57:09 +00:00
);
if (input != null) {
await method.setUsername(input);
setState(() {
_username = input;
});
}
},
),
ListTile(
2022-03-19 04:12:27 +00:00
title: const Text("密码"),
2021-12-15 15:44:05 +00:00
subtitle: Text(_password == "" ? "未设置" : '\u2022' * 10),
2021-09-29 23:57:09 +00:00
onTap: () async {
String? input = await displayTextInputDialog(
context,
2021-12-15 04:22:40 +00:00
src: _password,
title: '密码',
hint: '请输入密码',
isPasswd: true,
2021-09-29 23:57:09 +00:00
);
if (input != null) {
await method.setPassword(input);
setState(() {
_password = input;
});
}
},
),
2022-03-19 04:12:27 +00:00
const NetworkSetting(),
Container(
padding: const EdgeInsets.all(15),
child: Text.rich(TextSpan(
text: '没有账号,我要注册',
style: TextStyle(
color: Theme.of(context).colorScheme.secondary,
decoration: TextDecoration.underline,
),
recognizer: TapGestureRecognizer()
..onTap = () => Navigator.push(
context,
mixRoute(
builder: (BuildContext context) =>
const RegisterScreen()),
).then((value) => _loadProperties()),
)),
),
Container(
padding: const EdgeInsets.all(15),
child: Text.rich(TextSpan(
text: '密码找回',
style: TextStyle(
color: Theme.of(context).colorScheme.secondary,
decoration: TextDecoration.underline,
2021-09-29 23:57:09 +00:00
),
recognizer: TapGestureRecognizer()
..onTap = () => Navigator.push(
context,
mixRoute(
builder: (BuildContext context) =>
const ForgotPasswordScreen()),
).then((value) => _loadProperties()),
)),
2021-09-29 23:57:09 +00:00
),
],
),
);
}
_logIn() async {
setState(() {
_logging = true;
});
try {
await method.login();
2022-07-09 07:11:26 +00:00
await reloadIsPro();
2021-09-29 23:57:09 +00:00
Navigator.pushReplacement(
context,
2023-01-31 10:50:51 +00:00
mixRoute(builder: (context) => const AppScreen()),
2021-09-29 23:57:09 +00:00
);
} catch (e, s) {
print("$e\n$s");
setState(() {
_logging = false;
});
2022-02-28 08:46:31 +00:00
var message = "请检查账号密码或网络环境";
2021-10-22 02:24:39 +00:00
switch (errorType("$e")) {
case ERROR_TYPE_NETWORK:
message = "网络不通";
break;
case ERROR_TYPE_TIME:
message = "请检查设备时间";
break;
}
2022-02-28 08:46:31 +00:00
if ("$e".contains("email") && "$e".contains("password")) {
2023-02-16 08:40:26 +00:00
message = "账号或密码错误";
2022-02-28 08:46:31 +00:00
}
2021-09-29 23:57:09 +00:00
alertDialog(
context,
'登录失败',
2021-10-22 02:24:39 +00:00
"$message\n$e",
2021-09-29 23:57:09 +00:00
);
}
}
_toDownloadList() {
Navigator.push(
context,
2023-01-31 10:50:51 +00:00
mixRoute(builder: (context) => const DownloadListScreen()),
2021-09-29 23:57:09 +00:00
);
}
}