简体   繁体   English

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

[英]Flutter Webview - How to add more link or element on pages?

I am pretty new to flutter and i'm making a companion app for the covid-19.我对 flutter 很陌生,我正在为 covid-19 制作一个配套应用程序。 :>:> :>:>

The layout has heaps of buttons and I want to utilize the Webview function to load different web pages for different scenes.布局有很多按钮,我想利用 Webview function 为不同的场景加载不同的 web 页面。

Current main.dart is like:当前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/",
                    )));
          },
        ),
      ),
    );
  }
}

My webviewFlutter.dart is like:我的 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);
        },
      ),
    );
  }
}

I have only 1 linked page on that scene but now i have 20+ web adresses needs to be get linked on my mobile pages.我在那个场景中只有 1 个链接页面,但现在我有 20 多个 web 地址需要在我的移动页面上链接。 So i need to open which corresponds to different pages.所以我需要打开对应于不同页面的那个。 Sorry that silly question but how do i add more link like that in that code?抱歉这个愚蠢的问题,但我如何在该代码中添加更多这样的链接?

You can copy paste run full code below您可以在下面复制粘贴运行完整代码
You can use ListView.builder您可以使用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,
                          )));
                },
              ),
            );
          }),

working demo工作演示

在此处输入图像描述

full code完整代码

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);
        },
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Flutter 中添加 Webview? - How to add a Webview in Flutter? 如何在 Flutter WebView 或 InAppWebView 中添加 JavaScriptInterface? - How to add JavaScriptInterface in Flutter WebView or InAppWebView? 如何将WebView添加到具有更多小部件的布局中 - How add a webview to a layout that have more widgets 如何在 flutter 中打开呼叫或邮件(电话:和邮寄地址:)的链接。 使用 flutter_webview_plugin - How to open a link to call or Mail (tel: and mailto:) in flutter. Using flutter_webview_plugin 如何在android中将onclick添加到cardview以在webview中打开链接 - How to add onclick to cardview in android to open link in webview Flutter:webview 内的链接打开到内置浏览器而不是 webview - Flutter: Link inside the webview opens to built in browser instead of the webview Flutter webview 拦截并为所有请求添加标头 - Flutter webview intercept and add headers to all requests Android WebView 添加更多 URL 参数值 - Android WebView add more URL parameter values Flutter - 如何在 WebView 中下载文件? - Flutter - How to download files in WebView? Webview“mailto:”链接和“tel:”链接工作使用Intent.ACTION_VIEW,但如何添加唯一的主题,即“mailto:”链接 - Webview “mailto:” link & “tel:” link work using Intent.ACTION_VIEW, but how do I add unique Subject ie for “mailto:” link
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM