Flutter Webview - 如何在页面上添加更多链接或元素?

Posted

技术标签:

【中文标题】Flutter Webview - 如何在页面上添加更多链接或元素?【英文标题】:Flutter Webview - How to add more link or element on pages? 【发布时间】:2020-10-13 15:55:36 【问题描述】:

我对 Flutter 很陌生,我正在为 covid-19 制作一个配套应用程序。 :>:>

布局有很多按钮,我想利用 Webview 功能为不同的场景加载不同的网页。

当前的 main.dart 是这样的:

import 'package:flutter/material.dart';
import 'package:flutter_webview_project/webviewFlutter.dart';


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

class MyApp extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      theme: ThemeData(primarySwatch: Colors.deepPurple),
      home: MyHomePage(),
    );
  


class MyHomePage extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return Scaffold(
      
      appBar: AppBar(
        centerTitle: true,
        title: Text("Covid-19"),
      ),
      body: Center(
        child: RaisedButton(
          color: Colors.deepPurple,
          child: Text("Daily Threats",
          style: TextStyle(color: Colors.white),
          ),
          onPressed: () 
            Navigator.of(context).push(MaterialPageRoute(
                builder: (BuildContext context) => WebViewFlutter(
                      websiteName: "CV19 Map",
                      websiteUrl: "https://www.esp.com/",
                    )));
          ,
        ),
      ),
    );
  

我的 webviewFlutter.dart 是这样的:

import 'dart:async';

import 'package:flutter/material.dart';
//Webview in flutter
import 'package:webview_flutter/webview_flutter.dart';

class WebViewFlutter extends StatefulWidget 
  final String websiteName;
  final String websiteUrl;

  const WebViewFlutter(Key key, this.websiteName, this.websiteUrl)
      : super(key: key);

  @override
  _WebViewFlutterState createState() => _WebViewFlutterState();


class _WebViewFlutterState extends State<WebViewFlutter> 
  final Completer<WebViewController> _controller =
      Completer<WebViewController>();
  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.websiteName),
      ),
      body: WebView(
        initialUrl: widget.websiteUrl,
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController webViewController) 
          _controller.complete(webViewController);
        ,
      ),
    );
  

我在那个场景中只有 1 个链接页面,但现在我有 20 多个网址需要在我的移动页面上链接。所以我需要打开对应于不同页面的那个。抱歉这个愚蠢的问题,但我如何在该代码中添加更多类似的链接?

【问题讨论】:

【参考方案1】:

您可以在下面复制粘贴运行完整代码 你可以使用ListView.builder

ListView.builder(
          padding: const EdgeInsets.all(8),
          itemCount: siteList.length,
          itemBuilder: (BuildContext context, int index) 
            return Center(
              child: RaisedButton(
                color: Colors.deepPurple,
                child: Text(
                  "Daily Threat $siteList[index].name",
                  style: TextStyle(color: Colors.white),
                ),
                onPressed: () 
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (BuildContext context) => WebViewFlutter(
                            websiteName: siteList[index].name,
                            websiteUrl: siteList[index].url,
                          )));
                ,
              ),
            );
          ),

工作演示

完整代码

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:webview_flutter/webview_flutter.dart';

class Site 
  String name;
  String url;

  Site(this.name, this.url);


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

class MyApp extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      theme: ThemeData(primarySwatch: Colors.deepPurple),
      home: MyHomePage(),
    );
  


class MyHomePage extends StatelessWidget 
  List<Site> siteList = [
    Site(name: "CV19 Map", url: "https://www.esp.com/"),
    Site(name: "google", url: "https://www.google.com/"),
    Site(name: "flutter", url: "https://flutter.dev/")
  ];

  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("Covid-19"),
      ),
      body: ListView.builder(
          padding: const EdgeInsets.all(8),
          itemCount: siteList.length,
          itemBuilder: (BuildContext context, int index) 
            return Center(
              child: RaisedButton(
                color: Colors.deepPurple,
                child: Text(
                  "Daily Threat $siteList[index].name",
                  style: TextStyle(color: Colors.white),
                ),
                onPressed: () 
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (BuildContext context) => WebViewFlutter(
                            websiteName: siteList[index].name,
                            websiteUrl: siteList[index].url,
                          )));
                ,
              ),
            );
          ),
    );
  


class WebViewFlutter extends StatefulWidget 
  final String websiteName;
  final String websiteUrl;

  const WebViewFlutter(Key key, this.websiteName, this.websiteUrl)
      : super(key: key);

  @override
  _WebViewFlutterState createState() => _WebViewFlutterState();


class _WebViewFlutterState extends State<WebViewFlutter> 
  final Completer<WebViewController> _controller =
      Completer<WebViewController>();
  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.websiteName),
      ),
      body: WebView(
        initialUrl: widget.websiteUrl,
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController webViewController) 
          _controller.complete(webViewController);
        ,
      ),
    );
  

【讨论】:

以上是关于Flutter Webview - 如何在页面上添加更多链接或元素?的主要内容,如果未能解决你的问题,请参考以下文章

Flutter Webview - 如何在页面上添加更多链接或元素?

12-11锦上添花Flutter AI只能语音搜索功能实现

为啥 Flutter WebView 不加载页面?

如何使用 flutter_webview 插件在 Flutter 中启用位置?

如果在 WebView Flutter 中导航失败,则重新加载页面

如何在 Flutter 的 webview 中隐藏页眉和页脚。?