封装flutter webview页面

例如在flutter里面跳转百度页面

需要安装webview_flutter

webview_page.dart

Dart 复制代码
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';



class MyWebView extends StatefulWidget {


  const MyWebView({super.key, required this.webViewUrl,  this.webViewTitle =''});
 final String webViewUrl;
 final String webViewTitle;
  @override
  // ignore: library_private_types_in_public_api
  _MyWebViewState createState() => _MyWebViewState();
}

class _MyWebViewState extends State<MyWebView> {



  @override
  Widget build(BuildContext context) {
     WebViewController controller = WebViewController()
  ..setJavaScriptMode(JavaScriptMode.unrestricted)
  ..setBackgroundColor(const Color(0x00000000))
  ..setNavigationDelegate(
    NavigationDelegate(
      onProgress: (int progress) {
        // Update loading bar.
      },
      onPageStarted: (String url) {},
      onPageFinished: (String url) {},
      onWebResourceError: (WebResourceError error) {},
    ),
  )
  ..loadRequest(Uri.parse(widget.webViewUrl));
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
          appBar: AppBar(
            leading: GestureDetector(child: Icon(Icons.arrow_back),onTap: (){Navigator.pop(context);},),
            title:  Text(widget.webViewTitle),
            centerTitle: true,
            backgroundColor: const Color(0xFF206CFF),
          ),
          body: WebViewWidget(controller: controller)
          ),
    );
  }
}

使用

Dart 复制代码
 Navigator.push(
    context,
    MaterialPageRoute(
      builder: (context) => MyWebView(webViewUrl: 'https://www.baidu.com/', webViewTitle: '百度一下你就知道'),
    ),
  );
相关推荐
神经蛙39715 分钟前
settings.gradle' line: 22 * What went wrong: Plugin [id: 'org.jetbrains.kotlin.a
flutter
stringwu1 小时前
一个bug 引发的Dart 与 Java WeakReference 对比探讨
flutter
火柴就是我21 小时前
从头写一个自己的app
android·前端·flutter
●VON1 天前
Flutter 项目成功运行后,如何正确迁移到 OpenHarmony?常见疑问与跳转失效问题解析
flutter·华为·openharmony·开源鸿蒙
●VON1 天前
Flutter 编译开发 OpenHarmony 全流程实战教程(基于 GitCode 社区项目)
flutter·openharmony·gitcode
消失的旧时光-19432 天前
Flutter 组件:Row / Column
flutter
程序员老刘2 天前
Flutter版本选择指南:3.35稳定,3.38发布 | 2025年11月
flutter·客户端
kirk_wang2 天前
Flutter 3.38和Dart 3.10中最大的更新
flutter
前端小伙计2 天前
Flutter 配置国内镜像,加速项目加载!
flutter
zonda的地盘2 天前
开发 Flutter Plugin 之 初始配置
flutter