【问题标题】:How can I have a ListView with 200 different images?我怎样才能拥有一个包含 200 个不同图像的 ListView?
【发布时间】:2020-01-18 03:53:19
【问题描述】:

我正在尝试创建一个包含许多不同图像的 ListView,但是当我滚动并加载图像时,我的 iPhone 6 Plus 内存不足,然后应用程序就停止了。这是一个显示问题的示例(为简洁起见,我使用 URL 参数来确保 Flutter 将每个 URL 视为不同的图像)

import 'package:flutter/material.dart';

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

List<String> imgUrls = List.generate(
    200,
    (i) => 'https://images.unsplash.com/photo-1448227922836-6d05b3f8b663?ixlib'
        '=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60&foo=$i');

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Lots of Images',
      home: Scaffold(
        body: Scrollbar(
          child: ListView.builder(
            itemCount: imgUrls.length,
              itemBuilder: (BuildContext context, int index) => Image.network(
                imgUrls[index],
                  )),
        ),
      ),
    );
  }
}

有什么方法可以告诉 ListView 通过处理已滚动到屏幕外的图像来释放内存?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    你可以直接作为

    ListView.builder(
       itemCount: imgUrls.length,
       itemBuilder: (context,index){
          return Container(
            child: Image(
              image: NetworkImage(imgUrls[index])
            )
         );
       }
    )
    

    【讨论】:

    • 我担心同样的问题:/
    【解决方案2】:

    尝试使用无限滚动列表视图小部件 或第 3 方小部件 - https://pub.dev/packages/infinite_listview

    【讨论】:

    • 感谢指向该包的指针,但即使我将 ListView.builder 替换为 InfiniteListView.builder
    • 只需从 ListView.builder 中删除 itemCount 参数,它应该可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 2020-10-04
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    相关资源
    最近更新 更多