Flutter:ListView构建器仅构建3个项目

ntjbwcob  于 2023-01-09  发布在  Flutter
关注(0)|答案(1)|浏览(173)

我有一个奇怪的问题,我的listviewbuilder只构建前3个项目。对于其他项目,它只构建列表,但没有数据。
这是我的数据

import 'package:pinvault/shared/models/banklistmodel.dart';

List bankList = [
  BankListModel(
      bank: 'ABC Bank',
      img: 'https://cdn-icons-png.flaticon.com/512/66/66455.png',
      updated: '29 Dec 2022'),
  BankListModel(
      bank: 'DEF  Bank',
      img: 'https://cdn-icons-png.flaticon.com/512/66/66455.png',
      updated: '22 Nov 2022'),
  BankListModel(
      bank: '123 Bank',
      img: 'https://cdn-icons-png.flaticon.com/512/66/66455.png',
      updated: '4 Dec 2022'),
  BankListModel(
      bank: 'XYZ Bank',
      img: 'https://cdn-icons-png.flaticon.com/512/66/66455.png',
      updated: '12 Oct 2022'),
];

这是我的模特

class BankListModel {
  String img;
  String bank;
  String updated;

  BankListModel({
    required this.img,
    required this.bank,
    required this.updated,
  });
}

这是我的列表视图生成器代码

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:pinvault/shared/data/dummydata.dart';

class BankList extends StatelessWidget {
  const BankList({super.key});

  @override
  Widget build(BuildContext context) {    
    return ListView.builder(
        itemCount: bankList.length,
        scrollDirection: Axis.vertical,
        shrinkWrap: false,
        itemBuilder: (context, index) {
          print(index);
          return ListTile(
            visualDensity: VisualDensity.adaptivePlatformDensity,
            tileColor: Colors.grey,
            dense: false,
            shape: const Border(bottom: BorderSide()),
            subtitle: Text('Last Updated On: ${bankList[index].updated}'),
            title: Text(
              bankList[index].bank,
              style: const TextStyle(fontWeight: FontWeight.bold),
            ),
            leading: ClipRRect(
              borderRadius: BorderRadius.circular(5),
              child: Image.network(bankList[index].img,
                  height: 80, width: 80, fit: BoxFit.contain),
            ),
          );
        });
  }
}

这是它在屏幕上的样子,你会注意到最后一项没有呈现任何文本或img。

mbskvtky

mbskvtky1#

请更新sizebox

@override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 100.h,
      child: ListView.builder(
          itemCount: bankList.length,
          shrinkWrap: true,
          itemBuilder: (context, index) {
            return ListTile(
              visualDensity: VisualDensity.adaptivePlatformDensity,
              tileColor: Colors.grey,
              dense: false,
              shape: const Border(bottom: BorderSide()),
              subtitle: Text('Last Updated On: ${bankList[index].updated}'),
              title: Text(
                bankList[index].bank,
                style: const TextStyle(fontWeight: FontWeight.bold),
              ),
              leading: ClipRRect(
                borderRadius: BorderRadius.circular(5),
                child: Image.network(bankList[index].img,
                    height: 80, width: 80, fit: BoxFit.contain),
              ),
            );
          }),
    );
  }

相关问题