- Implement AppConfigService using path_provider to store settings in a local JSON file,\n ensuring configuration survives app updates/reinstalls.\n - Replace shared_preferences with AppConfigService in HomePage, SettingsPage,\n and RegistrationPage.\n - Replace shared_preferences with AppConfigService in HomePage, SettingsPage,\n and RegistrationPage.\n - Add HomePage.dart as the main navigation hub with feature cards (Registration,\n Boxing, Shelf Query) and dynamic connection status bar.\n - Remove Settings entry from RegistrationPage; Settings are only accessible from\n the Home page. Use 'flutter analyze' to verify the code.
124 lines
3.6 KiB
Dart
124 lines
3.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:pad_scanner/services/app_config_service.dart';
|
|
import 'package:pad_scanner/services/api_service.dart';
|
|
|
|
class SettingsPage extends StatefulWidget {
|
|
const SettingsPage({super.key});
|
|
|
|
@override
|
|
State<SettingsPage> createState() => _SettingsPageState();
|
|
}
|
|
|
|
class _SettingsPageState extends State<SettingsPage> {
|
|
final _controller = TextEditingController();
|
|
final _apiService = ApiService();
|
|
bool _saving = false;
|
|
bool _testing = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadUrl();
|
|
}
|
|
|
|
Future<void> _loadUrl() async {
|
|
final configService = AppConfigService();
|
|
final url = await configService.getString('api_url') ?? '';
|
|
_controller.text = url;
|
|
}
|
|
|
|
Future<void> _saveUrl() async {
|
|
final url = _controller.text.trim();
|
|
if (url.isEmpty) {
|
|
_showSnackBar('请输入 API 地址', isError: true);
|
|
return;
|
|
}
|
|
setState(() => _saving = true);
|
|
final configService = AppConfigService();
|
|
await configService.setString('api_url', url);
|
|
setState(() => _saving = false);
|
|
if (mounted) {
|
|
_showSnackBar('设置已保存');
|
|
}
|
|
}
|
|
|
|
Future<void> _testConnection() async {
|
|
final url = _controller.text.trim();
|
|
if (url.isEmpty) {
|
|
_showSnackBar('请先输入 API 地址', isError: true);
|
|
return;
|
|
}
|
|
setState(() => _testing = true);
|
|
final ok = await _apiService.testConnection(url);
|
|
setState(() => _testing = false);
|
|
if (mounted) {
|
|
_showSnackBar(ok ? '连接成功' : '连接失败,请检查地址和网络', isError: !ok);
|
|
}
|
|
}
|
|
|
|
void _showSnackBar(String message, {bool isError = false}) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(message),
|
|
backgroundColor: isError ? Colors.red.shade700 : Colors.green.shade700,
|
|
duration: const Duration(seconds: 2),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('设置')),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const Text('API 服务器地址', style: TextStyle(fontSize: 14)),
|
|
const SizedBox(height: 8),
|
|
TextField(
|
|
controller: _controller,
|
|
decoration: const InputDecoration(
|
|
hintText: 'http://192.168.1.100:8000',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
keyboardType: TextInputType.url,
|
|
),
|
|
const SizedBox(height: 20),
|
|
ElevatedButton.icon(
|
|
onPressed: _saving ? null : _saveUrl,
|
|
icon: _saving
|
|
? const SizedBox(
|
|
width: 18,
|
|
height: 18,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Icon(Icons.save),
|
|
label: const Text('保存设置'),
|
|
),
|
|
const SizedBox(height: 8),
|
|
OutlinedButton.icon(
|
|
onPressed: _testing ? null : _testConnection,
|
|
icon: _testing
|
|
? const SizedBox(
|
|
width: 18,
|
|
height: 18,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Icon(Icons.wifi),
|
|
label: const Text('测试连接'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|