颤振:从另一个Dart文件获取AlertDialog
我需要帮助,伙计们。 我有两个dart文件:main.dart和alertform.dart。有些情况下需要在我的应用程序中使用此方法。 我想尝试从main.dart按钮上的alertform.dart访问alerdialog。可能吗?这是我的代码: 主飞镖颤振:从另一个Dart文件获取AlertDialog,dart,flutter,Dart,Flutter,我需要帮助,伙计们。 我有两个dart文件:main.dart和alertform.dart。有些情况下需要在我的应用程序中使用此方法。 我想尝试从main.dart按钮上的alertform.dart访问alerdialog。可能吗?这是我的代码: 主飞镖 import 'package:flutter/material.dart'; import 'alertform.dart'; class MainPage extends StatelessWidget { @override
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”;
类MainPage扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:新文本(“测试”),
),
正文:新栏目(
儿童:[
升起的按钮(
子项:新文本(“显示警报”),
已按下:(){
注释形式();
},
)
],
),
);
}
}
飞镖
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(
);
}
}
导入“包装:颤振/材料.省道”;
类AlertForm扩展了StatefulWidget{
@凌驾
_AlertFormState createState()=>\u AlertFormState();
}
类_AlertFormState扩展了状态{
void_showDialog(){
//颤振定义函数
显示对话框(
上下文:上下文,
生成器:(BuildContext上下文){
//返回对话框类型的对象
返回警报对话框(
标题:新文本(“警报对话框标题”),
内容:新文本(“警报对话框正文”),
行动:[
//通常是对话框底部的按钮
新扁平按钮(
子项:新文本(“关闭”),
已按下:(){
Navigator.of(context.pop();
},
),
],
);
},
);
}
@凌驾
小部件构建(构建上下文){
返回容器(
);
}
}
我不知道你为什么要在课堂外调用这个对话框,你可以在课堂内调用它。但是如果你想这样做,你可以试试这段代码
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(
);
}
}
导入“包装:颤振/材料.省道”;
导入“alertform.dart”;
类MainPage扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:新文本(“测试”),
),
正文:新栏目(
儿童:[
升起的按钮(
子项:新文本(“显示警报”),
已按下:(){
AlertFormState(上下文).showDialogBox;
},
)
],
),
);
}
}**
进口“包装:颤振/材料.省道”;
类AlertForm扩展了StatefulWidget{
@凌驾
AlertFormState createState()=>AlertFormState();
}
类AlertFormState扩展了状态{
无效显示对话框(BuildContext上下文){
//颤振定义函数
显示对话框(
上下文:上下文,
生成器:(BuildContext上下文){
//返回对话框类型的对象
返回警报对话框(
标题:新文本(“警报对话框标题”),
内容:新文本(“警报对话框正文”),
行动:[
//通常是对话框底部的按钮
新扁平按钮(
子项:新文本(“关闭”),
已按下:(){
Navigator.of(context.pop();
},
),
],
);
},
);
}
@凌驾
小部件构建(构建上下文){
返回容器(
);
}
}
创建一个新类:
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();
},
),
],
);
},
);
}
}
我还没有测试过这个,因为我正在旅行并在手机上写作,所以如果它不起作用,我将在到达时编辑正确的版本。我尝试过,但我得到了一个行错误:CommentForm(context)。\u showDialog_showDialog是一个私有方法,您可以从外部访问它,删除ux。删除“x”没有区别。在CommentFormState,我得到错误:dart]没有为类“StatusFeed”定义方法“CommentFormState”。[undefined_method][dart]避免使用不必要的语句。[不必要的_语句]不过,你还在寻找答案吗?不需要创建类,你只能创建一个方法并调用它。
RaisedButton(
child: new Text('Show Alert'),
onPressed: (){
AlertDemo().showDialog(context);
},
)