簡體   English   中英

將焦點更改為顫動中的下一個文本字段

[英]Change focus to next textfield in flutter

我有一個用於制作文本字段的代碼,如下所示

 ...Column(
   children: <Widget>[
                      textField(text: 'User Name'),
                      SizedBox(height: 20),
                      textField(text: 'Password'), ])...

textField 是我制作的一個類,它具有以下代碼來制作文本字段。 我這樣做是為了避免代碼重復,因為我在我的應用程序中經常使用文本字段。

...TextField(
    textInputAction: TextInputAction.next,
    obscureText: text == 'Password' ? true : false,
    textCapitalization: TextCapitalization.words,
    cursorColor: primaryColor,
    textAlign: TextAlign.center,
    decoration: InputDecoration(
      labelText: text,
      labelStyle: TextStyle(color: primaryColor, fontFamily: 'Bellotta'),
      filled: true,
      fillColor: Colors.white,
      border: OutlineInputBorder(...

因為我使用這個函數來創建文本字段,所以我不能使用 focusNode 來改變焦點。 我該如何解決這個問題??

您可以為當前文本字段的focusNode指定另外兩個參數,另一個用於您想要更改的焦點,例如

textField(
    text: 'User Name', 
    focusNode: // your focus node, 
    nextFocusNode: // your next field focus node
),

現在你可以使用:

FocusScope.of(context).requestFocus(nextFocus);

提示:如果您想減少代碼重復,那么您應該將您的InputDecoration存儲在 var/ 方法中,而不是創建文本字段方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM