0

我正在使用 flutter_inappwebview 在颤振应用程序中显示我的网站。

在我的应用程序一个抽屉菜单中,当我单击抽屉菜单项更改flutter_inappwebview URL 并在flutter_inappwebview 中加载新URL 时。

抽屉菜单和 flutter_inappwebview 都在不同的页面中。

这可以在颤振中实现吗

这是我的 flutter_inappwebview 代码

class homePage extends StatefulWidget {
  @override
  static const String routeName = '/homePage';
  _homePageState createState() => _homePageState();
}

class _homePageState extends State<homePage> {
  String url = "";
  double progress = 0;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
    debugShowCheckedModeBanner:false,
    home: Scaffold(
      body: Container(
        child: Column(children: <Widget>[
          Container(
            padding: EdgeInsets.all(2.0),
            child: progress < 1.0
                ? LinearProgressIndicator(value: progress)
                : Container()),
            Expanded(
              child: Container(
              margin: const EdgeInsets.all(0.0),
              child: InAppWebView(
                initialUrl: selectedUrl,
                initialOptions: InAppWebViewGroupOptions(
                  crossPlatform: InAppWebViewOptions(
                    debuggingEnabled: true,
                  )
               ),
               onWebViewCreated: (InAppWebViewController controller) {
                 _webViewController = controller;
               },
               onLoadStart: (InAppWebViewController controller, String url) {
                 setState(() {
                   this.url = url;
                 });
               },
               onLoadStop: (InAppWebViewController controller, String url) async {
                 setState(() {
                   this.url = url;
                 });
               },
               onProgressChanged: (InAppWebViewController controller, int progress) {
                 setState(() {
                   this.progress = progress / 100;
                 });
               },
             ),
           ),
         ),
       ]),
     ),
   ),
 );

} }

4

3 回答 3

3

您可以通过以下代码监听 URL 变化。

InAppWebView(
        ...
        onUpdateVisitedHistory: (_, Uri uri, __) {
              // uri containts newly loaded url
        },
}
于 2021-04-13T08:59:32.093 回答
1

你可以使用 controller.loadUrl() 方法:

InAppWebViewController? webViewController;


InAppWebView(
    initialUrlRequest: URLRequest(
      url: Uri.parse(url),
    ),
  onWebViewCreated: (controller){
      webViewController = controller;
  },
)


webViewController?.loadUrl(urlRequest: URLRequest(
  url: Uri.parse(url),
));

于 2021-10-04T05:04:08.650 回答
0

在此处输入图像描述

textform从,drawer小部件或按下小部件时button加载新的 url

widget._controller!.loadUrl(
                    urlRequest:
                        URLRequest(url: Uri.parse("www.google.com")));

InAppWebViewController在内部侦听器中初始化onwebviewcreated

  onWebViewCreated: (controller) {
                    widget._controller = controller;
                  },

示例代码:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

void main() => runApp(MyHomePage());

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: homePage(),
    );
  }
}

class homePage extends StatefulWidget {
  @override
  static const String routeName = '/homePage';
  InAppWebViewController? _controller;

  _homePageState createState() => _homePageState();
}
final GlobalKey webViewKey = GlobalKey();
InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
    crossPlatform: InAppWebViewOptions(
      useShouldOverrideUrlLoading: true,
      mediaPlaybackRequiresUserGesture: false,
    ),
    android: AndroidInAppWebViewOptions(
      useHybridComposition: true,
    ),
    ios: IOSInAppWebViewOptions(
      allowsInlineMediaPlayback: true,
    ));
class _homePageState extends State<homePage> {
  String url = "";
  double progress = 0;


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        drawer: Container(
          width: 125,
          child: Drawer(
            child: ListView(
              // Important: Remove any padding from the ListView.
              padding: EdgeInsets.zero,
              children: [
                const DrawerHeader(
                  decoration: BoxDecoration(
                    color: Colors.blue,
                  ),
                  child: Text('Drawer Header'),
                ),
                ListTile(
                  title: const Text('google.com'),
                  onTap: () {
                    widget._controller!.loadUrl(
                        urlRequest:
                            URLRequest(url: Uri.parse("www.google.com")));

                    // Update the state of the app.
                    // ...
                  },
                ),
                ListTile(
                  title: const Text('yahoo.com'),
                  onTap: () {
                    widget._controller!.loadUrl(
                        urlRequest:
                            URLRequest(url: Uri.parse("www.yahoo.com")));
                    // ...
                  },
                ),
              ],
            ),
          ),
        ),
        body: Container(
          child: Column(children: <Widget>[
            Container(
                padding: EdgeInsets.all(2.0),
                child: progress < 1.0
                    ? LinearProgressIndicator(value: progress)
                    : Container()),
            Expanded(
              child: Container(
                margin: const EdgeInsets.all(0.0),
                child: InAppWebView(
                  key: webViewKey,
                  initialOptions: options,
                  initialUrlRequest:
                      URLRequest(url: Uri.parse("https://inappwebview.dev/")),
                  onWebViewCreated: (controller) {
                    widget._controller = controller;
                  },
                  onProgressChanged:
                      (InAppWebViewController controller, int progress) {
                    setState(() {
                      this.progress = progress / 100;
                    });
                  },
                ),
              ),
            ),
          ]),
        ),
      ),
    );
  }
}
于 2022-02-13T18:39:20.190 回答