添加依赖

flutter_inappwebview: ^6.0.0 #嵌套网页

代码

class PictureWebUrl extends StatefulWidget {
  final String weburl;

  PictureWebUrl({Key? key, required this.weburl}) : super(key: key);

  @override
  State<PictureWebUrl> createState() => _PictureWebUrlState();
}

class _PictureWebUrlState extends State<PictureWebUrl> {
  late InAppWebViewController _webViewController;  //声明 InAppWebViewController 控制器

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        double width = constraints.maxWidth;
        double height = constraints.maxHeight;

        return SizedBox(
          width: width,
          height: height,
          child: Center(
            child: Container(
              margin: EdgeInsets.fromLTRB(width / 9, height / 25, 0, 0),
              child: InAppWebView(
                initialUrlRequest: URLRequest(
                  url: WebUri(Uri.parse("${widget.weburl}").toString()),
                  // url: Uri.parse(widget.weburl),
                ),
                //onWebViewCreated 的作用就是在 WebView 创建完成后,提供一个机会让开发者执行一些初始化操作,
               // 同时获取 WebView 的控制器,以便后续对 WebView 进行操作
                onWebViewCreated: (controller) {   
                  _webViewController = controller;  // 当 WebView 创建完成时,将控制器赋值
                },
              ),
            ),
          ),
        );
      },
    );
  }

  @override
  void didUpdateWidget(covariant PictureWebUrl oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.weburl != oldWidget.weburl) {
      if (_webViewController != null) {
        _webViewController.loadUrl(
            urlRequest: URLRequest(
          url: WebUri(Uri.parse("${widget.weburl}").toString()),
        ));
      }
    }
  }
}

 切换网址

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  InAppWebViewController? webViewController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('InAppWebView Example'),
          actions: [
            GestureDetector(
                onTap: () {
                  print("网址切换中,请稍等----------");
                  webViewController?.loadUrl(
                      urlRequest: URLRequest(
                          url: WebUri(Uri.parse('https://www.alibabagroup.com/')
                              .toString())));
                },
                child: Container(
                  color: Colors.red,
                  height: 50,
                  width: 50,
                  child: Text("切换网址"),
                )),
          ],
        ),
        body: Column(
          children: [
            Expanded(
              child: InAppWebView(
                initialUrlRequest: URLRequest(
                    url: WebUri(Uri.parse('https://www.baidu.com').toString())),
                onWebViewCreated: (controller) {
                  webViewController = controller;
                },
              ),
            )
          ],
        ));
  }
}

 

posted on 2024-03-20 11:14  鲤斌  阅读(158)  评论(0编辑  收藏  举报