添加依赖
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()), )); } } } }
标签:web,widget,width,webViewController,weburl,height,嵌套,WebView,flutter From: https://www.cnblogs.com/xbinbin/p/18083715