问题
I need a textField that has a suffixIcon, but after click on that icon I don't need to open keyboard. How I can do it alternatively without suffixIcon?
回答1:
Container(
child: Stack(
alignment: Alignment.centerRight,
children: <Widget>[
TextField(),
IconButton(
icon: Icon(Icons.image),
onPressed: () {
// do something
},
),
],
),
)
回答2:
Tested and confirmed, exactly what u want.
Stack(
alignment: Alignment.centerRight,
children: <Widget>[
TextField(
keyboardType: TextInputType.text,
style: Theme.of(context).textTheme.body1,
obscureText: true,
decoration: InputDecoration(
labelText: 'Password',
contentPadding: const EdgeInsets.fromLTRB(6, 6, 48, 6), // 48 -> icon width
),
),
IconButton(
icon: Icon(Icons.dialpad, color: const Color(0xfff96800)),
onPressed: () {
FocusScope.of(context).requestFocus(FocusNode());
// Your codes...
},
),
],
),
回答3:
Click and not open the keyboard? If so, just create a class and assign it to focusNode, setting hasFocus to false, like this:
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,
),
With readOnly: true it changes icon color on click
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,
),
I think then you have to put a Row with a TextField and an IconButton, with separate actions.
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
},
)
],
)
回答4:
add suffix icon and suffix icon click on text filed, in my case I have used as below
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,
)
回答5:
I have achieved the same thing
Container(
height: 40,
child: Stack(
children: <Widget>[
TextField(
controller: textEditingController,
keyboardType: TextInputType.text,
decoration: InputDecoration(
prefixIcon: Icon(HelageeIcons.search_icon_of_search_box),
border: OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xff575656),
),
borderRadius: const BorderRadius.all(
const Radius.circular(50.0),
),
),
hintStyle:
TextStyle(color: Color(0xffADADAC), fontSize: 14),
hintText: "Quick Search",
),
),
Positioned(
right: 0,
child: Container(
height: 40,
width: 40,
child: Container(
child: Material(
shape: CircleBorder(),
color: Colors.transparent,
child: InkWell(
customBorder: CircleBorder(),
onTap: () {},
splashColor: Colors.grey,
child: Icon(Icons.keyboard))),
),
),
),
],
),
),
回答6:
This should work
Stack(
alignment: Alignment.centerRight,
children: <Widget>[
TextFormField(
),
FlatButton(
onPressed: () {
_openOrhidePassword();
},
child: Image(
height: 24.0,
width: 24.0,
image: AssetImage('images/Eye.png'),
),
),
],
),
In my case image size was another problem. When I provided the dimension it worked well with other widget.
来源:https://stackoverflow.com/questions/58097067/clickable-icon-on-textformfield-disable-textformfield-focus-on-icon-click-flu