diff --git a/lib/pages/settings_page.dart b/lib/pages/settings_page.dart index f58a7d7..bbbb8cf 100644 --- a/lib/pages/settings_page.dart +++ b/lib/pages/settings_page.dart @@ -13,6 +13,7 @@ class _SettingsPageState extends State { final _controller = TextEditingController(); final _apiService = ApiService(); bool _saving = false; + bool _testing = false; @override void initState() { @@ -22,41 +23,52 @@ class _SettingsPageState extends State { Future _loadUrl() async { final prefs = await SharedPreferences.getInstance(); - _controller.text = prefs.getString('api_url') ?? ''; + final url = prefs.getString('api_url') ?? ''; + _controller.text = url; } Future _saveUrl() async { + final url = _controller.text.trim(); + if (url.isEmpty) { + _showSnackBar('请输入 API 地址', isError: true); + return; + } setState(() => _saving = true); final prefs = await SharedPreferences.getInstance(); - await prefs.setString('api_url', _controller.text.trim()); + await prefs.setString('api_url', url); setState(() => _saving = false); if (mounted) { - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar(content: Text('URL saved')), - ); + _showSnackBar('设置已保存'); } } Future _testConnection() async { final url = _controller.text.trim(); if (url.isEmpty) { - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar(content: Text('Please enter a URL first')), - ); + _showSnackBar('请先输入 API 地址', isError: true); return; } - final ok = await _apiService.sendScanData( - url, - barcode: 'TEST_BARCODE', - codeType: 'TEST', - ); + setState(() => _testing = true); + final ok = await _apiService.testConnection(url); + setState(() => _testing = false); if (mounted) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text(ok ? 'Connection OK' : 'Connection failed')), + _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(); @@ -66,35 +78,45 @@ class _SettingsPageState extends State { @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar(title: const Text('Settings')), + 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( - labelText: 'API URL', - hintText: 'http://192.168.1.100:8000/scan', + hintText: 'http://192.168.1.100:8000', border: OutlineInputBorder(), ), keyboardType: TextInputType.url, ), - const SizedBox(height: 16), - ElevatedButton( + const SizedBox(height: 20), + ElevatedButton.icon( onPressed: _saving ? null : _saveUrl, - child: _saving + icon: _saving ? const SizedBox( - width: 20, - height: 20, - child: CircularProgressIndicator(strokeWidth: 2)) - : const Text('Save'), + width: 18, + height: 18, + child: CircularProgressIndicator(strokeWidth: 2), + ) + : const Icon(Icons.save), + label: const Text('保存设置'), ), const SizedBox(height: 8), - OutlinedButton( - onPressed: _testConnection, - child: const Text('Test Connection'), + 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('测试连接'), ), ], ),