pikapika/lib/basic/config/ShadowCategories.dart

83 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/dialog/mult_select_dialog.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>[];
shadowCategories.forEach((element) {
if (shadowCategories.contains(element)) {
initialValue.add(element);
}
});
return MultiSelectDialog<String>(
title: Text('封印'),
searchHint: '搜索',
cancelText: Text('取消'),
confirmText: Text('确定'),
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
},
icon: 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(
title: Text("封印"),
subtitle: Text(jsonEncode(shadowCategories)),
onTap: () async {
await _chooseShadowCategories(context);
setState(() {});
},
);
},
);
}