I have a Flutter app where I'm using the flutter_web_view package. I'm using it over several different files and would love to create its own file and simply reference with the _launchwebview function anywhere in my app because there are several lines of code needed in order to make it work. I know how to reference files and pass information but not methods/functions. Here is the class code...
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", }, ); } }
How can I use launchWebViewExample
in another class?
You need to import the file with the variable dateTime. Then you have 2 ways: make the variable static: static var dateTime. for this way you don't need todo know anything.
In Flutter this can be done using a stateful widget and calling your code in the initState function. What if you want to call it from a stateless widget? Well, that's possible too. Use a stateful widget as a your root widget that you can provide a callback function too to execute your startup logic.
You can write a file with just that function, like:
test.dart
void launchWebView () { print("1234"); }
and then import that file like this:
main.dart
import "test.dart"; class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { launchWebView();
It is not really clean, but you can do that. Alternatively you can use a class with a static method like:
class test { static void foo() { print("1234"); } }
and then in your code invoke it like that (after the import):
test.foo();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With