因應新的功能,開始設計新的UI。
目前專案的狀況是「關鍵功能無法在client端完成」,但疑似有找到使用第三方API完成的可能性。
這種情況下,專案的進行目標就可以彈性的修改,「A團隊開始測試第三方API並找出正確的使用方式」「B團隊開始繼續設計新的介面」。這就是我現在採行的方式,雖然「A團隊」與「B團隊」最終都是我一個人....
「請在main功能的介面上新增一個Spinner,裡面有兩個選項:『文字」與『間隔』,將Spinner放在文字輸入匡的上方。」
給我的答案有點傻眼...
class _MyHomePageState extends State<MyHomePage> {
  // ...
  String dropdownValue = '文字'; // 新增一個變數來儲存目前選擇的值
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // ...
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            DropdownButton<String>( // 新增一個 DropdownButton
              value: dropdownValue,
              icon: const Icon(Icons.arrow_downward),
              iconSize: 24,
              elevation: 16,
              style: const TextStyle(color: Colors.deepPurple),
              underline: Container(
                height: 2,
                color: Colors.deepPurpleAccent,
              ),
              onChanged: (String? newValue) {
                setState(() {
                  dropdownValue = newValue!;
                });
              },
              items: <String>['文字', '間隔']
                .map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                })
                .toList(),
            ),
            // ...
          ],
        ),
      ),
      // ...
    );
  }
}
這個介面不用測試,看就知道不對。
「放在輸入匡的上方,不是整個介面置頂。」
class _MyHomePageState extends State<MyHomePage> {
  // ...
  String dropdownValue = '文字'; // 新增一個變數來儲存目前選擇的值
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // ...
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            DropdownButton<String>( // 新增一個 DropdownButton
              value: dropdownValue,
              icon: const Icon(Icons.arrow_downward),
              iconSize: 24,
              elevation: 16,
              style: const TextStyle(color: Colors.deepPurple),
              underline: Container(
                height: 2,
                color: Colors.deepPurpleAccent,
              ),
              onChanged: (String? newValue) {
                setState(() {
                  dropdownValue = newValue!;
                });
              },
              items: <String>['文字', '間隔']
                .map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                })
                .toList(),
            ),
            // ...
          ],
        ),
      ),
      // ...
    );
  }
}
這次的對了。
那來追加一個要求...
「Spinner選擇「文字」時,文字輸入匡提示文字顯示「請輸入文字」。Spinner選擇「間隔」時,文字輸入匡提示文字顯示「請輸入間隔時間長度。(單位為秒)」。」
AI有特別說明:
「在 Flutter 中,我們可以使用 TextEditingController 的 text 屬性來設定 TextField 的提示文字。當 DropdownButton 的值改變時,我們可以更新 TextEditingController 的 text 屬性。」
              onChanged: (String? newValue) {
                setState(() {
                  dropdownValue = newValue!;
                  if (dropdownValue == '文字') {
                    textController.text = '請輸入文字';
                  } else if (dropdownValue == '間隔') {
                    textController.text = '請輸入間隔時間長度。(單位為秒)';
                  }
                });
              },
雖然有些小錯誤或說需要補充的修正的地方,但基本上這段功能是可以正常運行的...誒誒誒!錯了!
AI沒搞懂「提示文字」,而是直接把「提示文字」當成輸入文字了!
直接告訴它:「textController.text = '請輸入文字'; 請將這裡改為「設定提示文字」而不是「設定輸入文字」。」
TextField(
  controller: textController,
  decoration: InputDecoration(
    hintText: dropdownValue == '文字' ? '請輸入文字' : '請輸入間隔時間長度。(單位為秒)',
  ),
),
至於「textController.text」的地方呢?程式碼先自己手動拿掉了。
再追加一個條件...
「可否判斷dropdownValue之內容來限定可以輸入的資料種類。如果是「間隔」時只能輸入數字。」
TextField(
  controller: textController,
  decoration: InputDecoration(
    hintText: dropdownValue == '文字' ? '請輸入文字' : '請輸入間隔時間長度。(單位為秒)',
  ),
  inputFormatters: dropdownValue == '間隔' ? <TextInputFormatter>[
    FilteringTextInputFormatter.digitsOnly // 只允許輸入數字
  ] : <TextInputFormatter>[],
),
記得測試....測試通過!