iT邦幫忙

4

DAY 8 : To-Do List App 的動態功能與互動操作

  • 分享至 

  • xImage
  •  
  1. 新增互動功能
    在 To-Do List App 中,我們要讓使用者可以新增 to_do 項目。
    •在 MyHomePageState 中新增一個 TextEditingController 來讀取使用者輸入的文字:
final TextEditingController _textController = TextEditingController();

• 在 ListView 上方加入一個 TextField,使用者可以輸入新的 Todo 項目:

Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
  controller: _textController,
  decoration: const InputDecoration(
    labelText: '新增待辦項目',
    border: OutlineInputBorder(),
  ),
),
),
  1. 新增按鈕事件
    • 在畫面中加入新增按鈕(Floating Action Button)
    • 設定按下按鈕後,將使用者輸入的文字加入 to_do 清單
    • 按下按鈕後,文字輸入欄會自動清空,準備下一次輸入
floatingActionButton: FloatingActionButton(
 onPressed: () {
   setState(() {
     if (_textController.text.isNotEmpty) {
       _todos.add(_textController.text);
       _textController.clear();
     }
   });
 },
 child: const Icon(Icons.add),
),
  1. 動態顯示 to_do 項目
    • 將原本固定的 to_do 項目改為由 to_do 清單動態生成
    • 每新增一個 to_do 項目,列表會即時更新,並可自動滾動顯示

  2. 確認互動效果
    • 啟動模擬器,確認原本的 to_do 項目正常顯示
    • 在文字輸入欄輸入文字,按下新增按鈕後,新的to_do項目即時加入清單
    • 確認清單可以滾動,顯示新增的所有項目

https://ithelp.ithome.com.tw/upload/images/20250924/20178900HGCiHOGhCM.png


圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言