【发布时间】:2018-07-27 22:40:51
【问题描述】:
我有一个 Flutter 应用程序,我在其中使用了 flutter_web_view 包。我在几个不同的文件中使用它,并且很想创建自己的文件并简单地在我的应用程序中的任何位置使用 _launchwebview 函数进行引用,因为需要几行代码才能使其工作。我知道如何引用文件和传递信息,但不知道方法/函数。这是课程代码...
import 'package:flutter/material.dart';
import 'package:flutter_web_view/flutter_web_view.dart';
class ShopClass extends StatefulWidget {
@override
ShopClassState createState() => new ShopClassState();
}
class ShopClassState extends State<ShopClass> {
String _redirectedToUrl;
FlutterWebView flutterWebView = new FlutterWebView();
bool _isLoading = false;
@override
initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
Widget leading;
if (_isLoading) {
leading = new CircularProgressIndicator();
}
var columnItems = <Widget>[
new MaterialButton(
onPressed: launchWebViewExample, child: new Text("Launch"))
];
if (_redirectedToUrl != null) {
columnItems.add(new Text("Redirected to $_redirectedToUrl"));
}
var app = new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
leading: leading,
),
body: new Column(
children: columnItems,
),
),
);
return app;
}
void launchWebViewExample() {
if (flutterWebView.isLaunched) {
return;
}
flutterWebView.launch("https://apptreesoftware.com",
headers: {
"X-SOME-HEADER": "MyCustomHeader",
},
javaScriptEnabled: false,
toolbarActions: [
new ToolbarAction("Dismiss", 1),
new ToolbarAction("Reload", 2)
],
barColor: Colors.green,
tintColor: Colors.white);
flutterWebView.onToolbarAction.listen((identifier) {
switch (identifier) {
case 1:
flutterWebView.dismiss();
break;
case 2:
reload();
break;
}
});
flutterWebView.listenForRedirect("mobile://test.com", true);
flutterWebView.onWebViewDidStartLoading.listen((url) {
setState(() => _isLoading = true);
});
flutterWebView.onWebViewDidLoad.listen((url) {
setState(() => _isLoading = false);
});
flutterWebView.onRedirect.listen((url) {
flutterWebView.dismiss();
setState(() => _redirectedToUrl = url);
});
}
void reload() {
flutterWebView.load(
"https://google.com",
headers: {
"X-SOME-HEADER": "MyCustomHeader",
},
);
}
}
如何在另一个班级中使用launchWebViewExample?
【问题讨论】:
-
你能分享一些代码吗?目前还不清楚问题是什么。使用该功能导入文件应该可以。如果你想传递函数,那么字段或其他变量并没有真正的区别。
-
@GünterZöchbauer 我已经从我创建的文件中添加了代码。我想用它在另一个文件中启动 WebView 而不将所有这些行添加到每个文件中。
-
_ 在函数名前面将使其私有 _launchwebview,
标签: function methods dart flutter