在Flutter中删除列表中的勾选单词,可以通过以下步骤实现:
class Word {
String word;
bool isChecked;
Word({required this.word, this.isChecked = false});
}
class WordListManager {
List<Word> words = [];
void addWord(String word) {
words.add(Word(word: word));
}
void removeCheckedWords() {
words.removeWhere((word) => word.isChecked);
}
void updateWordCheckedStatus(int index, bool isChecked) {
words[index].isChecked = isChecked;
}
}
class WordListScreen extends StatefulWidget {
@override
_WordListScreenState createState() => _WordListScreenState();
}
class _WordListScreenState extends State<WordListScreen> {
WordListManager manager = WordListManager();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Word List'),
),
body: ListView.builder(
itemCount: manager.words.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(manager.words[index].word),
leading: Checkbox(
value: manager.words[index].isChecked,
onChanged: (isChecked) {
setState(() {
manager.updateWordCheckedStatus(index, isChecked!);
});
},
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
manager.removeCheckedWords();
});
},
child: Icon(Icons.delete),
),
);
}
}
这样,当用户在界面中勾选单词时,勾选状态会更新到数据模型中,点击删除按钮时,会删除所有勾选的单词。
领取专属 10元无门槛券
手把手带您无忧上云