如何通过onTap触发Grid布局中的每个网格的URL?每个网格应打开一个不同的URL

时间:2019-04-26 18:24:38

标签: url flutter flutter-layout

所以我在这里有两个列表:
image:包含显示在网格图块上的图像。
directLinks:包含应由“ onTap”打开的每个网格图块的链接

我不想为每个网格the grid looks like this分配launch_url。该怎么办? 每个网格图块都应将我转移到WebView中显示的新网站。 没有为每个网格图块分别键入url的情况下,我没有找到任何合适的方法。抱歉,我是初学者。

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class ExampleGrid extends StatelessWidget {
  List<String> images = [
    "https://uae.microless.com/cdn/no_image.jpg",
    "https://images-na.ssl-images-amazon.com/images/I/81aF3Ob-2KL._UX679_.jpg",
    "https://www.boostmobile.com/content/dam/boostmobile/en/products/phones/apple/iphone-7/silver/device-front.png.transform/pdpCarousel/image.jpg",
    "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgUgs8_kmuhScsx-J01d8fA1mhlCR5-1jyvMYxqCB8h3LCqcgl9Q",
    "https://ae01.alicdn.com/kf/HTB11tA5aiAKL1JjSZFoq6ygCFXaw/Unlocked-Samsung-GALAXY-S2-I9100-Mobile-Phone-Android-Wi-Fi-GPS-8-0MP-camera-Core-4.jpg_640x640.jpg",
    "https://media.ed.edmunds-media.com/gmc/sierra-3500hd/2018/td/2018_gmc_sierra-3500hd_f34_td_411183_1600.jpg",
    "https://hips.hearstapps.com/amv-prod-cad-assets.s3.amazonaws.com/images/16q1/665019/2016-chevrolet-silverado-2500hd-high-country-diesel-test-review-car-and-driver-photo-665520-s-original.jpg",
    "https://www.galeanasvandykedodge.net/assets/stock/ColorMatched_01/White/640/cc_2018DOV170002_01_640/cc_2018DOV170002_01_640_PSC.jpg",
    "https://media.onthemarket.com/properties/6191869/797156548/composite.jpg",
    "https://media.onthemarket.com/properties/6191840/797152761/composite.jpg",
  ];

  List<String> directLinks = [
    "https://google.com",
    "https://amazon.com/",
    "https://www.boostmobile.com/",
    "https://gmail.com",
    "https://samsung.com",
    "https://eminem.com",
    "https://facebook.com",
    "https://www.galeanasvandykedodge.net",
    "https://flipkart.com",
    "https://apple.com",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GridView(
        physics:
            BouncingScrollPhysics(), 
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2),
        children: images.map((url) {
          return ListTile(
              onTap: _launchURL, title: Card(child: Image.network(url)));
        }).toList(),
      ),
    );
  }
}

void _launchURL(int index) async {
  const url = "";
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

0 个答案:

没有答案