final TextEditingController _textController = TextEditingController();
• 在 ListView 上方加入一個 TextField,使用者可以輸入新的 Todo 項目:
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: _textController,
decoration: const InputDecoration(
labelText: '新增待辦項目',
border: OutlineInputBorder(),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
if (_textController.text.isNotEmpty) {
_todos.add(_textController.text);
_textController.clear();
}
});
},
child: const Icon(Icons.add),
),
動態顯示 to_do 項目
• 將原本固定的 to_do 項目改為由 to_do 清單動態生成
• 每新增一個 to_do 項目,列表會即時更新,並可自動滾動顯示
確認互動效果
• 啟動模擬器,確認原本的 to_do 項目正常顯示
• 在文字輸入欄輸入文字,按下新增按鈕後,新的to_do項目即時加入清單
• 確認清單可以滾動,顯示新增的所有項目