This commit is contained in:
jyuesong
2022-01-17 15:04:36 +08:00
parent 9f2aa34e29
commit 105d6172b6
8 changed files with 371 additions and 6 deletions

46
lib/module/env/env_bean.dart vendored Normal file
View File

@@ -0,0 +1,46 @@
import 'package:json_conversion_annotation/json_conversion_annotation.dart';
@JsonConversion()
class EnvBean {
String? value;
String? sId;
int? created;
int? status;
String? timestamp;
String? name;
String? remarks;
EnvBean(
{this.value,
this.sId,
this.created,
this.status,
this.timestamp,
this.name,
this.remarks});
EnvBean.fromJson(Map<String, dynamic> json) {
value = json['value'];
sId = json['_id'];
created = json['created'];
status = json['status'];
timestamp = json['timestamp'];
name = json['name'];
remarks = json['remarks'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['value'] = this.value;
data['_id'] = this.sId;
data['created'] = this.created;
data['status'] = this.status;
data['timestamp'] = this.timestamp;
data['name'] = this.name;
data['remarks'] = this.remarks;
return data;
}
static EnvBean jsonConversion(Map<String, dynamic> json) {
return EnvBean.fromJson(json);
}
}

View File

@@ -1,5 +1,14 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:qinglong_app/base/base_state_widget.dart';
import 'package:qinglong_app/base/routes.dart';
import 'package:qinglong_app/base/theme.dart';
import 'package:qinglong_app/base/ui/empty_widget.dart';
import 'package:qinglong_app/base/ui/menu.dart';
import 'package:qinglong_app/module/env/env_bean.dart';
import 'package:qinglong_app/module/env/env_viewmodel.dart';
import 'package:qinglong_app/utils/utils.dart';
class EnvPage extends StatefulWidget {
const EnvPage({Key? key}) : super(key: key);
@@ -9,8 +18,195 @@ class EnvPage extends StatefulWidget {
}
class _EnvPageState extends State<EnvPage> {
String _searchKey = "";
@override
Widget build(BuildContext context) {
return Scaffold();
return BaseStateWidget<EnvViewModel>(
builder: (ref, model, child) {
return RefreshIndicator(
color: Theme.of(context).primaryColor,
onRefresh: () async {
return model.loadData(false);
},
child: model.list.isEmpty
? const EmptyWidget()
: ListView.builder(
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
itemBuilder: (context, index) {
EnvBean item = model.list[index];
return EnvItemCell(item, ref);
},
itemCount: model.list.length,
),
);
},
model: envProvider,
onReady: (viewModel) {
viewModel.loadData();
},
);
}
}
class EnvItemCell extends StatelessWidget {
final EnvBean bean;
final WidgetRef ref;
const EnvItemCell(this.bean, this.ref, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return CupertinoContextMenu(
actions: [
QLCupertinoContextMenuAction(
child: const Text("编辑"),
onPressed: () {
Navigator.of(context).pop();
Navigator.of(context).pushNamed(Routes.route_AddTask, arguments: bean);
},
trailingIcon: CupertinoIcons.pencil_outline,
),
QLCupertinoContextMenuAction(
child: Text(bean.status! == 0 ? "禁用" : "启用"),
onPressed: () {
Navigator.of(context).pop();
enableEnv();
},
isDestructiveAction: true,
trailingIcon: bean.status! == 0 ? Icons.dnd_forwardslash : Icons.check_circle_outline_sharp,
),
QLCupertinoContextMenuAction(
child: const Text("删除"),
onPressed: () {
Navigator.of(context).pop();
delEnv(context, ref);
},
isDestructiveAction: true,
trailingIcon: CupertinoIcons.delete,
),
],
previewBuilder: (context, anima, child) {
return IntrinsicWidth(
child: Material(
color: Colors.transparent,
child: Text(
bean.name ?? "",
maxLines: 1,
style: TextStyle(
overflow: TextOverflow.ellipsis,
color: bean.status == 0 ? const Color(0xffF85152) : ref.watch(themeProvider).themeColor.taskTitleColor(),
fontSize: 18,
),
),
),
);
},
child: SizedBox(
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.symmetric(
horizontal: 15,
vertical: 8,
),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
child: Material(
color: Colors.transparent,
child: Text(
bean.name ?? "",
maxLines: 1,
style: TextStyle(
overflow: TextOverflow.ellipsis,
color: bean.status == 1 ? const Color(0xffF85152) : ref.watch(themeProvider).themeColor.taskTitleColor(),
fontSize: 18,
),
),
),
),
const SizedBox(
width: 5,
),
Material(
color: Colors.transparent,
child: Text(
bean.remarks ?? "-",
maxLines: 1,
style: TextStyle(
overflow: TextOverflow.ellipsis,
color: ref.watch(themeProvider).themeColor.descColor(),
fontSize: 12,
),
),
),
],
),
const SizedBox(
height: 8,
),
Material(
color: Colors.transparent,
child: Text(
bean.value ?? "",
maxLines: 1,
style: TextStyle(
overflow: TextOverflow.ellipsis,
color: ref.watch(themeProvider).themeColor.descColor(),
fontSize: 12,
),
),
),
],
),
),
const Divider(
height: 1,
indent: 15,
),
],
),
),
);
}
void enableEnv() {
ref.read(envProvider).enableEnv(bean.sId!, bean.status!);
}
void delEnv(BuildContext context, WidgetRef ref) {
showCupertinoDialog(
context: context,
builder: (context) => CupertinoAlertDialog(
title: const Text("确认删除"),
content: Text("确认删除环境变量 ${bean.name ?? ""}"),
actions: [
CupertinoDialogAction(
child: const Text("取消"),
onPressed: () {
Navigator.of(context).pop();
},
),
CupertinoDialogAction(
child: const Text("确定"),
onPressed: () {
Navigator.of(context).pop();
ref.read(envProvider).delEnv(bean.sId!);
},
),
],
),
);
}
}

