flutter - 如何在颤动中显示文本字段可见性单击警报对话框按钮
问题描述
我想要当我单击警报对话框的确定按钮时,然后显示文本字段可见性(此文本字段在“打开对话框”按钮下方给出,并且此文本字段可见性默认为隐藏)。但它没有发生我不明白这里的错误是什么。
我的代码:
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
void main()=> runApp(MyHomePages());
class MyHomePages extends StatelessWidget{
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "KYC Formm",
theme: ThemeData(
primarySwatch: Colors.green,
),
home: MyHomePageDesign(title: "Kyc form"),
);
}
}
class MyHomePageDesign extends StatefulWidget {
MyHomePageDesign({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePageDesign> {
bool _isVisible = false ;
bool _isRcVisible = false;
String textRcValue;
TextEditingController _textFieldControllerD = TextEditingController();
TextEditingController _textRcFieldController = TextEditingController();
void _displayDialog(BuildContext context) async {
return showDialog(
barrierDismissible: true,
context: context,
builder: (context) {
// this was required, rest is same
return StatefulBuilder(
builder: (BuildContext context, setState){
return AlertDialog(
title: Text('Choose any one'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Text("Capture Photo",
style: TextStyle(
fontSize: 20.0,),),
),
SizedBox(height: 10.0,),
Align(
alignment: Alignment.centerLeft,
child: Text("Pick Photo",
style: TextStyle(
fontSize: 20.0,),),
),
SizedBox(height: 10.0,),
Align(
alignment: Alignment.centerLeft,
child: InkWell(
onTap: (){
setState(() => _isVisible = !_isVisible);
},
child: Text("Enter RC Number",
style: TextStyle(
fontSize: 20.0,),),
),
),
SizedBox(height: 10.0,),
Visibility(
visible: _isVisible,
child: Align(
alignment: Alignment.centerLeft,
child: Container(
width: 200.0,
height: 60.0,
padding: const EdgeInsets.only(top: 10.0, left: 0.0,bottom: 10.0,right: 0.0),
child: TextField(
controller: _textFieldControllerD,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Enter RC No',
),
style: TextStyle(
color: Colors.red,
fontSize: 18.0,),),
),
),
),
Align(
alignment: Alignment.centerRight,
child: new FlatButton(
child: new Text('OK',
style: TextStyle(
color: Colors.teal
),),
onPressed: () {
textRcValue= _textFieldControllerD.text;
setState(() {
_isRcVisible = true;
});
_textRcFieldController.text= textRcValue;
print("RcValue:");
print(textRcValue);
Navigator.of(context).pop();
},
),
)
]
)
);
}
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Demo'),
),
body: Center(
child:Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
width: 160.0,
height: 50.0,
padding: const EdgeInsets.only(top: 0.0, left: 5.0,bottom: 10.0,right: 0.0),
child: RaisedButton(
color: Colors.green,
child: Text('Open Dialog'),
onPressed: () {
_displayDialog(context);
},
),
),
Visibility(
visible: _isRcVisible,
child: TextField(
controller: _textRcFieldController,
decoration: InputDecoration(hintText: "Enter Rc Number"),
onTap: (){},
),
),
],
),
),
);
}
}
解决方案
您需要更改setState
您在对话框中调用的方法,以使您的主小部件isRcVisible
中的函数调用成为真实的。State
_setRcVisible() { // this is new
this.setState(() {
_isRcVisible = true;
});
}
void _displayDialog(BuildContext context) async {
return showDialog(
barrierDismissible: true,
context: context,
builder: (context) {
// this was required, rest is same
return StatefulBuilder(
builder: (BuildContext context, setState){
return AlertDialog(
title: Text('Choose any one'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Text("Capture Photo",
style: TextStyle(
fontSize: 20.0,),),
),
SizedBox(height: 10.0,),
Align(
alignment: Alignment.centerLeft,
child: Text("Pick Photo",
style: TextStyle(
fontSize: 20.0,),),
),
SizedBox(height: 10.0,),
Align(
alignment: Alignment.centerLeft,
child: InkWell(
onTap: (){
setState(() => _isVisible = !_isVisible);
},
child: Text("Enter RC Number",
style: TextStyle(
fontSize: 20.0,),),
),
),
SizedBox(height: 10.0,),
Visibility(
visible: _isVisible,
child: Align(
alignment: Alignment.centerLeft,
child: Container(
width: 200.0,
height: 60.0,
padding: const EdgeInsets.only(top: 10.0, left: 0.0,bottom: 10.0,right: 0.0),
child: TextField(
controller: _textFieldControllerD,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Enter RC No',
),
style: TextStyle(
color: Colors.red,
fontSize: 18.0,),),
),
),
),
Align(
alignment: Alignment.centerRight,
child: new FlatButton(
child: new Text('OK',
style: TextStyle(
color: Colors.teal
),),
onPressed: () {
textRcValue= _textFieldControllerD.text;
_setRcVisible(); // this is new
_textRcFieldController.text= textRcValue;
print("RcValue:");
print(textRcValue);
Navigator.of(context).pop();
},
),
)
]
)
);
}
);
});
}
推荐阅读
- r - 如何使用 ggplot 为 GLM 模型制作效果图?
- c# - 实例化新字符串后,“对象(差异)”列(诊断工具)的值不会更改
- c++ - Qt - 如何从派生类发出信号?
- nginx - 如何让后端 api 处理 https 证书?
- javascript - 如何在垂直选项卡中呈现数据
- java - 鼠标光标移动后不改变位置
- sql - 从前一小时获取值并从 SQL 中的当前值中减去
- swift - 界面生成器上的 IBDesignable 问题
- c# - 为什么 C# 编译器不抛出 The given expression never of the provided type 警告
- c# - 时间:2019-01-01 标签:c#decoratorpatternsmultiplepropertieswrappingmultipletimes