Cha*_* Jr 23 methods function dart flutter
我有一个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",
},
);
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能launchWebViewExample在另一堂课中使用?
Ant*_*ace 38
您可以使用该函数编写文件,例如:
test.dart
void launchWebView () {
print("1234");
}
Run Code Online (Sandbox Code Playgroud)
然后像这样导入该文件:
main.dart
import "test.dart";
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
launchWebView();
Run Code Online (Sandbox Code Playgroud)
它不是很干净,但你可以做到这一点.或者,您可以使用具有静态方法的类,如:
class test {
static void foo() {
print("1234");
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的代码中调用它(导入后):
test.foo();
Run Code Online (Sandbox Code Playgroud)
Pra*_*tti 16
mixin而不是全局函数!我在几个不同的文件中使用它,并且希望创建自己的文件并在应用程序中的任何位置引用 _launchwebview 函数,因为需要几行代码才能使其工作。
带下划线的方法是给定库私有的。因此,如果我们_launchwebview在一个文件中定义,则该函数位于一个迷你库(Source)中。因此,该方法只能在该文件中访问。我探索了如何跨文件公开私有方法,但发现解决方案很混乱。因此,我认为使用公共函数可以更好地回答这个问题。问题是在不同的类中实现共享功能,而不是简单地提供访问。
我选择为这个特定问题添加此解决方案,因为该方法(启动 Web 视图)最好在每个适当的Widget类中实现。请注意,扩展方法在这种情况下也可以工作,但 Flutter 更喜欢组合而不是继承。
如果我们想将公共的(即非下划线的方法)移动到不同的类而不进行复制粘贴,我们可以尝试几种概述的方法。我在下面详细介绍了现有选项的缺点。
part指令使我们能够通过私有实现复制一个文件的源代码并在另一个文件中使用它。但这是不鼓励的,因为该技术增加了二进制大小,违背了Effective Dart使用指南,并且显然是一种代码味道,因为我们复制粘贴相同的代码。provider和其他包,如GetX.Function为类的属性可能会导致进一步的微妙错误。例如,我们可以将任何函数分配给该属性,因为没有函数签名类型检查。此外,这不是一个具有代码重复的可扩展解决方案。当我们想要减少重复代码但避免扩展整个类时,Dart 内置支持选择性地向类添加函数(源代码)。关键字mixin通过将类与某些特定逻辑混合来实现此目的。如果需要,我们可以将其限制mixin为特定的子类on。
mixin LaunchWebView on StatelessWidget { // you can also constrain the mixin to specific classes using on in this line.
void launchWebView() {
// Add web view logic here. We can add variables to the mixin itself as well.
}
}
Run Code Online (Sandbox Code Playgroud)
class ExampleClass extends StatelessWidget with LaunchWebView {
Widget build(BuildContext context) {
....
}
void testFunction() {
// We now have access to launchWebView().
launchWebView();
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过不同的方式做到这一点:
在文件中定义您的函数,例如global.dart:
void func() => print('Hello');
Run Code Online (Sandbox Code Playgroud)
要在任何文件中使用它,只需调用:
func();
Run Code Online (Sandbox Code Playgroud)
创建一个类,Foo并在其中定义您的函数:
class Foo {
static void func() => print('Hello');
}
Run Code Online (Sandbox Code Playgroud)
要在任何文件中使用它,只需调用
Foo.func();
Run Code Online (Sandbox Code Playgroud)
如果你想在任何类中使用该函数:
创建一个混合,说Bar:
mixin Bar {
void func() => print('Hello');
}
Run Code Online (Sandbox Code Playgroud)
要在类中使用它,只需使用with关键字后跟 mixin 即可。
class Baz with Bar {
void main() => func();
}
Run Code Online (Sandbox Code Playgroud)
如果你想限制 mixin 与任何类一起使用:
class Foo {}
Run Code Online (Sandbox Code Playgroud)
创建一个 mixin,说出Bar哪个在 上Foo。
mixin Bar on Foo {
void func() => print('Hello');
}
Run Code Online (Sandbox Code Playgroud)
要使用Barmixin,我们需要扩展Foo类,因为这就是它的作用。
class Baz extends Foo with Bar {
void main() => func();
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以只在一个类中声明所有函数(帮助器),然后将它们作为参数传递给其他类。
//The class which contains your functions
class HelperFunction{
//Define your method
void launchWebView () {
print("1234");
}
//Pass that function to a class
MyHomePage(launchWebView);
}
//The class which receives the function.
class MyHomePage extends StatefulWidget{
//Retrieve the function and store it to a variable of type Function.
final Function launchWebView;
MyHomePage(this.launchWebView);
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
//Access that function in State class using widget keyword.
widget.launchWebView();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31619 次 |
| 最近记录: |