[英]Calling a function from another dart file
I have a homepage with a lot of code, I want to separate the code used for building a TextInput filled dialogbox in another dart file and make a call to that function in the HomePage context.我有一个包含大量代码的主页,我想在另一个 dart 文件中分离用于构建 TextInput 填充对话框的代码,并在 HomePage 上下文中调用该 function。
//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);
},
)
],
);
},
);
}
Now I have written this code in file DialogBox.dart现在我已经在文件 DialogBox.dart 中编写了这段代码
I want to make a call to the _shareDialogBox()
function in my Homepage.dart on press like this on the onTap() event.我想在我的 Homepage.dart 在 onTap() 事件上按这样的方式调用
_shareDialogBox()
function。
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)),
],
)
),
I get the error saying我收到错误消息
error: The method '_shareDialogBox' isn't defined for the class '_MyHomePageState'. (undefined_method at ---- lib/HomePage.dart:308)
Can someone help understand why this is happening.有人可以帮助理解为什么会这样。
looks correct except the usage of underscore.除了使用下划线外,看起来是正确的。 this makes it a private method/variable.
这使它成为私有方法/变量。
and can only be used in the same file.并且只能在同一个文件中使用。 (or of its a file that starts with
part of 'library'
(或其以
part of 'library'
开头的文件
just rename from _shareDialogBox
to shareDialogBox
只需从
_shareDialogBox
重命名为shareDialogBox
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.