延續前篇,我想要在範例提供的功能中增加一個輸入匡,替未來的主要功能做輸入資料功能的鋪路。
所以我們打開GPT,網頁版即可,然後把整「段」程式碼貼給它。(注意!「段」是指像昨天文章裡展示的範圍,是一個明確的段落。如果要整「篇」文件都貼給它,那就是「篇」。這樣做的機會是很少的,也不建議這樣做。)
然後在貼上的程式碼後面寫上我們的需求,「請在這裡面增加一個輸入匡,寬度要滿版,但保持適當間距。」。
「寬度要滿版」的意思是說「這個輸入匡的寬度要最大,例如整個螢幕,」所以「保持適當間距」就是...保留點距離輸入匡兩側?
這裡用問句的原因是因為GPT執行結果有點奇妙。
@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      title: Text(widget.title),
    ),
    body: Padding(
      padding: const EdgeInsets.all(16.0), // 适度的间距
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          const Text(
            'You have pushed the button this many times:',
          ),
          Text(
            '$_counter',
            style: Theme.of(context).textTheme.headlineMedium,
          ),
          const SizedBox(height: 20), // 控制文本和输入框之间的间距
          TextField(
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              labelText: 'Enter something',
            ),
          ),
        ],
      ),
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: _incrementCounter,
      tooltip: 'Increment',
      child: const Icon(Icons.add),
    ), // This trailing comma makes auto-formatting nicer for build methods.
  );
}
它直接幫我把間距都設給了所有介面上可見的東西,把整個介面的主要區域都用這個「間距」包起來了!
可以比對一下原本輸入的程式碼,看看它增加了什麼東西,就會知道什麼是輸入匡的程式碼、什麼是間距的程式碼,因為它連中文註解都幫你寫好了。
(但其實它在網頁上就說明了!哈哈!)
這是種錯誤嗎?
是,但也不是。
因為絕大多數時刻,間距確實是「所有介面上的元件都應該要遵守的設計規則」,所以GPT可能擅自認為「我輸入的程式碼有不佳的設計」然後幫我用更理想的做法,但也可能只是因為「其他工程師碰到這個需求時都會這樣處理」。
我們還沒辦法知道GPT是怎麼解讀我們的意思,因為它不會像我這樣策略分析後回應,它只會做標準的工程技術說明而已。
這時候多思考一下會發現、會擔憂,自己剛剛提出要求的時候好像少講了點東西,例如「我希望輸入匡可以在輸入兩行以上時擴展開來」。
這時候不用再次貼上程式碼了,直接輸入「我希望輸入匡可以在輸入兩行以上時擴展開來」,它就會自動幫你帶入上一次的成果。
@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      title: Text(widget.title),
    ),
    body: Padding(
      padding: const EdgeInsets.all(16.0), // 适度的间距
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          const Text(
            'You have pushed the button this many times:',
          ),
          Text(
            '$_counter',
            style: Theme.of(context).textTheme.headlineMedium,
          ),
          const SizedBox(height: 20), // 控制文本和输入框之间的间距
          Expanded(
            child: TextField(
              maxLines: null, // 支持多行输入
              keyboardType: TextInputType.multiline,
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                labelText: 'Enter something',
              ),
              // Optionally, you can set `minLines` if you want a minimum height for the TextField
              minLines: 1,
            ),
          ),
        ],
      ),
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: _incrementCounter,
      tooltip: 'Increment',
      child: const Icon(Icons.add),
    ), // This trailing comma makes auto-formatting nicer for build methods.
  );
}
這次很精準的執行了我的要求。雖然還沒測試,但我看了覺得...讚啦!
可惜,我接下來要改成使用GitHub copilot。因為它有Android Studio plugin。
掰掰了,網頁。