我使用for循环来显示带有滚动条的文本。它开始向我抛出这个错误- The Scrollbar's ScrollController has no ScrollPosition attached.
当我没有使用lopp时,即当我作为row
的子级多次直接复制粘贴sizedbox
和listview
时,我没有得到这个错误。
这是密码-
List<String> list1 = [
"Academic Year",
"Lead Id",
"Lead created date",
"Student name",
"Grade",
"Date of birth",
"Gender",
"Email id",
"Address",
"Counsellor",
"BDA",
"Digital Counsellor"
];
List<String> list2 = [
"2023",
"156388",
"22-03-2019",
"Ankit",
"9th",
"21-04-2008",
"Male",
"ankit@gmail.com",
"None",
"2023",
"156388",
"22-03-2019"
];
.............
Container(
height: MediaQuery.of(context).size.height*0.5,
padding: EdgeInsets.symmetric(
vertical: getProportionateScreenWidth(32),
),
child: Scrollbar(
controller: controller,
isAlwaysShown: true,
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(24),
),
child: ListView(
children: [
for(int i=0;i<list1.length;i++)
Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
list1[i],
style: TextStyle(
color: primaryText,
fontSize: 16,
),
),
Text(
list2[i],
style: const TextStyle(
color: Colors.black87,
fontSize: 16,
),
)
],
),
SizedBox(
height: getProportionateScreenHeight(20),
),
],
)
],
),
),
),
),
发布于 2022-05-18 07:07:55
使用Listview.builder
为您构建项列表,而不是for循环。
这是密码
Container(
height: MediaQuery.of(context).size.height*0.5,
padding: EdgeInsets.symmetric(
vertical: getProportionateScreenWidth(32),
),
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(24),
),
child: ListView.builder(
itemCount: list1.length,
itemBuilder: (BuildContext context, int index) {
return
Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
list1[index],
style: TextStyle(
color: primaryText,
fontSize: 16,
),
),
Text(
list2[index],
style: const TextStyle(
color: Colors.black87,
fontSize: 16,
),
)
],
),
],
);
}
),
),
)
如果要以编程方式滚动controller
,可以将它分配给Listview.builder
。
https://stackoverflow.com/questions/72284378
复制相似问题