first commit

This commit is contained in:
2026-05-22 22:00:37 +08:00
commit fee7291ab9
152 changed files with 8954 additions and 0 deletions

92
lib/pages/about_page.dart Normal file
View File

@@ -0,0 +1,92 @@
import 'package:flutter/material.dart';
import '../config/strings.dart';
class AboutPage extends StatelessWidget {
const AboutPage({super.key});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 500),
child: Column(
children: [
const SizedBox(height: 32),
Icon(Icons.qr_code_scanner, size: 64, color: theme.colorScheme.primary),
const SizedBox(height: 16),
Text(
AppStrings.aboutTitle,
style: theme.textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text(
AppStrings.aboutDesc,
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppStrings.credits,
style: theme.textTheme.labelLarge?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 12),
_creditRow(theme, AppStrings.creditBuiltWith, 'Flutter'),
_creditRow(theme, AppStrings.creditQRDecode, 'jsQR (web)'),
_creditRow(theme, AppStrings.creditIconAssets, 'assets2.lxns.net'),
_creditRow(theme, AppStrings.creditLicense, 'MIT'),
],
),
),
),
],
),
),
);
}
Widget _creditRow(ThemeData theme, String label, String value) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Row(
children: [
SizedBox(
width: 120,
child: Text(
label,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
),
Expanded(
child: Text(value, style: theme.textTheme.bodyMedium),
),
],
),
);
}
}

651
lib/pages/home_page.dart Normal file
View File