81
lib/module/env/env_viewmodel.dart vendored Normal file
View File

@@ -0,0 +1,81 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:qinglong_app/base/base_viewmodel.dart';
import 'package:qinglong_app/base/http/api.dart';
import 'package:qinglong_app/base/http/http.dart';
import 'package:qinglong_app/module/env/env_bean.dart';
var envProvider = ChangeNotifierProvider((ref) => EnvViewModel());
class EnvViewModel extends BaseViewModel {
List<EnvBean> list = [];
Future<void> loadData([isLoading = true]) async {
if (isLoading) {
loading(notify: true);
}
HttpResponse<List<EnvBean>> result = await Api.envs("");
if (result.success && result.bean != null) {
list.clear();
list.addAll(result.bean!);
sortList();
success();
} else {
list.clear();
failed(result.message, notify: true);
}
}
void sortList() {
list.sort((a, b) {
return a.status!.compareTo(b.status!);
});
}
Future<void> delEnv(String id) async {
HttpResponse<NullResponse> result = await Api.delEnv(id);
if (result.success) {
list.removeWhere((element) => element.sId == id);
notifyListeners();
} else {
failed(result.message, notify: true);
}
}
void updateEnv(EnvBean result) {
if (result.sId == null) {
loadData(false);
return;
}
EnvBean bean = list.firstWhere((element) => element.sId == result.sId);
bean.name = result.name;
bean.remarks = result.remarks;
bean.value = result.value;
notifyListeners();
}
void enableEnv(String sId, int status) async {
if (status == 1) {
HttpResponse<NullResponse> response = await Api.enableEnv(sId);
if (response.success) {
list.firstWhere((element) => element.sId == sId).status = 1;
sortList();
success();
} else {
failToast(response.message, notify: true);
}
} else {
HttpResponse<NullResponse> response = await Api.disableEnv(sId);
if (response.success) {
list.firstWhere((element) => element.sId == sId).status = 0;
sortList();
success();
} else {
failToast(response.message, notify: true);
}
}
}
}