Кликабельный значок на TextFormField - отключить фокус TextFormField при щелчке значка (Flutter) - PullRequest
0 голосов
/ 25 сентября 2019

Мне нужно textField с суффиксом Icon, но после нажатия на этот значок мне не нужно открывать клавиатуру.Как я могу сделать это альтернативно без суффикса Icon?

enter image description here

Ответы [ 2 ]

0 голосов
/ 25 сентября 2019

добавьте значок суффикса и значок суффикса нажмите на текстовое поле, в моем случае я использовал, как показано ниже

TextFormField(
    textInputAction: TextInputAction.done,
    maxLines: 1,
    obscureText: _obscureText,
    autofocus: false,
    focusNode: _passwordFocus,
    style: TextStyle(fontSize: 17.0, color: Colors.black),
    onFieldSubmitted: (term) {
      _passwordFocus.unfocus();
      _validateAndSubmit();
    },
    decoration: InputDecoration(
      hintText: HINT_PASSWORD,
      hintStyle: TextStyle(fontSize: 17.0, color: Colors.black54),
      focusedBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Colors.black),
      ),
      enabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Colors.black87),
      ),
      errorBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Colors.red),
      ),
      disabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Colors.black87),
      ),
      focusedErrorBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Colors.red),
      ),
      labelText: HINT_PASSWORD,
      labelStyle: TextStyle(fontSize: 17.0, color: Colors.black),
      errorStyle: TextStyle(fontSize: 12.0, color: Colors.red),
      prefixIcon: Icon(
        Icons.lock,
        color: themeColor,
      ),
      /// magic is here suffix ixon click
      suffixIcon: IconButton(
        icon: Icon(
          // Based on passwordVisible state choose the icon
          _obscureText ? Icons.visibility : Icons.visibility_off,
          color: themeColor,
        ),
        onPressed: () {
          // Update the state i.e. toogle the state of passwordVisible variable
          setState(() {
            _obscureText ? _obscureText = false : _obscureText = true;
          });
        },
      ),
    ),
    validator: validatePassword,
    onSaved: (value) => _password = value,
  )
0 голосов
/ 25 сентября 2019

Нажмите и не откроете клавиатуру?Если это так, просто создайте класс и присвойте ему focusNode, установив hasFocus в false, например:

class AlwaysDisabledFocusNode extends FocusNode {
  @override
  bool get hasFocus => false;
}

new TextField(
focusNode: AlwaysDisabledFocusNode(),
onTap: () {},
keyboardType: TextInputType.text,
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(Icons.apps),
hintText: 'Password'),
style: Theme.of(context).textTheme.body1,
),

enter image description here

С readOnly: true он меняет цвет значка при клике

new TextField(readOnly: true,
    //focusNode: AlwaysDisabledFocusNode(),
    onTap: () {},
    keyboardType: TextInputType.text,
    decoration: InputDecoration(
    border: InputBorder.none,
    icon: Icon(Icons.apps),
    hintText: 'Password'),
    style: Theme.of(context).textTheme.body1,
    ),

enter image description here

Я думаю, тогда вам нужно поставить Row с TextField и IconButton, с отдельными действиями.

new Row(
  crossAxisAlignment: CrossAxisAlignment.center,
  mainAxisAlignment: MainAxisAlignment.center,
  children: <Widget>[
    new Expanded(
        child: Padding(
      child: new TextField(
        onTap: () {//action of TextField
        },
        keyboardType: TextInputType.text,
        decoration: InputDecoration(
            border: InputBorder.none, hintText: 'Password'),
        style: Theme.of(context).textTheme.body1,
      ),
      padding: EdgeInsets.only(left: 40),
    )),
    IconButton(
      icon: Icon(Icons.apps),
      onPressed: () {//action of iconbutton
      },
    )
  ],
)

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...