- 添加物品分类、位置、购买日期等新字段 - 实现物品是否使用的Radio选择功能 - 增加价格输入框并限制输入格式 - 优化表单布局,添加适当间距 - 更新Item模型以支持新字段 -调整数据库操作以适应新数据结构
227 lines
8.2 KiB
Dart
227 lines
8.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:item_tracker/database/sqlite_operation.dart';
|
|
|
|
DateTime? selectedDate;
|
|
|
|
enum ItemIsUse {
|
|
yes,
|
|
no,
|
|
}
|
|
|
|
class AddItemScreen extends StatefulWidget {
|
|
@override
|
|
_FromTestRouteSate createState() => _FromTestRouteSate();
|
|
}
|
|
|
|
class _FromTestRouteSate extends State<AddItemScreen> {
|
|
TextEditingController _nameController = TextEditingController();
|
|
TextEditingController _descriptionController = TextEditingController();
|
|
String? _selectedCategory; // 当前选中的分类
|
|
TextEditingController _locationController = TextEditingController();
|
|
ItemIsUse? _itemIsUse = ItemIsUse.yes;
|
|
TextEditingController _priceController = TextEditingController();
|
|
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
|
|
|
// 添加自定义分类列表
|
|
List<String> _categories = ['A', 'B', 'C', 'D']; // 自定义分类
|
|
|
|
// 更新选中的分类
|
|
void _updateSelectedCategory(String? category) {
|
|
setState(() {
|
|
_selectedCategory = category;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
var date = selectedDate;
|
|
|
|
void setGroupValue(ItemIsUse? itemIsUse) {
|
|
setState(() {
|
|
_itemIsUse = itemIsUse;
|
|
});
|
|
}
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text('新增物品'),
|
|
),
|
|
body: Builder(builder: (context) {
|
|
return Form(
|
|
key: _formKey,
|
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
|
child: ListView(
|
|
padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
|
|
children: <Widget>[
|
|
TextFormField(
|
|
autofocus: true,
|
|
controller: _nameController,
|
|
decoration: InputDecoration(
|
|
labelText: "名称",
|
|
hintText: "请输入物品名称",
|
|
border: OutlineInputBorder(),
|
|
),
|
|
maxLength: 20,
|
|
validator: (v) {
|
|
if (v == null || v.trim().isEmpty) {
|
|
print('名称不能为空');
|
|
return "物品名称不能为空";
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
SizedBox(height: 16.0), // 添加间距
|
|
TextField(
|
|
controller: _descriptionController,
|
|
decoration: InputDecoration(
|
|
labelText: "物品描述",
|
|
hintText: "请输入物品描述",
|
|
border: OutlineInputBorder(),
|
|
),
|
|
maxLines: 4,
|
|
maxLength: 200,
|
|
),
|
|
SizedBox(height: 16.0), // 添加间距
|
|
DropdownButtonFormField<String>(
|
|
value: _selectedCategory,
|
|
items: _categories.map((category) {
|
|
return DropdownMenuItem<String>(
|
|
value: category,
|
|
child: Text(category),
|
|
);
|
|
}).toList(),
|
|
onChanged: _updateSelectedCategory,
|
|
decoration: InputDecoration(
|
|
border: OutlineInputBorder(),
|
|
filled: true,
|
|
fillColor: Colors.grey[100],
|
|
labelText: '请选择分类',
|
|
),
|
|
),
|
|
SizedBox(height: 16.0), // 添加间距
|
|
TextFormField(
|
|
controller: _locationController,
|
|
decoration: InputDecoration(
|
|
labelText: "物品位置",
|
|
hintText: "请输入物品位置",
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
SizedBox(height: 16.0), // 添加间距
|
|
Text(
|
|
date == null
|
|
? '请选择日期'
|
|
: '已选择日期: ${date.year}年${date.month}月${date.day}日',
|
|
),
|
|
SizedBox(height: 16.0), // 添加间距
|
|
ElevatedButton.icon(
|
|
icon: Icon(Icons.calendar_today, color: Colors.white),
|
|
onPressed: () async {
|
|
var pickedDate = await showDatePicker(
|
|
context: context,
|
|
initialEntryMode: DatePickerEntryMode.calendarOnly,
|
|
initialDate: DateTime.now(),
|
|
firstDate: DateTime(2015, 8),
|
|
lastDate: DateTime(2101),
|
|
);
|
|
setState(() {
|
|
selectedDate = pickedDate;
|
|
});
|
|
},
|
|
label: Text('选择日期', style: TextStyle(color: Colors.white)),
|
|
style: ElevatedButton.styleFrom(
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8.0),
|
|
),
|
|
),
|
|
),
|
|
SizedBox(height: 16.0), // 添加间距
|
|
Wrap(
|
|
crossAxisAlignment: WrapCrossAlignment.center,
|
|
spacing: 12, // 每个元素之间水平间距
|
|
runSpacing: 8, // 换行后垂直间距
|
|
children: [
|
|
Text(
|
|
'请选择是否使用:',
|
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
|
),
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Radio<ItemIsUse>(
|
|
value: ItemIsUse.yes,
|
|
groupValue: _itemIsUse,
|
|
onChanged: setGroupValue,
|
|
),
|
|
Text('是'),
|
|
],
|
|
),
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Radio<ItemIsUse>(
|
|
value: ItemIsUse.no,
|
|
groupValue: _itemIsUse,
|
|
onChanged: setGroupValue,
|
|
),
|
|
Text('否'),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
SizedBox(height: 16.0), // 添加间距
|
|
TextField(
|
|
controller: _priceController,
|
|
keyboardType: TextInputType.number,
|
|
inputFormatters: [
|
|
FilteringTextInputFormatter.allow(
|
|
RegExp(r'^\d*\.?\d{0,2}$'),
|
|
),
|
|
],
|
|
decoration: InputDecoration(
|
|
labelText: "物品价格",
|
|
hintText: "请输入物品价格",
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
SizedBox(height: 28.0), // 添加间距
|
|
ElevatedButton(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Text("提交"),
|
|
),
|
|
onPressed: () {
|
|
if (_formKey.currentState!.validate()) {
|
|
print('物品名称:${_nameController.text}');
|
|
print('物品描述:${_descriptionController.text}');
|
|
print('物品分类:${_selectedCategory}');
|
|
print('物品位置:${_locationController.text}');
|
|
print('物品购买日期:${date}');
|
|
print('物品是否使用:${_itemIsUse.toString()}');
|
|
print('物品价格:${_priceController.text}');
|
|
|
|
insertItem(_nameController.text,
|
|
_descriptionController.text);
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text('提交成功'),
|
|
duration: Duration(seconds: 1),
|
|
));
|
|
// 确保只有在表单验证成功后才调用 Navigator.pop
|
|
// Navigator.pop(context, true);
|
|
}
|
|
},
|
|
style: ElevatedButton.styleFrom(
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8.0),
|
|
),
|
|
elevation: 4.0,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}),
|
|
);
|
|
}
|
|
} |