@@ -0,0 +1,651 @@
import 'package:flutter/material.dart';
import '../config/strings.dart';
import '../config/title_server_config.dart';
import '../models/user_preview.dart';
import '../services/title_api_service.dart';
import 'about_page.dart';
import 'settings_page.dart';
import 'ticket_page.dart';
class HomePage extends StatefulWidget {
final int userId;
final String token;
const HomePage({
super.key,
required this.userId,
required this.token,
});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _currentTab = 0;
UserPreviewDataBean? _data;
String? _error;
bool _loading = true;
static const _tabTitles = [AppStrings.tabHome, AppStrings.tabTickets, AppStrings.tabSettings, AppStrings.tabAbout];
@override
void initState() {
super.initState();
_loadPreview();
}
Future<void> _loadPreview() async {
// ignore: avoid_print
print('[_loadPreview] called, isConfigured=${TitleServerConfigHolder().isConfigured}');
if (!TitleServerConfigHolder().isConfigured) {
// ignore: avoid_print
print('[_loadPreview] config not set, bailing');
setState(() {
_loading = false;
_error = null;
});
return;
}
setState(() {
_loading = true;
_error = null;
});
try {
// ignore: avoid_print
print('[_loadPreview] calling getUserPreview userId=${widget.userId} token=${widget.token}');
final service = TitleApiService(TitleServerConfigHolder().config!);
final data = await service.getUserPreview(
userId: widget.userId,
token: widget.token,
);
if (!mounted) return;
// ignore: avoid_print
print('[_loadPreview] success, errorId=${data.errorId}');
setState(() {
_data = data;
_loading = false;
});
} on TitleApiException catch (e) {
if (!mounted) return;
// ignore: avoid_print
print('[_loadPreview] TitleApiException: $e');
setState(() {
_error = e.message;
_loading = false;
});
} catch (e) {
if (!mounted) return;
// ignore: avoid_print
print('[_loadPreview] exception: $e');
setState(() {
_error = e.toString();
_loading = false;
});
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Scaffold(
appBar: AppBar(
title: Text(_tabTitles[_currentTab]),
actions: [
IconButton(
icon: const Icon(Icons.logout),
tooltip: AppStrings.logoutTooltip,
onPressed: () => Navigator.of(context).pop(),
),
],
),
body: IndexedStack(
index: _currentTab,
children: [
_buildBody(theme),
TicketPage(userId: widget.userId, token: widget.token),
SettingsPage(showAppBar: false),
const AboutPage(),
],
),
bottomNavigationBar: NavigationBar(
selectedIndex: _currentTab,
onDestinationSelected: (i) => setState(() => _currentTab = i),
destinations: const [
NavigationDestination(icon: Icon(Icons.home_outlined), selectedIcon: Icon(Icons.home), label: AppStrings.tabHome),
NavigationDestination(icon: Icon(Icons.confirmation_number_outlined), selectedIcon: Icon(Icons.confirmation_number), label: AppStrings.tabTickets),
NavigationDestination(icon: Icon(Icons.settings_outlined), selectedIcon: Icon(Icons.settings), label: AppStrings.tabSettings),
NavigationDestination(icon: Icon(Icons.info_outlined), selectedIcon: Icon(Icons.info), label: AppStrings.tabAbout),
],
),
);
}
Widget _buildBody(ThemeData theme) {
if (!TitleServerConfigHolder().isConfigured) {
return Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.settings_ethernet, size: 64, color: theme.colorScheme.primary),
const SizedBox(height: 16),
Text(
AppStrings.titleServerNotConfigured,
style: theme.textTheme.titleMedium,
),
const SizedBox(height: 8),
Text(
AppStrings.titleServerNotConfiguredDesc,
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
FilledButton.icon(
onPressed: () => setState(() => _currentTab = 2),
icon: const Icon(Icons.settings, size: 20),
label: const Text(AppStrings.openSettings),
),
],
),
),
);
}
if (_loading) {
return const Center(child: CircularProgressIndicator());
}
if (_error != null) {
return Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.error_outline, size: 48, color: theme.colorScheme.error),
const SizedBox(height: 12),
Text(AppStrings.loadFailed, style: theme.textTheme.titleMedium),
const SizedBox(height: 8),
Text(
_error!,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
fontFamily: 'monospace',
),
textAlign: TextAlign.center,
),
const SizedBox(height: 20),
FilledButton.icon(
onPressed: _loadPreview,
icon: const Icon(Icons.refresh, size: 20),
label: const Text(AppStrings.retry),
),
],
),
),
);
}
final data = _data!;
return SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 500),
child: Column(
children: [
if (data.errorId != 0) _ErrorIdBanner(theme: theme, errorId: data.errorId),
_ProfileCard(theme: theme, data: data),
const SizedBox(height: 12),
_GameInfoCard(theme: theme, data: data),
const SizedBox(height: 12),
_StatusCard(theme: theme, data: data),
const SizedBox(height: 12),
_DetailCard(theme: theme, data: data),
if (TitleApiService.lastRawResponse != null) ...[
const SizedBox(height: 12),
_DebugRawJsonCard(
theme: theme,
rawJson: TitleApiService.lastRawResponse!,
),
],
],
),
),
);
}
}
class _ProfileCard extends StatelessWidget {
final ThemeData theme;
final UserPreviewDataBean data;
const _ProfileCard({required this.theme, required this.data});
@override
Widget build(BuildContext context) {
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
child: Padding(
padding: const EdgeInsets.all(20),
child: Row(
children: [
CircleAvatar(
radius: 30,
backgroundColor: theme.colorScheme.primaryContainer,
backgroundImage: data.iconId > 0
? NetworkImage('https://assets2.lxns.net/maimai/icon/${data.iconId}.png')
: null,
child: data.iconId == 0
? Icon(Icons.person, color: theme.colorScheme.onPrimaryContainer)
: null,
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
data.userName.isEmpty ? AppStrings.unknownUser : data.userName,
style: theme.textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 4),
Text(
AppStrings.rating(data.playerRating),
style: theme.textTheme.bodyLarge?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
],
),
),
Icon(
data.isLogin ? Icons.circle : Icons.circle_outlined,
color: data.isLogin ? Colors.green : Colors.grey,
size: 14,
),
const SizedBox(width: 4),
Text(
data.isLogin ? AppStrings.online : AppStrings.offline,
style: theme.textTheme.labelSmall?.copyWith(
color: data.isLogin ? Colors.green : Colors.grey,
),
),
],
),
),
);
}
}
class _GameInfoCard extends StatelessWidget {
final ThemeData theme;
final UserPreviewDataBean data;
const _GameInfoCard({required this.theme, required this.data});
@override
Widget build(BuildContext context) {
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppStrings.gameInfo,
style: theme.textTheme.labelLarge?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 14),
_infoRow(theme, AppStrings.lastGame, data.lastGameId),
_infoRow(theme, AppStrings.lastPlay, data.lastPlayDate),
_infoRow(theme, AppStrings.lastLogin, data.lastLoginDate),
_infoRow(theme, AppStrings.romVersion, data.lastRomVersion),
_infoRow(theme, AppStrings.dataVersion, data.lastDataVersion),
],
),
),
);
}
Widget _infoRow(ThemeData theme, String label, String value) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
children: [
SizedBox(
width: 110,
child: Text(
label,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
),
Expanded(
child: Text(
value.isEmpty ? '-' : value,
style: theme.textTheme.bodyMedium,
),
),
],
),
);
}
}
class _StatusCard extends StatelessWidget {
final ThemeData theme;
final UserPreviewDataBean data;
const _StatusCard({required this.theme, required this.data});
@override
Widget build(BuildContext context) {
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppStrings.status,
style: theme.textTheme.labelLarge?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 14),
_statusChip(theme, AppStrings.netMember, data.isNetMember),
_statusChip(theme, AppStrings.inherit, data.isInherit),
_statusChip(theme, AppStrings.banState, data.banState == 0, trueText: AppStrings.clean, falseText: '已封禁 (${data.banState})'),
_statusChip(theme, AppStrings.dailyBonus, data.dailyBonusDate.isNotEmpty, trueText: data.dailyBonusDate, falseText: AppStrings.notClaimed),
],
),
),
);
}
Widget _statusChip(ThemeData theme, String label, dynamic value, {String trueText = 'Yes', String falseText = 'No'}) {
String text;
Color color;
if (value is bool) {
text = value ? trueText : falseText;
color = value ? Colors.green : Colors.grey;
} else {
text = '$value';
color = theme.colorScheme.onSurface;
}
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
children: [
SizedBox(
width: 110,
child: Text(
label,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 2),
decoration: BoxDecoration(
color: color.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(6),
),
child: Text(
text,
style: theme.textTheme.labelSmall?.copyWith(color: color),
),
),
],
),
);
}
}
class _DetailCard extends StatefulWidget {
final ThemeData theme;
final UserPreviewDataBean data;
const _DetailCard({required this.theme, required this.data});
@override
State<_DetailCard> createState() => _DetailCardState();
}
class _DetailCardState extends State<_DetailCard> {
bool _expanded = false;
@override
Widget build(BuildContext context) {
final theme = widget.theme;
final data = widget.data;
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
child: InkWell(
borderRadius: BorderRadius.circular(16),
onTap: () => setState(() => _expanded = !_expanded),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
AppStrings.moreDetails,
style: theme.textTheme.labelLarge?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
),
Icon(
_expanded ? Icons.expand_less : Icons.expand_more,
color: theme.colorScheme.onSurfaceVariant,
),
],
),
if (_expanded) ...[
const SizedBox(height: 14),
_detailRow(theme, AppStrings.userId, '${data.userId}'),
_detailRow(theme, AppStrings.totalAwake, '${data.totalAwake}'),
_detailRow(theme, AppStrings.displayRate, '${data.dispRate}'),
_detailRow(theme, AppStrings.iconId, '${data.iconId}'),
_detailRow(theme, AppStrings.nameplateId, '${data.nameplateId}'),
_detailRow(theme, AppStrings.trophyId, '${data.trophyId}'),
_detailRow(theme, AppStrings.headphoneVol, '${data.headPhoneVolume}'),
_detailRow(theme, AppStrings.errorId, '${data.errorId}'),
],
],
),
),
),
);
}
Widget _detailRow(ThemeData theme, String label, String value) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
children: [
SizedBox(
width: 130,
child: Text(
label,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
),
Expanded(
child: Text(value, style: theme.textTheme.bodyMedium),
),
],
),
);
}
}
class _DebugRawJsonCard extends StatefulWidget {
final ThemeData theme;
final String rawJson;
const _DebugRawJsonCard({
required this.theme,
required this.rawJson,
});
@override
State<_DebugRawJsonCard> createState() => _DebugRawJsonCardState();
}
class _DebugRawJsonCardState extends State<_DebugRawJsonCard> {
bool _expanded = false;
@override
Widget build(BuildContext context) {
final theme = widget.theme;
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(
color: Colors.orange.withValues(alpha: 0.5),
),
),
child: InkWell(
borderRadius: BorderRadius.circular(16),
onTap: () => setState(() => _expanded = !_expanded),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(Icons.bug_report, size: 16, color: Colors.orange),
const SizedBox(width: 8),
Expanded(
child: Text(
AppStrings.rawApiResponse,
style: theme.textTheme.labelLarge?.copyWith(
color: Colors.orange,
fontWeight: FontWeight.w600,
),
),
),
Icon(
_expanded ? Icons.expand_less : Icons.expand_more,
color: Colors.orange,
),
],
),
if (_expanded) ...[
const SizedBox(height: 12),
Container(
width: double.infinity,
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: theme.colorScheme.surfaceContainerHighest
.withValues(alpha: 0.5),
borderRadius: BorderRadius.circular(8),
),
child: SelectableText(
widget.rawJson,
style: theme.textTheme.bodySmall?.copyWith(
fontFamily: 'monospace',
fontSize: 11,
),
),
),
],
],
),
),
),
);
}
}
class _ErrorIdBanner extends StatelessWidget {
final ThemeData theme;
final int errorId;
const _ErrorIdBanner({required this.theme, required this.errorId});
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
margin: const EdgeInsets.only(bottom: 12),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: theme.colorScheme.errorContainer,
borderRadius: BorderRadius.circular(12),
),
child: Row(
children: [
Icon(Icons.warning_amber_rounded,
size: 20, color: theme.colorScheme.onErrorContainer),
const SizedBox(width: 10),
Expanded(
child: Text(
'API returned errorId: $errorId',
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onErrorContainer,
fontWeight: FontWeight.w600,
),
),
),
],
),
);
}
}

