簡體   English   中英

如何檢查 TextFormField 是否聚焦

[英]How to check whether TextFormField is focused or not

我有一個帶有focusNode的TextFormField:

TextFormField(
   key: Key('login-username-field-key'),
   controller: loginTextController,
   textInputAction: TextInputAction.next,
   keyboardType: TextInputType.text,
   onFieldSubmitted: (term){
      _changeFocusField(context, _loginFocus, _passwordFocus);
   },
   focusNode: _loginFocus,
   decoration: InputDecoration(
   labelText: AppLocalizations.of(context).loginFieldUsername
   ),
),

進而:

// Check that text field initially is not focused
final TextFormField textField = tester.widget(find.byKey(Key('login-username-field-key')));
expect(textField.focusNode.hasFocus, isFalse);

但是從文檔中我看到TextFormField沒有 'focusNode' 屬性(如TextField.focusNode .hasFocus)。

那么如何檢查這種行為呢?

PS 我的意思是我們可以使用 FocusNode 偵聽器,但我不想僅出於測試目的而這樣做。 它應該真的很簡單 field.focusNode 就像 TextField 一樣。

您應該添加偵聽器,您可以在其中檢查 textFormField 的狀態。 方法hasFocus返回 true 或 false。

@override
  void initState() {
    super.initState();
    _loginFocus.addListener(_onFocusChange);
  }

  void _onFocusChange(){
    debugPrint("Focus: "+_focus.hasFocus.toString());
  }

暫無
暫無

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

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