add
This commit is contained in:
209
frontend/asset_assistant/lib/pages/country_add_page.dart
Normal file
209
frontend/asset_assistant/lib/pages/country_add_page.dart
Normal file
@@ -0,0 +1,209 @@
|
||||
import 'package:asset_assistant/utils/host_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class AddCountryPage extends StatefulWidget {
|
||||
const AddCountryPage({super.key});
|
||||
|
||||
@override
|
||||
State<AddCountryPage> createState() => _AddCountryPageState();
|
||||
}
|
||||
|
||||
class _AddCountryPageState extends State<AddCountryPage> {
|
||||
// 输入控制器
|
||||
final TextEditingController _nameController = TextEditingController();
|
||||
final TextEditingController _codeController = TextEditingController();
|
||||
|
||||
// 加载状态
|
||||
bool _isLoading = false;
|
||||
|
||||
// 表单验证键
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
// 创建国家
|
||||
Future<void> _createCountry() async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
// 获取用户ID
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final userId = prefs.getString('user_id');
|
||||
if (userId == null) {
|
||||
if (mounted) {
|
||||
_showDialog('错误', '请先登录');
|
||||
Navigator.pushReplacementNamed(context, '/login');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 准备请求数据
|
||||
final baseUrl = HostUtils().currentHost;
|
||||
const path = '/country/create';
|
||||
final url = '$baseUrl$path';
|
||||
|
||||
final requestData = {
|
||||
'name': _nameController.text.trim(),
|
||||
'code': _codeController.text.trim(),
|
||||
};
|
||||
|
||||
// 发送请求
|
||||
final dio = Dio();
|
||||
final response = await dio.post(
|
||||
url,
|
||||
data: requestData,
|
||||
options: Options(headers: {'Content-Type': 'application/json'}),
|
||||
);
|
||||
|
||||
// 处理响应
|
||||
if (response.statusCode == 200) {
|
||||
final result = response.data;
|
||||
if (result['success'] == true) {
|
||||
if (mounted) {
|
||||
_showDialog('成功', '国家创建成功', () {
|
||||
Navigator.pop(context, true); // 返回并通知上一页刷新
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (mounted) {
|
||||
_showDialog('失败', result['message'] ?? '创建失败,请重试');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (mounted) {
|
||||
_showDialog('错误', '服务器响应异常: ${response.statusCode}');
|
||||
}
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
String errorMessage = '网络请求失败';
|
||||
if (e.response != null) {
|
||||
errorMessage = '请求失败: ${e.response?.statusCode}';
|
||||
} else if (e.type == DioExceptionType.connectionTimeout) {
|
||||
errorMessage = '连接超时,请检查网络';
|
||||
} else if (e.type == DioExceptionType.connectionError) {
|
||||
errorMessage = '网络连接错误';
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
_showDialog('错误', errorMessage);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
_showDialog('错误', '发生未知错误: $e');
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 显示对话框
|
||||
void _showDialog(String title, String content, [VoidCallback? onConfirm]) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: Text(title),
|
||||
content: Text(content),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
onConfirm?.call();
|
||||
},
|
||||
child: const Text('确定'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('新增国家'),
|
||||
centerTitle: true,
|
||||
elevation: 4,
|
||||
shadowColor: Colors.black12,
|
||||
backgroundColor: theme.colorScheme.surfaceContainerHighest,
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.save),
|
||||
onPressed: _isLoading ? null : _createCountry,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SafeArea(
|
||||
child: Container(
|
||||
color: theme.colorScheme.surface,
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
children: [
|
||||
// 国家名称输入框
|
||||
TextFormField(
|
||||
controller: _nameController,
|
||||
style: TextStyle(color: theme.colorScheme.onSurface),
|
||||
decoration: InputDecoration(
|
||||
labelText: '国家名称',
|
||||
hintText: '请输入国家名称',
|
||||
prefixIcon: Icon(
|
||||
Icons.account_balance,
|
||||
color: theme.colorScheme.secondary,
|
||||
),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入国家名称';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// 国家代码输入框
|
||||
TextFormField(
|
||||
controller: _codeController,
|
||||
style: TextStyle(color: theme.colorScheme.onSurface),
|
||||
decoration: InputDecoration(
|
||||
labelText: '国家代码',
|
||||
hintText: '请输入国家代码',
|
||||
prefixIcon: Icon(
|
||||
Icons.code,
|
||||
color: theme.colorScheme.secondary,
|
||||
),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入国家代码';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user