View File

@@ -0,0 +1,220 @@
import 'package:flutter/material.dart';
import '../config/strings.dart';
import '../config/title_server_config.dart';
class SettingsPage extends StatefulWidget {
final bool showAppBar;
const SettingsPage({super.key, this.showAppBar = true});
@override
State<SettingsPage> createState() => _SettingsPageState();
}
class _SettingsPageState extends State<SettingsPage> {
final _formKey = GlobalKey<FormState>();
final _urlController = TextEditingController();
final _aesKeyController = TextEditingController();
final _aesIvController = TextEditingController();
final _clientIdController = TextEditingController();
final _regionIdController = TextEditingController(text: '1');
final _placeIdController = TextEditingController(text: '1403');
final _obfuscateController = TextEditingController(text: 'LatuAa81');
final _apiVersionController = TextEditingController(text: '1.53');
@override
void initState() {
super.initState();
final config = TitleServerConfigHolder().config;
if (config != null) {
_urlController.text = config.titleServerUrl;
_aesKeyController.text = config.aesKey;
_aesIvController.text = config.aesIv;
_clientIdController.text = config.clientId;
_regionIdController.text = '${config.regionId}';
_placeIdController.text = '${config.placeId}';
_obfuscateController.text = config.obfuscateParam;
_apiVersionController.text = config.apiVersion;
}
}
@override
void dispose() {
_urlController.dispose();
_aesKeyController.dispose();
_aesIvController.dispose();
_clientIdController.dispose();
_regionIdController.dispose();
_placeIdController.dispose();
_obfuscateController.dispose();
_apiVersionController.dispose();
super.dispose();
}
Future<void> _save() async {
if (!_formKey.currentState!.validate()) return;
await TitleServerConfigHolder().update(TitleServerConfig(
titleServerUrl: _urlController.text.trim(),
aesKey: _aesKeyController.text.trim(),
aesIv: _aesIvController.text.trim(),
clientId: _clientIdController.text.trim(),
regionId: int.tryParse(_regionIdController.text.trim()) ?? 1,
placeId: int.tryParse(_placeIdController.text.trim()) ?? 1403,
obfuscateParam: _obfuscateController.text.trim(),
apiVersion: _apiVersionController.text.trim(),
));
if (mounted) Navigator.of(context).pop();
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Scaffold(
appBar: widget.showAppBar
? AppBar(
title: const Text(AppStrings.titleServerSettings),
)
: null,
body: SingleChildScrollView(
padding: const EdgeInsets.all(20),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 500),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_buildSection(theme, AppStrings.required, [
_buildField(
controller: _urlController,
label: AppStrings.labelTitleServerUrl,
hint: AppStrings.hintTitleServerUrl,
required: true,
),
const SizedBox(height: 14),
_buildField(
controller: _aesKeyController,
label: AppStrings.labelAesKey,
hint: AppStrings.hintAesKey,
required: true,
),
const SizedBox(height: 14),
_buildField(
controller: _aesIvController,
label: AppStrings.labelAesIv,
hint: AppStrings.hintAesIv,
required: true,
),
const SizedBox(height: 14),
_buildField(
controller: _clientIdController,
label: AppStrings.labelClientId,
hint: AppStrings.hintClientId,
required: true,
),
]),
const SizedBox(height: 24),
_buildSection(theme, AppStrings.optional, [
_buildField(
controller: _regionIdController,
label: AppStrings.labelRegionId,
hint: AppStrings.hintRegionId,
required: false,
),
const SizedBox(height: 14),
_buildField(
controller: _placeIdController,
label: AppStrings.labelPlaceId,
hint: AppStrings.hintPlaceId,
required: false,
),
const SizedBox(height: 14),
_buildField(
controller: _obfuscateController,
label: AppStrings.labelObfuscateParam,
hint: AppStrings.hintObfuscateParam,
required: false,
),
const SizedBox(height: 14),
_buildField(
controller: _apiVersionController,
label: AppStrings.labelApiVersion,
hint: AppStrings.hintApiVersion,
required: false,
),
]),
const SizedBox(height: 32),
FilledButton.icon(
onPressed: _save,
icon: const Icon(Icons.save, size: 20),
label: const Text(AppStrings.save),
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
],
),
),
),
),
);
}
Widget _buildSection(ThemeData theme, String title, List<Widget> children) {
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: theme.textTheme.labelLarge?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 16),
...children,
],
),
),
);
}
Widget _buildField({
required TextEditingController controller,
required String label,
required String hint,
required bool required,
}) {
return TextFormField(
controller: controller,
decoration: InputDecoration(
labelText: label,
hintText: hint,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
contentPadding: const EdgeInsets.all(14),
),
validator: required
? (v) => (v == null || v.trim().isEmpty) ? AppStrings.fieldRequired(label) : null
: null,
);
}
}

