简体   繁体   English

Flutter 的 ListView 需要可点击元素,ListTile 中不显示前导图片

[英]Flutter's ListView needs clickable elements, and leading images don't show in ListTile

I am writing a small application in Dart/Flutter with ListView (that shows data - text, and images - from the Internet).我正在使用 ListView 在 Dart/Flutter 中编写一个小应用程序(显示数据 - 文本和图像 - 来自互联网)。 I have two problems two solve in this code.我在这段代码中有两个问题要解决。

Here is my code (the part that shows data in ListView):这是我的代码(在 ListView 中显示数据的部分):

  ListView _testsListView(data) {
    return ListView.builder(
        itemCount: data.length,
        itemBuilder: (context, index) {
          return _tile(data[index].title, data[index].lead, data[index].href, data[index].imageHref);
        });
  }

  ListTile _tile(String title, String lead, String href, String imageHref) => ListTile(

    subtitle: Text(lead,
        style: TextStyle(
          fontWeight: FontWeight.w500,
          fontSize: 12,
          color: Colors.green,
        )),
    title: Text(title,
      style: TextStyle(color: Colors.blue),
    ),
    leading: CachedNetworkImage(
      imageUrl: imageHref,
      fit: BoxFit.cover,
      alignment: Alignment.centerLeft,
      placeholder: (context, url) => CircularProgressIndicator(),
      errorWidget: (context, url, error) => Icon(Icons.error),
    ),
  );
}

void launchWeb(String URL) async
{
    if (await canLaunch(URL)) {
      await launch(URL);
    } else {
      throw 'Could not launch $URL';
    }
}

The first problem is I need clickable element, one element of ListView:第一个问题是我需要可点击元素,ListView 的一个元素:

title ( title here),标题(此处为标题),

subtitile ( lead in my code),字幕(在我的代码中引导),

leading (image with imageHref ),领先(带有imageHref 的图像),

...needs to be clickable. ...需要可点击。 When the user clicks it should open a website (in a default web browser) where URL is href.当用户点击它应该打开一个网站(在默认的网络浏览器中),其中 URL 是 href。 I tried random codes from Internet and tried with flutter_linkify and url_launcher, but it does not work.我尝试了来自 Internet 的随机代码并尝试使用 flutter_linkify 和 url_launcher,但它不起作用。 On the other hand, when I use onTap: application automatically (without pressing anything) opens web page, which is of course not desired here.另一方面,当我使用 onTap 时:应用程序会自动(不按任何键)打开网页,这在这里当然是不需要的。

The second problem is that I need images in ListView (the leading property).第二个问题是我需要 ListView 中的图像(前导属性)。 Images I wanted to use from some website which address starts with htpps:// and I got "handshake" error:我想从某个地址以 htpps:// 开头的网站上使用的图像,我收到了“握手”错误:

(2) Exception caught by image resource service ════════════════════════════════════════════
Handshake error in client (OS Error: 
    CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:354))

I used the Image.network(), but it did not work (I have read that this method changes user agent), so I decided to use CachedNetworkImage, but still have problems to view those images.我使用了 Image.network(),但它不起作用(我已经读到此方法会更改用户代理),因此我决定使用 CachedNetworkImage,但仍然无法查看这些图像。 URLs of images are correct and when I use them from the Chrome browser there are no problems with viewing images.图像的 URL 是正确的,当我从 Chrome 浏览器使用它们时,查看图像没有问题。

   return ListTile(
            onTap:(){
            //list tile tap
              }
             ),
             title: InkWell(
                 child: Text('Title'),
                 onTap:(){
               //title tap
                 }
             ),
             subtitle:InkWell(
                 child: Text('Sub Title'),onTap:(){
               //sub title tap
                 }
             ),),
             leading:InkWell(
                 onTap:(){
                 //title tap
                  }
                 ),
                 child: CachedNetworkImage(
                      imageUrl: imageHref,
                      fit: BoxFit.cover,
                      alignment: Alignment.centerLeft,
                      placeholder: (context, url) => CircularProgressIndicator(),
                      errorWidget: (context, url, error) => Icon(Icons.error),
                    ),
                  )
                )

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

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