- 新增数据库相关代码,包括数据库帮助类和操作函数 - 重构添加物品页面,增加表单验证和数据提交逻辑 - 新增物品列表页面,实现数据加载和展示功能 - 更新项目配置,添加必要的依赖库
92 lines
3.2 KiB
Dart
92 lines
3.2 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:item_tracker/database/sqlite_operation.dart';
|
||
|
||
class AddItemScreen extends StatefulWidget {
|
||
@override
|
||
_FromTestRouteSate createState() => _FromTestRouteSate();
|
||
}
|
||
|
||
class _FromTestRouteSate extends State<AddItemScreen> {
|
||
TextEditingController _nameController = TextEditingController();
|
||
TextEditingController _contentController = TextEditingController();
|
||
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
appBar: AppBar(
|
||
title: Text('新增物品'),
|
||
),
|
||
body: Builder(builder: (context) {
|
||
return Form(
|
||
key: _formKey, // 设置 globalKey,用于后面获取 FormState
|
||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||
child: Column(
|
||
children: <Widget>[
|
||
TextFormField(
|
||
autofocus: true,
|
||
controller: _nameController,
|
||
decoration: InputDecoration(
|
||
labelText: "名称",
|
||
hintText: "请输入物品名称",
|
||
icon: Icon(Icons.person),
|
||
),
|
||
maxLength: 20,
|
||
// 校验用户名
|
||
validator: (v) {
|
||
if (v == null || v.trim().isEmpty) {
|
||
print('名称不能为空');
|
||
return "物品名称不能为空";
|
||
}
|
||
return null;
|
||
},
|
||
),
|
||
TextFormField(
|
||
controller: _contentController,
|
||
decoration: InputDecoration(
|
||
labelText: "物品描述",
|
||
hintText: "请输入物品描述",
|
||
icon: Icon(Icons.lock),
|
||
),
|
||
maxLength: 200,
|
||
),
|
||
// 登录按钮
|
||
Padding(
|
||
padding: const EdgeInsets.only(top: 28.0),
|
||
child: Row(
|
||
children: <Widget>[
|
||
Expanded(
|
||
child: ElevatedButton(
|
||
child: Padding(
|
||
padding: const EdgeInsets.all(16.0),
|
||
child: Text("提交"),
|
||
),
|
||
onPressed: () {
|
||
if (_formKey.currentState!.validate()) {
|
||
// 验证通过提交数据
|
||
print('物品名称:${_nameController.text}');
|
||
print('物品描述:${_contentController.text}');
|
||
|
||
insertItem(_nameController.text, _contentController.text);
|
||
// 显示操作成功的提示
|
||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||
content: Text('提交成功'),
|
||
duration: Duration(seconds: 1),
|
||
));
|
||
// 返回上一页
|
||
Navigator.pop(context, true);
|
||
}
|
||
},
|
||
),
|
||
),
|
||
],
|
||
),
|
||
)
|
||
],
|
||
),
|
||
);
|
||
}),
|
||
);
|
||
}
|
||
}
|