Flutter ListView 在错误的索引或元素处自动更新
Flutter ListView getting automatically updated at wrong index or element
我正在尝试创建一个列表视图来显示某个数字的加法。在图像中,我正在尝试为 ListView 中的 16 个步骤生成加法 table。它在加载时看起来很好,但在滚动顶部和底部元素后会自动更新。这个问题只有在基本控制ListView中item个数的变量tableSize的值超过15时才会出现,请问如何解决这个问题??
// table大小 = 16 && tCount = 10
这是代码
return ListView.builder(
key: UniqueKey(),
padding: EdgeInsets.symmetric(horizontal: 5),
itemCount: DataFile.tableSize,
scrollDirection: Axis.vertical,
itemBuilder: (context, index) {
tCount = tCount + 1;
String title;
print(" Tcount = ${tCount}");
if (sign == DataFile.divisionSign) {
double ans = tableNo / tCount;
title = tableNo.toString() +
space +
sign +
space +
tCount.toString() +
space +
equal +
space +
f.format(ans);
} else {
int ans;
if (sign == DataFile.additionSign) {
ans = tableNo + tCount;
} else if (sign == DataFile.subtractionSign) {
ans = tableNo - tCount;
} else {
ans = tableNo * tCount;
}
title = tableNo.toString() +
space +
space +
sign +
space +
space +
tCount.toString() +
space +
space +
equal +
space +
space +
int.parse(ans.toString()).toString();
print(title);
}
return InkWell(
child: Container(
height: cellHeight,
margin: EdgeInsets.symmetric(
vertical: ConstantData.getScreenPercentSize(context, 2.3)),
child: Center(
child: ConstantWidget.getTextWidget(
title,
ConstantData.textColors!,
ConstantData.getPercentSize(cellHeight, 98),
FontWeight.w500,
TextAlign.center),
),
),
onTap: () {},
);
},
);
需要更改 tCount,它是状态变量中的可变数据。
使用此代码:
return ListView.builder(
key: UniqueKey(),
padding: EdgeInsets.symmetric(horizontal: 5),
itemCount: DataFile.tableSize,
scrollDirection: Axis.vertical,
itemBuilder: (context, index) {
tCount = index + 1; //change here
String title;
print(" Tcount = ${tCount}");
if (sign == DataFile.divisionSign) {
double ans = tableNo / tCount;
title = tableNo.toString() +
space +
sign +
space +
tCount.toString() +
space +
equal +
space +
f.format(ans);
} else {
int ans;
if (sign == DataFile.additionSign) {
ans = tableNo + tCount;
} else if (sign == DataFile.subtractionSign) {
ans = tableNo - tCount;
} else {
ans = tableNo * tCount;
}
title = tableNo.toString() +
space +
space +
sign +
space +
space +
tCount.toString() +
space +
space +
equal +
space +
space +
int.parse(ans.toString()).toString();
print(title);
}
return InkWell(
child: Container(
height: cellHeight,
margin: EdgeInsets.symmetric(
vertical: ConstantData.getScreenPercentSize(context, 2.3)),
child: Center(
child: ConstantWidget.getTextWidget(
title,
ConstantData.textColors!,
ConstantData.getPercentSize(cellHeight, 98),
FontWeight.w500,
TextAlign.center),
),
),
onTap: () {},
);
},
);
我正在尝试创建一个列表视图来显示某个数字的加法。在图像中,我正在尝试为 ListView 中的 16 个步骤生成加法 table。它在加载时看起来很好,但在滚动顶部和底部元素后会自动更新。这个问题只有在基本控制ListView中item个数的变量tableSize的值超过15时才会出现,请问如何解决这个问题??
// table大小 = 16 && tCount = 10
这是代码
return ListView.builder(
key: UniqueKey(),
padding: EdgeInsets.symmetric(horizontal: 5),
itemCount: DataFile.tableSize,
scrollDirection: Axis.vertical,
itemBuilder: (context, index) {
tCount = tCount + 1;
String title;
print(" Tcount = ${tCount}");
if (sign == DataFile.divisionSign) {
double ans = tableNo / tCount;
title = tableNo.toString() +
space +
sign +
space +
tCount.toString() +
space +
equal +
space +
f.format(ans);
} else {
int ans;
if (sign == DataFile.additionSign) {
ans = tableNo + tCount;
} else if (sign == DataFile.subtractionSign) {
ans = tableNo - tCount;
} else {
ans = tableNo * tCount;
}
title = tableNo.toString() +
space +
space +
sign +
space +
space +
tCount.toString() +
space +
space +
equal +
space +
space +
int.parse(ans.toString()).toString();
print(title);
}
return InkWell(
child: Container(
height: cellHeight,
margin: EdgeInsets.symmetric(
vertical: ConstantData.getScreenPercentSize(context, 2.3)),
child: Center(
child: ConstantWidget.getTextWidget(
title,
ConstantData.textColors!,
ConstantData.getPercentSize(cellHeight, 98),
FontWeight.w500,
TextAlign.center),
),
),
onTap: () {},
);
},
);
需要更改 tCount,它是状态变量中的可变数据。
使用此代码:
return ListView.builder(
key: UniqueKey(),
padding: EdgeInsets.symmetric(horizontal: 5),
itemCount: DataFile.tableSize,
scrollDirection: Axis.vertical,
itemBuilder: (context, index) {
tCount = index + 1; //change here
String title;
print(" Tcount = ${tCount}");
if (sign == DataFile.divisionSign) {
double ans = tableNo / tCount;
title = tableNo.toString() +
space +
sign +
space +
tCount.toString() +
space +
equal +
space +
f.format(ans);
} else {
int ans;
if (sign == DataFile.additionSign) {
ans = tableNo + tCount;
} else if (sign == DataFile.subtractionSign) {
ans = tableNo - tCount;
} else {
ans = tableNo * tCount;
}
title = tableNo.toString() +
space +
space +
sign +
space +
space +
tCount.toString() +
space +
space +
equal +
space +
space +
int.parse(ans.toString()).toString();
print(title);
}
return InkWell(
child: Container(
height: cellHeight,
margin: EdgeInsets.symmetric(
vertical: ConstantData.getScreenPercentSize(context, 2.3)),
child: Center(
child: ConstantWidget.getTextWidget(
title,
ConstantData.textColors!,
ConstantData.getPercentSize(cellHeight, 98),
FontWeight.w500,
TextAlign.center),
),
),
onTap: () {},
);
},
);