1. 要先用一個輸入匡, **[TextFormField](https://api.flutter.dev/flutter/material/TextFormField-class.html)** 2. TextFormField 中可以 decoration: InputDecoration 設定裡面的樣式 ```flutter! TextFormField( decoration: InputDecoration(), ) ``` 3. 之後可以在 TextFormField 用 decoration: InputDecoration 中的 suffixIcon 去新增右側按鈕 ```flutter! TextFormField( decoration: InputDecoration( suffixIcon: IconButton(), ), ) ``` 4. [要取得輸入匡中的值](https://docs.flutter.dev/cookbook/forms/retrieve-input),就要在 TextFormField 中新增 controller: myController ,並且在 ```flutter! TextFormField( controller: myController, decoration: InputDecoration( suffixIcon: IconButton(), ), ) ``` 5. 在 state 中 增加一個變量為 ```dart! final myController = TextEditingController(); ``` 6. 並在下面新增 ```dart! @override void dispose() { myController.dispose(); super.dispose(); } ``` 7. 在外面活用 myController.text 即可 <br> ## ListView.builder 比 List.generate 好 通常 **List.generate** 會在初始化的時候,把串接的資料直接讓他跑出來,但他沒有 ListView.builder 活用度高,所以一般使用前者。 ```dart! List.generate( addTodoList.length, (index)=> { Text(addTodoList[index]) } ) ``` **ListView.builder** 使用方法 ```flutter! ListView.builder( itemCount: addTodoList.length, itemBuilder: (context, index) => Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text(addTodoListIndex), IconButton( onPressed: addTodoListRemoveIndex.call(), icon: const Icon(Icons.delete) ) ], ); ), ``` --- ### 按鈕方法 IconButton 通常不推薦,因為只能用內建的 icon。 ```flutter! IconButton( onPressed: () { addTodoList.removeAt(index); setState(() {}); }, icon: Icon(Icons.delete) ) ``` ### 按鈕方法 GestureDetector 在 Flutter 中我們可以利用 [GestureDetector](https://api.flutter.dev/flutter/widgets/GestureDetector-class.html) 來偵測互動手勢,藉此給予適當的回饋。 GestureDetector 可以從父元件管理 Stateful Widget 的狀態,也可以在元件本身管理。 可以參考:[ithome文章](https://https://ithelp.ithome.com.tw/articles/10223328) ```flutter! GestureDetector( onTap: () {}, child: Icon(Icons.delete), ), ``` ### 按鈕方法 ElevatedButton 最推薦的方法,因為可以很活用,也可以塞圖片進去,各種動畫也可以使用 ```flutter! ElevatedButton( onPressed: () {}, style: ElevatedButton.styleFrom(), child: Icon(Icons.delete) ) ```