Flutter:控制2个不同控制器的滚动

问题描述 投票:0回答:1

所以,我有两个不同的滚动小部件,即 TextField 和 ListView (ListView.builder),附加有两个不同的滚动控制器,即。 _textEditingController 和 _scrollController。

这里的基本概念是使用 Stack 和可滚动小部件创建一个类似规则线的结构并在其顶部创建一个文本字段。如何通过另一个控制器的运动/滚动操作来移动一个控制器?

Stack(
  children: [
    SizedBox(
      height:
          ((lines * 15) < MediaQuery.of(context).size.height)
              ? MediaQuery.of(context).size.height
              : (lines * 15),
      child: ListView.builder(
        controller: _scrollController,
        itemBuilder: (context, index) => const Padding(
          padding: EdgeInsets.only(top: 5, bottom: 5),
          child: Divider(
            color: Colors.black,
            height: 30,
            thickness: 1.5,
          ),
        ),
      ),
    ),
    Container(
      padding: const EdgeInsets.symmetric(horizontal: 10)
          .copyWith(top: 5),
      child: TextField(
        style: const TextStyle(
          fontFamily: "WorkSans",
          fontSize: 26,
          fontStyle: FontStyle.italic,
          height: 1.55,
        ),
        decoration: const InputDecoration(
          border: InputBorder.none,
        ),
        cursorColor: Colors.black,
        controller: _textEditingController,
        maxLines: lines,
        onChanged: (value) {
          /* This function adds another line if I am at the end
          of the text field, thus increasing the ListView's height */
          final selection = _textEditingController.selection;
          if (isAtLastLine(selection)) {
            setState(() {
              lines++;
            });
          }
        },
      ),
    ),
  ],
),
flutter controller
1个回答
0
投票

您可以在ListView.builder和shrinkWrap中使用物理来解决高度问题

ListView.builder(
     shrinkWrap: true,
     physics: const NeverScrollableScrollPhysics(),
     itemCount: recommendedJobsList.length,
     padding: const EdgeInsets.all(0),
     itemBuilder: (BuildContext context, int index) {
     return your code here;
});
© www.soinside.com 2019 - 2024. All rights reserved.