【问题标题】:Change focus to next textfield in flutter将焦点更改为颤动中的下一个文本字段
【发布时间】:2020-07-03 06:32:13
【问题描述】:

我有如下所示的制作文本字段的代码

 ...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 来改变焦点。 我该如何解决这个问题??

【问题讨论】:

    标签: flutter focus flutter-layout flutter-text


    【解决方案1】:

    您可以为当前文本字段的focusNode 指定另外两个参数,另一个用于您想要更改的焦点,例如

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

    现在你可以使用了:

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

    提示:如果您想减少代码重复,那么您应该将 InputDecoration 存储在 var/ 方法中,而不是创建文本字段方法。

    【讨论】:

    • 这太明显了。不敢相信我忽略了这一点。谢谢
    猜你喜欢
    • 2018-08-30
    • 2019-02-08
    • 1970-01-01
    • 2023-04-05
    • 2015-04-11
    • 1970-01-01
    • 2020-09-17
    • 2020-05-04
    • 2018-08-08
    相关资源
    最近更新 更多