简体   繁体   English

Flutter:从另一个 Dart 文件获取 AlertDialog

[英]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.

相关问题 如何从颤动的另一个飞镖文件中获取地图长度? - How to get Map length from another dart file in flutter? 如何在flutter文件中获取一个.dart文件到另一个.dart文件的变量 - How to get a variable of .dart file to another .dart file in flutter 如何从另一个文件调用 alertdialog? 使用 flutter - How to call alertdialog from another file? using flutter 如何从另一个 dart 文件中获取 object - How to get the object from another dart file 在 Dart (Flutter) 中从一种方法到另一种方法获取值 - Get a value from one method to another method in Dart (Flutter) Flutter、Dart - 如何使用另一个文件中的类属性 - Flutter, Dart - How to use a class property from another file Flutter:从另一个 dart 文件路由到页面浏览小部件的特定页面 - Flutter: Route to a specific page of pageview widget from another dart file 如何从 flutter 中的另一个 dart 文件导入和使用 function - How to import and use a function from another dart file in flutter 如何从flutter dart中firebasestorage中该文件的链接获取文件名 - How to get name of a file from the link of that file in firebasestorage in flutter dart 如何在 Dart / Flutter 中使用另一个文件的函数? - How to use Functions of another File in Dart / Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM