答案 0 :(得分:3)
Container(
child: Stack(
alignment: Alignment.centerRight,
children: <Widget>[
TextField(),
IconButton(
icon: Icon(Icons.image),
onPressed: () {
// do something
},
),
],
),
)
答案 1 :(得分:2)
经过测试并确认,正是您想要的。
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...
},
),
],
),
答案 2 :(得分:1)
此问题还有另一种可能的解决方法-从此处获取:https://github.com/flutter/flutter/issues/39376-使用带有按钮的标准TextField作为suffixIcon,然后使用魔术:
InputDecoration(labelText: "Email address",
border: OutlineInputBorder(),
suffixIcon: IconButton(
iconSize: 40,
icon: Icon(Icons.fingerprint),
onPressed: () async {
focusNode.unfocus();
focusNode.canRequestFocus = false;
await performBiometricLogin();
focusNode.canRequestFocus = true;
},
),
),
在这种情况下,您需要注意两件事:
a)在您的小部件中声明focusNode(我正在我的全状态小部件的state类中进行声明),然后将其用于您的文本字段:
FocusNode focusNode = FocusNode();
并在TextField中使用名为focusNode的属性:
focusNode: focusNode,
b)如果您未在onPressed事件处理程序中执行任何异步操作,则必须完全遵循github问题的逻辑-一段时间后启用canRequestFocus:
Future.delayed(Duration(milliseconds: 100), () {
widget.textFieldFocusNode.canRequestFocus = true;
});
希望它能像帮助我一样帮助其他人。
谢谢。
答案 3 :(得分:0)
单击而不打开键盘?如果是这样,只需创建一个类并将其分配给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,
),
使用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,
),
我认为您必须将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
},
)
],
)
答案 4 :(得分:0)
添加后缀图标和后缀图标单击提交的文本,就我而言,我已按以下方式使用
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 :(得分:0)
我已经取得了同样的成就
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 :(得分:0)
这应该有效
Stack(
alignment: Alignment.centerRight,
children: <Widget>[
TextFormField(
),
FlatButton(
onPressed: () {
_openOrhidePassword();
},
child: Image(
height: 24.0,
width: 24.0,
image: AssetImage('images/Eye.png'),
),
),
],
),
在我看来,图像尺寸是另一个问题。当我提供尺寸时,它可以与其他小部件一起很好地工作。
答案 7 :(得分:0)
使用readOnly
,不要使用enabled
。