1+ import 'package:flutter/material.dart' ;
2+ import '../basic/config/HiddenWords.dart' ;
3+ import 'components/RightClickPop.dart' ;
4+ import 'components/ListView.dart' ;
5+ import 'components/ContentBuilder.dart' ;
6+
7+ class HiddenWordsScreen extends StatefulWidget {
8+ const HiddenWordsScreen ({Key ? key}) : super (key: key);
9+
10+ @override
11+ State <StatefulWidget > createState () => _HiddenWordsScreenState ();
12+ }
13+
14+ class _HiddenWordsScreenState extends State <HiddenWordsScreen > {
15+ late Future <String > _future = initHiddenWords ();
16+ late Key _key = UniqueKey ();
17+ final TextEditingController _textController = TextEditingController ();
18+
19+ Future <void > _addWord () async {
20+ if (_textController.text.trim ().isEmpty) return ;
21+ await addHiddenWord (_textController.text.trim ());
22+ _textController.clear ();
23+ setState (() {
24+ _future = initHiddenWords ();
25+ _key = UniqueKey ();
26+ });
27+ }
28+
29+ Future <void > _removeWord (String word) async {
30+ await removeHiddenWord (word);
31+ setState (() {
32+ _future = initHiddenWords ();
33+ _key = UniqueKey ();
34+ });
35+ }
36+
37+ Future <void > _clearAll () async {
38+ await clearHiddenWords ();
39+ setState (() {
40+ _future = initHiddenWords ();
41+ _key = UniqueKey ();
42+ });
43+ }
44+
45+ @override
46+ void dispose () {
47+ _textController.dispose ();
48+ super .dispose ();
49+ }
50+
51+ @override
52+ Widget build (BuildContext context) {
53+ return rightClickPop (
54+ child: buildScreen (context),
55+ context: context,
56+ canPop: true ,
57+ );
58+ }
59+
60+ Widget buildScreen (BuildContext context) {
61+ return Scaffold (
62+ appBar: AppBar (
63+ title: const Text ('隐藏词管理' ),
64+ actions: [
65+ IconButton (
66+ icon: const Icon (Icons .delete_sweep),
67+ onPressed: () async {
68+ bool ? confirm = await showDialog <bool >(
69+ context: context,
70+ builder: (context) => AlertDialog (
71+ title: const Text ('确认清空' ),
72+ content: const Text ('确定要清空所有隐藏词吗?' ),
73+ actions: [
74+ TextButton (
75+ onPressed: () => Navigator .of (context).pop (false ),
76+ child: const Text ('取消' ),
77+ ),
78+ TextButton (
79+ onPressed: () => Navigator .of (context).pop (true ),
80+ child: const Text ('确定' ),
81+ ),
82+ ],
83+ ),
84+ );
85+ if (confirm == true ) {
86+ await _clearAll ();
87+ }
88+ },
89+ ),
90+ ],
91+ ),
92+ body: Column (
93+ children: [
94+ Padding (
95+ padding: const EdgeInsets .all (16.0 ),
96+ child: Row (
97+ children: [
98+ Expanded (
99+ child: TextField (
100+ controller: _textController,
101+ decoration: const InputDecoration (
102+ hintText: '输入要隐藏的词' ,
103+ border: OutlineInputBorder (),
104+ ),
105+ onSubmitted: (_) => _addWord (),
106+ ),
107+ ),
108+ const SizedBox (width: 8 ),
109+ IconButton (
110+ icon: const Icon (Icons .add),
111+ onPressed: _addWord,
112+ ),
113+ ],
114+ ),
115+ ),
116+ Expanded (
117+ child: ContentBuilder (
118+ key: _key,
119+ future: _future,
120+ onRefresh: () async {
121+ setState (() {
122+ _future = initHiddenWords ();
123+ _key = UniqueKey ();
124+ });
125+ },
126+ successBuilder: (BuildContext context, AsyncSnapshot <String > snapshot) {
127+ if (hiddenWords.isEmpty) {
128+ return const Center (
129+ child: Text ('暂无隐藏词' ),
130+ );
131+ }
132+ return PikaListView (
133+ children: hiddenWords.map ((word) => ListTile (
134+ title: Text (word),
135+ trailing: IconButton (
136+ icon: const Icon (Icons .delete),
137+ onPressed: () => _removeWord (word),
138+ ),
139+ )).toList (),
140+ );
141+ },
142+ ),
143+ ),
144+ ],
145+ ),
146+ );
147+ }
148+ }
0 commit comments