140 lines
3.6 KiB
Dart
140 lines
3.6 KiB
Dart
import 'package:flisp_app/models/flisp.dart';
|
|
import 'package:flisp_app/provider/flisp_provider.dart';
|
|
import 'package:flisp_app/service/flisp_service.dart';
|
|
import 'package:flisp_app/utils/flisp_utils.dart';
|
|
import 'package:flisp_app/widgets/awesome_dialog.dart';
|
|
import 'package:flisp_app/widgets/flisp_form.dart';
|
|
import 'package:flisp_app/widgets/flisp_widget.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_form_builder/flutter_form_builder.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:flisp_app/widgets/common.dart';
|
|
|
|
class FlispPage extends StatefulWidget {
|
|
final VoidCallback? onAddPressed;
|
|
|
|
const FlispPage({super.key, this.onAddPressed});
|
|
|
|
@override
|
|
State<FlispPage> createState() => FlispPageState();
|
|
}
|
|
|
|
class FlispPageState extends State<FlispPage> {
|
|
final FlispService flispService = FlispService();
|
|
|
|
late List<Flisp> _flisps;
|
|
FlispTab _currentTab = FlispTab.all;
|
|
|
|
// 获取过滤后的闪灵事项
|
|
List<Flisp> get _activeFlisps => getActiveFlisps(_currentTab, _flisps);
|
|
|
|
void showAddDialog() {
|
|
_showDialog(false, null);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_flisps = flispService.getAllFlisps();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Padding(
|
|
padding: EdgeInsets.all(10),
|
|
child: Column(
|
|
children: [
|
|
buildTabs(
|
|
context: context,
|
|
currentTab: _currentTab,
|
|
onTabChanged: (value) {
|
|
setState(() {
|
|
_currentTab = value;
|
|
});
|
|
},
|
|
),
|
|
// 待办事项列表
|
|
Expanded(child: _buildActiveFlispList(context)),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildActiveFlispList(BuildContext context) {
|
|
return _activeFlisps.isEmpty
|
|
? buildEmptyState(context)
|
|
: buildFlispList(
|
|
context: context,
|
|
flisps: _activeFlisps,
|
|
onEdit: (flisp) {
|
|
_showDialog(true, flisp);
|
|
},
|
|
onDelete: (flisp) {
|
|
_deleteFlisp(flisp);
|
|
},
|
|
);
|
|
}
|
|
|
|
// 显示对话框
|
|
void _showDialog(bool isEditing, Flisp? flisp) {
|
|
final flispProvider = Provider.of<FlispProvider>(context, listen: false);
|
|
|
|
if (isEditing) {
|
|
flispProvider.initForm(flisp!);
|
|
} else {
|
|
flispProvider.resetForm();
|
|
}
|
|
|
|
final formKey = GlobalKey<FormBuilderState>();
|
|
|
|
buildModalBottom(
|
|
context: context,
|
|
title: isEditing ? '编辑闪灵' : '创建闪灵',
|
|
body: FlispForm(
|
|
formKey: formKey,
|
|
isEditing: isEditing,
|
|
initialFlisp: flisp,
|
|
onOk: () {
|
|
if (formKey.currentState!.saveAndValidate()) {
|
|
Navigator.of(context).pop();
|
|
_saveFlisp(isEditing, flispProvider.formItem);
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
void _saveFlisp(bool isEditing, Flisp flisp) async {
|
|
late bool isSuccess;
|
|
if (isEditing) {
|
|
isSuccess = await flispService.updateFlisp(flisp);
|
|
} else {
|
|
isSuccess = await flispService.addFlisp(flisp);
|
|
}
|
|
|
|
setState(() {
|
|
_flisps = flispService.getAllFlisps();
|
|
});
|
|
|
|
if (isSuccess) {
|
|
showSuccessDialog(context, isEditing ? '更新成功' : '添加成功');
|
|
} else {
|
|
showErrorDialog(context, isEditing ? '更新失败' : '添加失败');
|
|
}
|
|
}
|
|
|
|
void _deleteFlisp(Flisp flisp) async {
|
|
bool isSuccess = await flispService.deleteFlisp(flisp);
|
|
|
|
setState(() {
|
|
_flisps = flispService.getAllFlisps();
|
|
});
|
|
|
|
if (isSuccess) {
|
|
showSuccessDialog(context, '删除成功');
|
|
} else {
|
|
showErrorDialog(context, '删除失败');
|
|
}
|
|
}
|
|
}
|