Files
jizhi/frontend/lib/features/settings/push_settings_page.dart
T

169 lines
5.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:miaoji_zhang/shared/services/push_service.dart';
import 'package:miaoji_zhang/shared/theme/app_theme.dart';
import 'package:miaoji_zhang/shared/widgets/app_controls.dart';
class PushSettingsPage extends StatefulWidget {
const PushSettingsPage({super.key});
@override
State<PushSettingsPage> createState() => _PushSettingsPageState();
}
class _PushSettingsPageState extends State<PushSettingsPage> {
final service = PushService.instance;
@override
void initState() {
super.initState();
service.addListener(_changed);
service.refresh();
}
@override
void dispose() {
service.removeListener(_changed);
super.dispose();
}
void _changed() {
if (mounted) setState(() {});
}
Future<void> _toggle(String category, bool value) async {
final ok = await service.setCategory(category, value);
if (!ok && mounted && service.lastError != null) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text(service.lastError!)));
}
}
@override
Widget build(BuildContext context) {
final status = service.nativeStatus;
return Scaffold(
appBar: AppBar(title: const Text('通知设置')),
body: ListView(
padding: const EdgeInsets.fromLTRB(16, 8, 16, 24),
children: [
Card(
child: Column(
children: [
_switchRow(
'系统通知',
'版本更新和重要服务状态',
service.preferences.system,
(value) => _toggle('system', value),
),
const Divider(height: 1),
_switchRow(
'预算提醒',
'预算达到 80% 或 100% 时提醒',
service.preferences.budget,
(value) => _toggle('budget', value),
),
const Divider(height: 1),
_switchRow(
'运营通知',
'活动和产品公告',
service.preferences.operations,
(value) => _toggle('operations', value),
),
],
),
),
const SizedBox(height: 12),
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'推送通道',
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w800),
),
const SizedBox(height: 8),
Text(
_providerLabel(status.provider),
style: TextStyle(color: context.jz.text2, fontSize: 12),
),
const SizedBox(height: 4),
Text(
_statusLabel(status),
style: TextStyle(
color: status.notificationsAllowed
? context.jz.text2
: AppTheme.orange,
fontSize: 12,
),
),
if (!status.notificationsAllowed) ...[
const SizedBox(height: 12),
JzActionButton(
label: '打开系统通知设置',
onPressed: service.openNotificationSettings,
secondary: true,
),
],
],
),
),
),
if (service.loading) ...[
const SizedBox(height: 16),
const Center(child: CircularProgressIndicator(strokeWidth: 2)),
],
],
),
);
}
Widget _switchRow(
String title,
String subtitle,
bool value,
ValueChanged<bool> onChanged,
) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(title, style: const TextStyle(fontWeight: FontWeight.w700)),
const SizedBox(height: 3),
Text(
subtitle,
style: TextStyle(color: context.jz.text2, fontSize: 11.5),
),
],
),
),
Switch(value: value, onChanged: service.loading ? null : onChanged),
],
),
);
String _providerLabel(String? provider) => switch (provider) {
'huawei' => '华为 Push Kit',
'honor' => '荣耀 Push Kit',
'xiaomi' => '小米推送',
'oppo' => 'OPPO 推送',
'vivo' => 'vivo 推送',
'meizu' => '魅族推送',
_ => '当前设备没有可用的国产厂商通道',
};
String _statusLabel(PushNativeStatus status) {
if (!status.supported) return '不支持';
if (!status.sdkAvailable) return '当前安装包未配置对应厂商 SDK';
if (!status.notificationsAllowed) return '系统通知权限已关闭';
if (status.token?.isNotEmpty == true) return '已连接';
if (status.enabled) return '正在获取厂商令牌';
return '未启用';
}
}