类似Android的文本字段验证在颤振中?
我想在类似Android的文本字段验证在颤振中?,android,flutter,textfield,Android,Flutter,Textfield,我想在flatter中实现类似Android的TextField验证 我尝试了TextField的docsflatter,而InputDecoration的属性是errorText,但它在TextField的底部显示error。我想在《颤栗》中实现以下目标 您可以使用overlay小部件自定义此类错误。错误可以通过覆盖窗口小部件显示,错误图标可以使用文本字段的inputDecoration进行更改 下面是了解overlay小部件的链接-- 我想你说的是工具提示 你可以使用这个或通过颤振 您可以使
flatter
中实现类似Android的TextField
验证
我尝试了TextField
的docsflatter
,而InputDecoration
的属性是errorText
,但它在TextField
的底部显示error。我想在《颤栗》中实现以下目标
您可以使用overlay小部件自定义此类错误。错误可以通过覆盖窗口小部件显示,错误图标可以使用文本字段的inputDecoration进行更改
下面是了解overlay小部件的链接--
我想你说的是工具提示
你可以使用这个或通过颤振
您可以使用库super#u工具提示
您可以在堆栈中使用TextFormField
以及自定义工具提示
,以实现此效果。在TextFormField
的decoration
属性中,您可以使用InputDecoration
类的subfixicon
属性传递错误图标
当发生验证错误时,您可以使用bool
变量来显示/隐藏工具提示消息
TextFormField
的示例代码:
TextFormField(
decoration: InputDecoration(
//Set the different border properties for a custom design
suffixIcon: IconButton(
icon: Icon(Icons.error, color: Colors.red),
onPressed: () {
setState(() {
showTooltip = !showTooltip; //Toggles the tooltip
});
},
),
),
validator: (String value) {
if(value.isEmpty) {
setState(() {
showTooltip = true; //Toggles the tooltip
});
return "";
}
}
);
您可以使用堆栈
将上述代码与自定义工具提示小部件代码一起包装,以实现错误工具提示效果
下面是一个快速工作的示例。您可以设计自己的自定义工具提示小部件,并在代码中使用它
例如:
class LoginAlternate extends StatefulWidget {
@override
_LoginAlternateState createState() => _LoginAlternateState();
}
class _LoginAlternateState extends State<LoginAlternate> {
GlobalKey<FormState> _formKey = GlobalKey();
bool showTooltip = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blueGrey,
body: Container(
padding: EdgeInsets.symmetric(
horizontal: 100,
vertical: 100
),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
Stack(
alignment: Alignment.topRight,
overflow: Overflow.visible,
children: <Widget>[
TextFormField(
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(
borderSide: BorderSide.none
),
suffixIcon: IconButton(
icon: Icon(Icons.error, color: Colors.red,),
onPressed: () {
setState(() {
showTooltip = !showTooltip;
});
},
),
hintText: "Password"
),
validator: (value) {
if(value.isEmpty) {
setState(() {
showTooltip = true;
});
return "";
}
},
),
Positioned(
top: 50,
right: 10,
//You can use your own custom tooltip widget over here in place of below Container
child: showTooltip
? Container(
width: 250,
height: 50,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all( color: Colors.red, width: 2.0 ),
borderRadius: BorderRadius.circular(10)
),
padding: EdgeInsets.symmetric(horizontal: 10),
child: Center(
child: Text(
"Your passwords must match and be 6 characters long.",
),
),
) : Container(),
)
],
),
RaisedButton(
child: Text("Validate"),
onPressed: () {
_formKey.currentState.validate();
},
),
],
),
),
),
);
}
}
类LoginAlternate扩展StatefulWidget{
@凌驾
_LoginAlternateState createState()=>\u LoginAlternateState();
}
类_LoginAlternateState扩展状态{
GlobalKey _formKey=GlobalKey();
bool showTooltip=false;
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:颜色。蓝灰色,
主体:容器(
填充:EdgeInsets.symmetric(
横向:100,
垂直:100
),
孩子:表格(
键:_formKey,
子:列(
儿童:[
堆叠(
对齐:alignment.topRight,
溢出:溢出。可见,
儿童:[
TextFormField(
装饰:输入装饰(
是的,
fillColor:Colors.white,
边框:大纲输入边框(
borderSide:borderSide.none
),
后缀:图标按钮(
图标:图标(Icons.error,颜色:Colors.red,),
已按下:(){
设置状态(){
showTooltip=!showTooltip;
});
},
),
hintText:“密码”
),
验证器:(值){
if(value.isEmpty){
设置状态(){
showTooltip=true;
});
返回“”;
}
},
),
定位(
前50名,
右:10,,
//您可以在这里使用自己的自定义工具提示小部件来代替下面的容器
子:显示工具提示
?容器(
宽度:250,
身高:50,
装饰:盒子装饰(
颜色:颜色,白色,
边框:边框。全部(颜色:Colors.red,宽度:2.0),
边界半径:边界半径。圆形(10)
),
填充:边缘组。对称(水平:10),
儿童:中心(
子:文本(
“您的密码必须匹配且长度为6个字符。”,
),
),
):Container(),
)
],
),
升起的按钮(
子项:文本(“验证”),
已按下:(){
_formKey.currentState.validate();
},
),
],
),
),
),
);
}
}
See@Sniffer我知道如何在原生Android中实现这一点。我的问题是如何在颤振中做到这一点?可能重复@Sniffer No它不是重复的将尝试并让您知道
class LoginAlternate extends StatefulWidget {
@override
_LoginAlternateState createState() => _LoginAlternateState();
}
class _LoginAlternateState extends State<LoginAlternate> {
GlobalKey<FormState> _formKey = GlobalKey();
bool showTooltip = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blueGrey,
body: Container(
padding: EdgeInsets.symmetric(
horizontal: 100,
vertical: 100
),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
Stack(
alignment: Alignment.topRight,
overflow: Overflow.visible,
children: <Widget>[
TextFormField(
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(
borderSide: BorderSide.none
),
suffixIcon: IconButton(
icon: Icon(Icons.error, color: Colors.red,),
onPressed: () {
setState(() {
showTooltip = !showTooltip;
});
},
),
hintText: "Password"
),
validator: (value) {
if(value.isEmpty) {
setState(() {
showTooltip = true;
});
return "";
}
},
),
Positioned(
top: 50,
right: 10,
//You can use your own custom tooltip widget over here in place of below Container
child: showTooltip
? Container(
width: 250,
height: 50,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all( color: Colors.red, width: 2.0 ),
borderRadius: BorderRadius.circular(10)
),
padding: EdgeInsets.symmetric(horizontal: 10),
child: Center(
child: Text(
"Your passwords must match and be 6 characters long.",
),
),
) : Container(),
)
],
),
RaisedButton(
child: Text("Validate"),
onPressed: () {
_formKey.currentState.validate();
},
),
],
),
),
),
);
}
}