我有一个 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
?