[英]Flutter : Get AlertDialog From Another Dart File
i need help guys.我需要帮助伙计们。 I have 2 dart file : main.dart and alertform.dart.我有 2 个 dart 文件:main.dart 和 alertform.dart。 some cases require using this method in my application.有些情况需要在我的应用程序中使用这种方法。 I want to try accessing the alerdialog from alertform.dart on the button on main.dart.我想尝试在 main.dart 上的按钮上从 alertform.dart 访问 alerdialog。 is that possible?那可能吗? this my code:这是我的代码:
main.dart main.dart
import 'package:flutter/material.dart';
import 'alertform.dart';
class MainPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: new Text('Test'),
),
body: new Column(
children: <Widget>[
RaisedButton(
child: new Text('Show Alert'),
onPressed: (){
CommentForm();
},
)
],
),
);
}
}
alertform.dart alertform.dart
import 'package:flutter/material.dart';
class AlertForm extends StatefulWidget {
@override
_AlertFormState createState() => _AlertFormState();
}
class _AlertFormState extends State<AlertForm> {
void _showDialog() {
// flutter defined function
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Alert Dialog title"),
content: new Text("Alert Dialog body"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Container(
);
}
}
I don't know why you want to call this _dialog from outside class, where you can call inside your class.我不知道你为什么要从课外调用这个 _dialog,在那里你可以在你的课内调用。 But if you want to do then you can try this code.但是如果你想这样做,你可以试试这个代码。
import 'package:flutter/material.dart';
import 'alertform.dart';
class MainPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: new Text('Test'),
),
body: new Column(
children: <Widget>[
RaisedButton(
child: new Text('Show Alert'),
onPressed: (){
AlertFormState(context).showDialogBox;
},
)
],
),
);
}
}**
import 'package:flutter/material.dart';
class AlertForm extends StatefulWidget {
@override
AlertFormState createState() => AlertFormState();
}
class AlertFormState extends State<AlertForm> {
void showDialogBox(BuildContext context) {
// flutter defined function
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Alert Dialog title"),
content: new Text("Alert Dialog body"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Container(
);
}
}
Create a new Class :创建一个新类:
class AlertDemo{
void showDialog(BuildContext context) {
// flutter defined function
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Alert Dialog title"),
content: new Text("Alert Dialog body"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
And then call using the AlertDemo class instance call the showDialog Method.然后调用使用AlertDemo 类实例调用showDialog 方法。
RaisedButton(
child: new Text('Show Alert'),
onPressed: (){
AlertDemo().showDialog(context);
},
)
I havent tested this as i am travelling and wrote on mobile , so if it didnt worked i will edit the correct one when i reach.我在旅行时还没有测试过这个并在手机上写过,所以如果它不起作用,我会在到达时编辑正确的。
It's simple.这很简单。
main.dart main.dart
import 'package:flutter/material.dart';
import 'showdialog.dart';
void main() => runApp(MaterialApp(
home: HomePage(),
));
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child:
ElevatedButton(
child: Text("Show"),
onPressed: () {
showDialogBox(context);
}
),
);
}
}
showdialog.dart显示对话框.dart
import 'package:flutter/material.dart';
void showDialogBox(BuildContext context) {
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text("title"),
content: new Text("body"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("close"),
onPressed: () {
Navigator.of(context).pop();
},
)
]
);
}
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.