flutter - Flutter:向文本添加自定义对话框
问题描述
单击文本时,我正在尝试创建自定义对话框。但对话框没有创建。下面添加对话框实现代码
child: new Center(
child: new RichText(
text: new TextSpan(
children: [
new TextSpan(
text: 'Accept the ',
style: new TextStyle(color: Colors.black),
),
new TextSpan(
text: 'Terms & Conditions',
style: new TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold),
recognizer: new TapGestureRecognizer()
..onTap = () {
CustomDialogBox(
title: "Custom Dialog Demo",
descriptions: "Hii all this is a custom dialog in flutter and you will be use in your flutter applications",
text: "Yes",
);
},
),
],
),
),
),
添加自定义对话框代码。在新 dart 页面中创建为新小部件并在应用程序中导入。我们需要它作为一个小部件,我们可以在应用程序中多次使用它。所以在一个新的飞镖页面中创建。
class CustomDialogBox extends StatefulWidget {
final String title, descriptions, text;
final Image img;
const CustomDialogBox({Key key, this.title, this.descriptions, this.text, this.img}) : super(key: key);
@override
_CustomDialogBoxState createState() => _CustomDialogBoxState();
}
class _CustomDialogBoxState extends State<CustomDialogBox> {
@override
Widget build(BuildContext context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(Constants.padding),
),
elevation: 0,
backgroundColor: Colors.transparent,
child: contentBox(context),
);
}
contentBox(context){
return Stack(
children: <Widget>[
Container(
padding: EdgeInsets.only(left: Constants.padding,top: Constants.avatarRadius
+ Constants.padding, right: Constants.padding,bottom: Constants.padding
),
margin: EdgeInsets.only(top: Constants.avatarRadius),
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: Colors.white,
borderRadius: BorderRadius.circular(Constants.padding),
boxShadow: [
BoxShadow(color: Colors.black,offset: Offset(0,10),
blurRadius: 10
),
]
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(widget.title,style: TextStyle(fontSize: 22,fontWeight: FontWeight.w600),),
SizedBox(height: 15,),
Text(widget.descriptions,style: TextStyle(fontSize: 14),textAlign: TextAlign.center,),
SizedBox(height: 22,),
Align(
alignment: Alignment.bottomRight,
child: FlatButton(
onPressed: (){
Navigator.of(context).pop();
},
child: Text(widget.text,style: TextStyle(fontSize: 18),)),
),
],
),
),
],
);
}
}
解决方案
CustomDialogBox 没有任何问题...您的 onTap 方法在发布的问题中不正确。
你应该做
showDialog(
context: context,
builder: (BuildContext context) => CustomDialogBox(),
)
推荐阅读
- android - 使用 GridLayout 在每个设备中显示相同大小的 Cardviews
- rest - REST API:PUT 到包含查询参数过滤器的资源是否可以接受?
- c++ - Boost::algorithms 是否被纳入标准库?
- git - 如何在两个拉取请求之间拆分一个拉取请求中的提交?
- sparql - 使用 SPARQL 过滤 DBpedia 结果并返回特定结果的最佳方法
- python - 提取未来的时间序列数据并加入相隔 12 小时的过去时间序列?
- c++ - 是否可以在标准弱指针之上实现一个非拥有的“稍微智能”的指针?
- python - 使用 pandas 规范化嵌套的 json 数据
- powerbi - Power BI Web.Contents 超时
- android - 如何将 BottomSheetDialogFragment 高度保持为始终 match_parent?