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

5

我需要帮助,有两个Dart文件:main.dart和alertform.dart。我的应用程序中有些情况需要使用此方法。我想尝试从main.dart的按钮上访问alertform.dart中的alerdialog,这可能吗?这是我的代码:

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

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(

    );
  }
}
3个回答

7

我不知道为什么你想从类的外部调用这个对话框,因为你可以在类内部调用。但如果你确实想这样做,你可以尝试这段代码。

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(

    );
  }
}

我尝试过了,但是出现了一行错误:CommentForm(context)._showDialog; - UnderdoX
showDialog 是一个私有方法,你可以从外部访问它,去掉“_”。 - satish
从CommentFormState我得到了错误:dart]在类'StatusFeed'中,方法'CommentFormState'未定义[undefined_method] [dart]避免使用不必要的语句[unnecessary_statements]。 - UnderdoX
你还在寻找答案吗? - satish

4

创建一个新类:

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();
            },
            ),
        ],
        );
    },
    );
}
}

然后使用AlertDemo类的实例调用showDialog方法。
 RaisedButton(
        child: new Text('Show Alert'),
        onPressed: (){
          AlertDemo().showDialog(context);
        },
      )

我还没有测试过这个,因为我正在旅行中并且是用手机写的,所以如果它没有起作用,我到达目的地后会编辑正确的版本。


2
不需要创建类,只需创建一个方法并调用它。 - satish

1

这很简单。

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

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();
                },
              )
            ]
        );
      }
  );
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接