首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >滚动条的ScrollController在使用for循环时没有附加ScrollPosition

滚动条的ScrollController在使用for循环时没有附加ScrollPosition
EN

Stack Overflow用户
提问于 2022-05-18 06:47:44
回答 1查看 168关注 0票数 0

我使用for循环来显示带有滚动条的文本。它开始向我抛出这个错误- The Scrollbar's ScrollController has no ScrollPosition attached.

当我没有使用lopp时,即当我作为row的子级多次直接复制粘贴sizedboxlistview时,我没有得到这个错误。

这是密码-

代码语言:javascript
运行
复制
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),
                                ),
                              ],
                            )
                        ],
                      ),
                    ),
                  ),
                ),
EN

回答 1

Stack Overflow用户

发布于 2022-05-18 07:07:55

使用Listview.builder为您构建项列表,而不是for循环。

这是密码

代码语言:javascript
运行
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72284378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档