简体   繁体   中英

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. :>:>

The layout has heaps of buttons and I want to utilize the Webview function to load different web pages for different scenes.

Current main.dart is like:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM