为什么此警报对话框在flutter中不起作用



每当用户点击特定列表项时,我都会使用下面的代码来获得警报


import 'package:flutter/material.dart';
import 'package:rflutter_alert/rflutter_alert.dart';


class RamList extends StatelessWidget {

@override
Widget build(BuildContext context) {

return _myListView(context);
}
}
Widget _myListView(BuildContext context) {
final titles = [
'Part-1',
'part-2',
'part-3',
'part-4',
'part-5',
];
final numbers = [
'1 ',
'2 ',
'3 ',
'4 ',
'5 ',

];
functionOne() {
Navigator.push(
context, new MaterialPageRoute(builder: (context) =>  RamOne()));
}
functionTwo() {
Navigator.push(context, MaterialPageRoute(builder: (context) => RamTwo()));
}
functionThree() {
Navigator.push(
context, MaterialPageRoute(builder: (context) => RamThree()));
}
functionFour() {
Navigator.push(context, MaterialPageRoute(builder: (context) => RamFour()));
}
functionFive()=>_onAlertButtonPressed1; 


final List<Function> onTaps = [
functionOne,
functionTwo,
functionThree,
functionFour,
functionFive,
];
return ListView.builder(
itemCount: titles.length,
itemBuilder: (context, index) {
return Card(
elevation: 50,
child: InkWell(

child: Row(
children: <Widget>[

Container(

height: 100.0,
width:50.0,
decoration: BoxDecoration(
gradient:LinearGradientStyle.linearGradient(
orientation:LinearGradientStyle.ORIENTATION_HORIZONTAL,
gradientType: LinearGradientStyle.GRADIENT_TYPE_AMIN
)
),),
Container(
margin: EdgeInsets.all(10),
child: Text(
numbers[index],
)),

Flexible(child:  Container(
margin: EdgeInsets.all(10),
child: GradientText((titles[index]),
gradient:gradient,
style:TextStyle(fontSize:20.0,fontWeight:FontWeight.bold, ),
),
))
],
),
onTap: () => onTaps[index](),
));
});

}
_onAlertButtonPressed1(context) {
Alert(
context: context,
type: AlertType.info,
title: "Coming soon",
desc: "This link will be available in future updates",
).show();
}

我试着用rflutter警报来显示警报。一切正常,没有任何错误。但单击预定列表视图后不会出现警报。。为什么此代码不起作用。如果你有什么解决办法,请帮帮我。。

如果您需要更多信息,请发表评论。顺便说一句,控制台消息运行良好。控制台框中没有错误消息。

要在flutter中显示警报对话框,必须执行以下操作:

_onAlertButtonPressed1(context) {
AlertDialog alert = AlertDialog(
title: Text("Coming soon"),
content: Text("This link will be available in future updates"),
actions: [
//your actions (I.E. a button)
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}

您可以在此处查看文档。

还有一篇文章解释了如何在这里使用AlertDialog。

您有这个回调:functionFive()=>_onAlertButtonPressed;

它调用__onAlertButtonPressed_,但显示对话框的函数被称为_onAlertButtonPressed1(它的末尾有1(。

UPD:您的回调应该接受上下文和函数Five应该调用_onAlertButtonPressed1函数:

functionOne(BuildContext context) {
Navigator.push(
context, new MaterialPageRoute(builder: (context) => RamOne()));
}
functionTwo(BuildContext context) {
Navigator.push(context, MaterialPageRoute(builder: (context) => RamTwo()));
}
functionThree(BuildContext context) {
Navigator.push(
context, MaterialPageRoute(builder: (context) => RamThree()));
}
functionFour(BuildContext context) {
Navigator.push(context, MaterialPageRoute(builder: (context) => RamFour()));
}
functionFive(BuildContext context) => _onAlertButtonPressed1(context); 

并将上下文传递给回调:

onTap: () => onTaps[index](context),

相关内容

  • 没有找到相关文章

最新更新