631
lib/pages/ticket_page.dart Normal file
View File

@@ -0,0 +1,631 @@
import 'dart:async';
import 'package:flutter/material.dart';
import '../config/strings.dart';
import '../config/title_server_config.dart';
import '../services/title_api_service.dart';
enum TicketStep {
idle,
checkPreview,
userLogin,
fetchData,
simulatePlay,
uploadPlaylog,
upsertAll,
logout,
complete,
failed,
}
class TicketPage extends StatefulWidget {
final int userId;
final String token;
const TicketPage({super.key, required this.userId, required this.token});
@override
State<TicketPage> createState() => _TicketPageState();
}
class _TicketPageState extends State<TicketPage> {
static const _ticketNameMap = {
1: '?',
2: '2倍票',
3: '3倍票',
4: '4倍票',
5: '5倍票',
6: '6倍票',
7: '?',
8: '?',
9: '?',
10: '?',
11: '?',
12: '?',
13: '?',
14: '?',
15: '?',
16: '?',
};
final _formKey = GlobalKey<FormState>();
final _musicIdController = TextEditingController(text: '417');
final _levelController = TextEditingController(text: '3');
final _achievementController = TextEditingController(text: '1010000');
final _comboStatusController = TextEditingController(text: '4');
final _syncStatusController = TextEditingController(text: '4');
final _deluxscoreMaxController = TextEditingController(text: '2277');
final _scoreRankController = TextEditingController(text: '13');
TicketStep _step = TicketStep.idle;
String _stepMessage = '';
String? _error;
bool _running = false;
List<Map<String, dynamic>>? _tickets;
bool _ticketsLoading = false;
String? _ticketsError;
String _ticketName(int chargeId) =>
_ticketNameMap[chargeId] ?? '$chargeId';
@override
void dispose() {
_musicIdController.dispose();
_levelController.dispose();
_achievementController.dispose();
_comboStatusController.dispose();
_syncStatusController.dispose();
_deluxscoreMaxController.dispose();
_scoreRankController.dispose();
super.dispose();
}
Map<String, dynamic> _buildMusicData() {
return {
'musicId': int.tryParse(_musicIdController.text) ?? 417,
'level': int.tryParse(_levelController.text) ?? 3,
'achievement': int.tryParse(_achievementController.text) ?? 1010000,
'comboStatus': int.tryParse(_comboStatusController.text) ?? 4,
'syncStatus': int.tryParse(_syncStatusController.text) ?? 4,
'deluxscoreMax': int.tryParse(_deluxscoreMaxController.text) ?? 2277,
'scoreRank': int.tryParse(_scoreRankController.text) ?? 13,
'extNum1': 0,
'playCount': 1,
};
}
void _updateStep(TicketStep step, [String? message]) {
if (!mounted) return;
setState(() {
_step = step;
_stepMessage = message ?? '';
});
}
Future<void> _loadTickets() async {
if (!TitleServerConfigHolder().isConfigured) return;
setState(() {
_ticketsLoading = true;
_ticketsError = null;
_tickets = null;
});
try {
final config = TitleServerConfigHolder().config!;
final service = TitleApiService(config);
final data = await service.getUserCharge(widget.userId);
if (!mounted) return;
final rawList = data['userChargeList'] as List<dynamic>? ?? [];
setState(() {
_tickets = rawList.map((e) => e as Map<String, dynamic>).toList();
_ticketsLoading = false;
});
} catch (e) {
if (!mounted) return;
setState(() {
_ticketsError = e.toString();
_ticketsLoading = false;
});
}
}
Future<void> _runTicket() async {
if (!TitleServerConfigHolder().isConfigured) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text(AppStrings.ticketNotConfigured)),
);
}
return;
}
final config = TitleServerConfigHolder().config!;
final service = TitleApiService(config);
final musicData = _buildMusicData();
final userId = widget.userId;
final token = widget.token;
setState(() {
_running = true;
_error = null;
});
try {
// Step 1: Check preview
_updateStep(TicketStep.checkPreview);
final preview = await service.getUserPreview(userId: userId, token: token);
if (preview.isLogin) {
throw TitleApiException(AppStrings.ticketErrorAlreadyLogin);
}
// Step 2: Login
_updateStep(TicketStep.userLogin);
final loginResult = await service.userLoginFull(userId: userId, token: token);
// Step 3: Fetch user data
_updateStep(TicketStep.fetchData);
final results = await Future.wait([
service.getUserData(userId),
service.getUserExtend(userId),
service.getUserOption(userId),
service.getUserRating(userId),
service.getUserCharge(userId),
service.getUserActivity(userId),
service.getUserMissionData(userId),
]);
// Step 4: Simulate play time
_updateStep(TicketStep.simulatePlay);
await Future.delayed(const Duration(seconds: 1));
// Step 5: Upload playlog
_updateStep(TicketStep.uploadPlaylog);
await service.uploadUserPlaylog(
userId: userId,
loginId: loginResult.loginId,
musicData: musicData,
userData: results[0],
);
// Step 6: Upsert all
_updateStep(TicketStep.upsertAll);
await service.upsertUserAll(
userId: userId,
loginId: loginResult.loginId,
loginDate: loginResult.lastLoginDate,
musicData: musicData,
generalUserInfo: results,
);
// Step 7: Logout
_updateStep(TicketStep.logout);
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
await service.userLogout(userId: userId, loginDateTime: now);
_updateStep(TicketStep.complete, AppStrings.ticketLoginInfo(loginResult.loginId));
} on TitleApiException catch (e) {
_updateStep(TicketStep.failed, e.message);
setState(() => _error = e.message);
} catch (e) {
_updateStep(TicketStep.failed, e.toString());
setState(() => _error = e.toString());
} finally {
setState(() => _running = false);
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
if (!TitleServerConfigHolder().isConfigured) {
return Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.settings_ethernet, size: 48, color: theme.colorScheme.primary),
const SizedBox(height: 16),
Text(AppStrings.ticketNotConfigured, style: theme.textTheme.titleMedium),
],
),
),
);
}
return SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 500),
child: Column(
children: [
_buildTicketListCard(theme),
const SizedBox(height: 16),
_buildMusicForm(theme),
const SizedBox(height: 16),
_buildRunButton(theme),
if (_step != TicketStep.idle) ...[
const SizedBox(height: 16),
_buildProgressCard(theme),
],
],
),
),
);
}
Widget _buildTicketListCard(ThemeData theme) {
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
AppStrings.myTickets,
style: theme.textTheme.labelLarge?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
),
SizedBox(
height: 32,
child: OutlinedButton.icon(
onPressed: _ticketsLoading ? null : _loadTickets,
icon: _ticketsLoading
? const SizedBox(
width: 14,
height: 14,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.refresh, size: 16),
label: Text(
_ticketsLoading ? AppStrings.loading : AppStrings.refreshTickets,
style: theme.textTheme.labelSmall,
),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
),
],
),
const SizedBox(height: 12),
if (_ticketsError != null)
Container(
width: double.infinity,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: theme.colorScheme.errorContainer,
borderRadius: BorderRadius.circular(8),
),
child: Text(
_ticketsError!,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onErrorContainer,
fontFamily: 'monospace',
),
),
)
else if (_tickets == null)
Text(
AppStrings.ticketsNotLoaded,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
)
else if (_tickets!.isEmpty)
Text(
AppStrings.noTickets,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
)
else
..._tickets!.map((t) => _ticketRow(theme, t)),
],
),
),
);
}
Widget _ticketRow(ThemeData theme, Map<String, dynamic> ticket) {
final chargeId = (ticket['chargeId'] as num?)?.toInt() ?? 0;
final stock = (ticket['stock'] as num?)?.toInt() ?? 0;
final validDate = ticket['validDate'] as String? ?? '-';
final name = _ticketName(chargeId);
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
children: [
Container(
width: 28,
height: 28,
decoration: BoxDecoration(
color: theme.colorScheme.primaryContainer,
borderRadius: BorderRadius.circular(6),
),
alignment: Alignment.center,
child: Text(
'$chargeId',
style: theme.textTheme.labelSmall?.copyWith(
color: theme.colorScheme.onPrimaryContainer,
fontWeight: FontWeight.w600,
),
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
name,
style: theme.textTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
Text(
'有效期: $validDate',
style: theme.textTheme.labelSmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
],
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration(
color: stock > 0 ? Colors.green.withValues(alpha: 0.15) : Colors.grey.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(6),
),
child: Text(
'剩余 $stock',
style: theme.textTheme.labelSmall?.copyWith(
color: stock > 0 ? Colors.green : Colors.grey,
fontWeight: FontWeight.w600,
),
),
),
],
),
);
}
Widget _buildMusicForm(ThemeData theme) {
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
child: Padding(
padding: const EdgeInsets.all(20),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppStrings.musicConfig,
style: theme.textTheme.labelLarge?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 14),
_buildField(_musicIdController, AppStrings.musicId, theme),
_buildField(_levelController, AppStrings.level, theme),
_buildField(_achievementController, AppStrings.achievement, theme),
_buildField(_comboStatusController, AppStrings.comboStatus, theme),
_buildField(_syncStatusController, AppStrings.syncStatus, theme),
_buildField(_deluxscoreMaxController, AppStrings.deluxscoreMax, theme),
_buildField(_scoreRankController, AppStrings.scoreRank, theme),
],
),
),
),
);
}
Widget _buildField(
TextEditingController controller,
String label,
ThemeData theme,
) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
children: [
SizedBox(
width: 140,
child: Text(
label,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
),
Expanded(
child: TextFormField(
controller: controller,
enabled: !_running,
style: theme.textTheme.bodyMedium,
decoration: InputDecoration(
isDense: true,
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
),
],
),
);
}
Widget _buildRunButton(ThemeData theme) {
return SizedBox(
width: double.infinity,
child: FilledButton.icon(
onPressed: _running ? null : _runTicket,
icon: _running
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
)
: const Icon(Icons.play_arrow, size: 20),
label: Text(_running ? AppStrings.runningTicket : AppStrings.runTicket),
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
);
}
Widget _buildProgressCard(ThemeData theme) {
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(
color: _step == TicketStep.failed
? theme.colorScheme.error.withValues(alpha: 0.5)
: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
_step == TicketStep.failed ? AppStrings.stepFailed : AppStrings.ticketsTitle,
style: theme.textTheme.labelLarge?.copyWith(
color: _step == TicketStep.failed
? theme.colorScheme.error
: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 14),
_stepRow(TicketStep.checkPreview, AppStrings.stepCheckPreview, theme),
_stepRow(TicketStep.userLogin, AppStrings.stepUserLogin, theme),
_stepRow(TicketStep.fetchData, AppStrings.stepFetchData, theme),
_stepRow(TicketStep.simulatePlay, AppStrings.stepSimulatePlay, theme),
_stepRow(TicketStep.uploadPlaylog, AppStrings.stepUploadPlaylog, theme),
_stepRow(TicketStep.upsertAll, AppStrings.stepUpsertAll, theme),
_stepRow(TicketStep.logout, AppStrings.stepLogout, theme),
if (_stepMessage.isNotEmpty) ...[
const SizedBox(height: 12),
Container(
width: double.infinity,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: _step == TicketStep.failed
? theme.colorScheme.errorContainer
: theme.colorScheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(8),
),
child: Text(
_stepMessage,
style: theme.textTheme.bodySmall?.copyWith(
fontFamily: 'monospace',
color: _step == TicketStep.failed
? theme.colorScheme.onErrorContainer
: theme.colorScheme.onSurface,
),
),
),
],
if (_error != null) ...[
const SizedBox(height: 12),
Container(
width: double.infinity,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: theme.colorScheme.errorContainer,
borderRadius: BorderRadius.circular(8),
),
child: Text(
_error!,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onErrorContainer,
fontFamily: 'monospace',
),
),
),
],
],
),
),
);
}
Widget _stepRow(TicketStep step, String label, ThemeData theme) {
IconData icon;
Color? color;
if (_step == TicketStep.failed && _step.index > step.index) {
icon = step.index < _step.index ? Icons.check_circle_outline : Icons.circle_outlined;
color = step.index < _step.index ? Colors.green : theme.colorScheme.onSurfaceVariant;
} else if (_step.index > step.index) {
icon = Icons.check_circle;
color = Colors.green;
} else if (_step == step) {
icon = Icons.sync;
color = theme.colorScheme.primary;
} else {
icon = Icons.circle_outlined;
color = theme.colorScheme.onSurfaceVariant;
}
return Padding(
padding: const EdgeInsets.symmetric(vertical: 3),
child: Row(
children: [
Icon(icon, size: 16, color: color),
const SizedBox(width: 10),
Text(
label,
style: theme.textTheme.bodySmall?.copyWith(
color: _step.index >= step.index
? theme.colorScheme.onSurface
: theme.colorScheme.onSurfaceVariant,
fontWeight: _step == step ? FontWeight.w600 : FontWeight.normal,
),
),
],
),
);
}
}