简体   繁体   English

控制 flutter 中 listview.builder 中文本字段的滚动

[英]controlling scroll of textfield in listview.builder in flutter

I have created TextFields inside a ListView.builder .我在ListView.builder中创建了TextFields As the user submits, the value gets added to the valueList .当用户提交时,该值被添加到valueList中。

List valueList = [];

ListView.builder(
                itemBuilder: (context, index){
                  return Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: TextField(
                      keyboardType: TextInputType.number,
                      onSubmitted: (value) {
                        valueList.add(value);
                        print(valueList);
                      },
                    ),
                  );
                }),

What I want it to do is, when the user hits the submit button I want the scroll to move to the next TextField .我想要它做的是,当用户点击提交按钮时,我希望滚动移动到下一个TextField There is FocusNode .FocusNode But I can't seep to find how to implement it in a ListView.builder但我无法找到如何在ListView.builder中实现它

You could use a controller on your ListView.builder and animate to a position, provided you know how far to scroll...您可以在ListView.builder上使用controller并动画到 position,前提是您知道滚动多远...

final ScrollController scrollcontroller = new Scrollcontroller();
final List valueList = [];

//...

ListView.builder(
  controller: scrollcontroller,

  itemBuilder: (context, index){
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: TextField(
        keyboardType: TextInputType.number,
        onSubmitted: (value) {
          valueList.add(value);

          double height_of_textfield = 20;
          // focus on next input then
          controller.animateTo(index * height_of_textfield, duration: /* some duration */, curve: Curves.easeOutCubic);
        },
      ),
    );
  }),
//...

This would work nice if you have consistent height across, other you would have to figure a way to calculate the scroll height to animate to...如果你有一致的高度,这会很好用,否则你必须想办法计算滚动高度以动画到......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM