feat: rewrite SettingsPage with save and test-connection
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,7 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
final _controller = TextEditingController();
|
final _controller = TextEditingController();
|
||||||
final _apiService = ApiService();
|
final _apiService = ApiService();
|
||||||
bool _saving = false;
|
bool _saving = false;
|
||||||
|
bool _testing = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -22,41 +23,52 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
|
|
||||||
Future<void> _loadUrl() async {
|
Future<void> _loadUrl() async {
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
_controller.text = prefs.getString('api_url') ?? '';
|
final url = prefs.getString('api_url') ?? '';
|
||||||
|
_controller.text = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _saveUrl() async {
|
Future<void> _saveUrl() async {
|
||||||
|
final url = _controller.text.trim();
|
||||||
|
if (url.isEmpty) {
|
||||||
|
_showSnackBar('请输入 API 地址', isError: true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
setState(() => _saving = true);
|
setState(() => _saving = true);
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
await prefs.setString('api_url', _controller.text.trim());
|
await prefs.setString('api_url', url);
|
||||||
setState(() => _saving = false);
|
setState(() => _saving = false);
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
_showSnackBar('设置已保存');
|
||||||
const SnackBar(content: Text('URL saved')),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _testConnection() async {
|
Future<void> _testConnection() async {
|
||||||
final url = _controller.text.trim();
|
final url = _controller.text.trim();
|
||||||
if (url.isEmpty) {
|
if (url.isEmpty) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
_showSnackBar('请先输入 API 地址', isError: true);
|
||||||
const SnackBar(content: Text('Please enter a URL first')),
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final ok = await _apiService.sendScanData(
|
setState(() => _testing = true);
|
||||||
url,
|
final ok = await _apiService.testConnection(url);
|
||||||
barcode: 'TEST_BARCODE',
|
setState(() => _testing = false);
|
||||||
codeType: 'TEST',
|
|
||||||
);
|
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
_showSnackBar(
|
||||||
SnackBar(content: Text(ok ? 'Connection OK' : 'Connection failed')),
|
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
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_controller.dispose();
|
_controller.dispose();
|
||||||
@@ -66,35 +78,45 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: const Text('Settings')),
|
appBar: AppBar(title: const Text('设置')),
|
||||||
body: Padding(
|
body: Padding(
|
||||||
padding: const EdgeInsets.all(16),
|
padding: const EdgeInsets.all(16),
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
children: [
|
children: [
|
||||||
|
const Text('API 服务器地址', style: TextStyle(fontSize: 14)),
|
||||||
|
const SizedBox(height: 8),
|
||||||
TextField(
|
TextField(
|
||||||
controller: _controller,
|
controller: _controller,
|
||||||
decoration: const InputDecoration(
|
decoration: const InputDecoration(
|
||||||
labelText: 'API URL',
|
hintText: 'http://192.168.1.100:8000',
|
||||||
hintText: 'http://192.168.1.100:8000/scan',
|
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
),
|
),
|
||||||
keyboardType: TextInputType.url,
|
keyboardType: TextInputType.url,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 20),
|
||||||
ElevatedButton(
|
ElevatedButton.icon(
|
||||||
onPressed: _saving ? null : _saveUrl,
|
onPressed: _saving ? null : _saveUrl,
|
||||||
child: _saving
|
icon: _saving
|
||||||
? const SizedBox(
|
? const SizedBox(
|
||||||
width: 20,
|
width: 18,
|
||||||
height: 20,
|
height: 18,
|
||||||
child: CircularProgressIndicator(strokeWidth: 2))
|
child: CircularProgressIndicator(strokeWidth: 2),
|
||||||
: const Text('Save'),
|
)
|
||||||
|
: const Icon(Icons.save),
|
||||||
|
label: const Text('保存设置'),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
OutlinedButton(
|
OutlinedButton.icon(
|
||||||
onPressed: _testConnection,
|
onPressed: _testing ? null : _testConnection,
|
||||||
child: const Text('Test Connection'),
|
icon: _testing
|
||||||
|
? const SizedBox(
|
||||||
|
width: 18,
|
||||||
|
height: 18,
|
||||||
|
child: CircularProgressIndicator(strokeWidth: 2),
|
||||||
|
)
|
||||||
|
: const Icon(Icons.wifi),
|
||||||
|
label: const Text('测试连接'),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user