pikapika/lib/basic/config/ShadowCategories.dart

84 lines
2.3 KiB
Dart
Raw Normal View History

2021-09-29 23:57:09 +00:00
/// 屏蔽的分类
2021-10-21 04:08:51 +00:00
import 'dart:convert';
2021-09-29 23:57:09 +00:00
import 'package:flutter/material.dart';
import 'package:multi_select_flutter/multi_select_flutter.dart';
import '../Method.dart';
import '../store/Categories.dart';
2021-11-24 13:22:22 +00:00
import 'ShadowCategoriesEvent.dart';
2021-09-29 23:57:09 +00:00
2021-11-04 05:56:25 +00:00
const _propertyName = "shadowCategories";
2021-09-29 23:57:09 +00:00
late List<String> shadowCategories;
2021-10-21 04:08:51 +00:00
/// 获取封印的类型
Future<List<String>> _loadShadowCategories() async {
var value = await method.loadProperty(_propertyName, jsonEncode(<String>[]));
return List.of(jsonDecode(value)).map((e) => "$e").toList();
}
/// 保存封印的类型
Future<dynamic> _saveShadowCategories(List<String> value) {
return method.saveProperty(_propertyName, jsonEncode(value));
}
2021-09-29 23:57:09 +00:00
Future<void> initShadowCategories() async {
2021-10-21 04:08:51 +00:00
shadowCategories = await _loadShadowCategories();
2021-09-29 23:57:09 +00:00
}
2021-10-21 04:08:51 +00:00
Future<void> _chooseShadowCategories(BuildContext context) async {
2021-09-29 23:57:09 +00:00
await showDialog(
context: context,
builder: (ctx) {
var initialValue = <String>[];
2022-03-17 03:31:25 +00:00
for (var element in shadowCategories) {
2021-09-29 23:57:09 +00:00
if (shadowCategories.contains(element)) {
initialValue.add(element);
}
2022-03-17 03:31:25 +00:00
}
2021-09-29 23:57:09 +00:00
return MultiSelectDialog<String>(
2022-03-19 04:12:27 +00:00
title: const Text('封印'),
2021-09-29 23:57:09 +00:00
searchHint: '搜索',
2022-03-19 04:12:27 +00:00
cancelText: const Text('取消'),
confirmText: const Text('确定'),
2021-09-29 23:57:09 +00:00
items: storedCategories.map((e) => MultiSelectItem(e, e)).toList(),
initialValue: initialValue,
onConfirm: (List<String>? value) async {
if (value != null) {
2021-10-21 04:08:51 +00:00
await _saveShadowCategories(value);
2021-09-29 23:57:09 +00:00
shadowCategories = value;
shadowCategoriesEvent.broadcast();
}
},
);
},
);
}
Widget shadowCategoriesActionButton(BuildContext context) {
return IconButton(
onPressed: () {
2021-10-21 04:08:51 +00:00
_chooseShadowCategories(context);
2021-09-29 23:57:09 +00:00
},
2022-03-25 14:57:30 +00:00
icon: const Icon(Icons.hide_source),
2021-09-29 23:57:09 +00:00
);
}
2021-10-21 04:08:51 +00:00
Widget shadowCategoriesSetting() {
return StatefulBuilder(
builder: (BuildContext context, void Function(void Function()) setState) {
return ListTile(
2022-03-19 04:12:27 +00:00
title: const Text("封印"),
2021-10-21 04:08:51 +00:00
subtitle: Text(jsonEncode(shadowCategories)),
onTap: () async {
await _chooseShadowCategories(context);
setState(() {});
},
);
},
);
}
2023-02-16 08:14:29 +00:00
const chooseShadowCategories = _chooseShadowCategories;