从另一个dart文件调用函数

问题描述 投票:0回答:1

我有一个包含很多代码的主页,我想在另一个dart文件中分离用于构建TextInput填充对话框的代码,并在HomePage上下文中对该函数进行调用。

//DialogBox code

import 'package:flutter/material.dart';

Future<String> _shareDialogBox(BuildContext context) async {
  String shareContent = '';
  return showDialog<String>(
    context: context,
    barrierDismissible:
        false, // dialog is dismissible with a tap on the barrier
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Enter current team'),
        content: new Row(
          children: <Widget>[
            new Expanded(
                child: new TextField(
              autofocus: true,
              decoration: InputDecoration(
                //prefixIcon: Icon(Icons.note_add),
                //icon: Icon(Icons.note_add),
                hintText: 'Add Description',
                enabledBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.all(Radius.circular(15.0)),
                  borderSide: BorderSide(color: Colors.grey),
                ),
                focusedBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.all(Radius.circular(15.0)),
                  borderSide: BorderSide(color: Colors.grey),
                ),
              ),
              onChanged: (value) {
                shareContent = value;
              },
            ))
          ],
        ),
        actions: <Widget>[
          FlatButton(
            child: Text('Share on Facebook'),
            onPressed: () {
              Navigator.of(context).pop(shareContent);
            },
          ),
          FlatButton(
            child: Text('Share on Twitter'),
            onPressed: () {
              Navigator.of(context).pop(shareContent);
            },
          )
        ],
      );
    },
  );
}

现在我已经在文件DialogBox.dart中编写了此代码

我想在onTap()事件上按如下所示在Homepage.dart中调用_shareDialogBox()函数。

   import 'package:app_name/ui_components/ShareDialogBox.dart';

                           GestureDetector(
                              onTap: () async {
                                await _shareDialogBox(context);
                              },
                              child: Row(
                                children: <Widget>[
                                  Icon(
                                    Icons.reply,
                                    size: 15,
                                    color: Colors.black54,
                                  ),
                                  SizedBox(width: 5),
                                  Text("Share",
                                      style: TextStyle(
                                          fontSize: 14.0,
                                          color: Colors.black54)),
                                ],
                              )
                          ),

我说错了

error: The method '_shareDialogBox' isn't defined for the class '_MyHomePageState'. (undefined_method at ---- lib/HomePage.dart:308)

有人可以帮忙了解为什么会这样吗。

flutter dart flutter-layout dart-html
1个回答
0
投票

看起来正确,除了使用下划线。这使其成为私有方法/变量。

并且只能在同一文件中使用。(或其文件以part of 'library'开头的文件>

只需从_shareDialogBox重命名为shareDialogBox

© www.soinside.com 2019 - 2024. All rights reserved.