Listview滚动到小部件

Gre*_*Eye 16 dart flutter flutter-layout

如何滚动到列表视图中的特殊小部件?例如,如果我按下特定按钮,我想自动滚动到ListView中的某个Container.

ListView(children: <Widget>[
  Container(...),
  Container(...), #scroll for example to this container 
  Container(...)
]);
Run Code Online (Sandbox Code Playgroud)

TWL*_*TWL 49

不幸的是,ListView 没有针对 scrollToIndex() 函数的内置方法。您必须开发自己的方法来测量animateTo()or 的该元素的偏移量jumpTo(),或者您可以搜索这些建议的解决方案/插件或从其他帖子(如flutter ListView scroll to index not available)进行搜索

(自 2017 年以来,在flutter/issues/12319 上讨论了一般的 scrollToIndex 问题,但目前还没有计划)


但是有一种不同类型的 ListView 确实支持 scrollToIndex:

您可以像 ListView 一样设置它并且工作方式相同,但您现在可以访问ItemScrollController,它执行以下操作:

  • jumpTo({index, alignment})
  • scrollTo({index, alignment, duration, curve})

简化示例:

ItemScrollController _scrollController = ItemScrollController();

ScrollablePositionedList.builder(
  itemScrollController: _scrollController,
  itemCount: _myList.length,
  itemBuilder: (context, index) {
    return _myList[index];
  },
)

_scrollController.scrollTo(index: 150, duration: Duration(seconds: 1));
Run Code Online (Sandbox Code Playgroud)

(请注意,这个库是由 Google 开发的,而不是由 Flutter 核心团队开发的。)

  • 很好的答案。然而,`ScrollablePositionedList`没有shrinkWrap属性,不能与Slivers一起使用。 (4认同)
  • `ScrollablePositionedList` 本来可以节省我很多时间! (2认同)

Blo*_*oss 36

您可以使用 GlobalKey 访问构建器上下文。

GlobalObjectKey与 一起使用Scrollable

在 item 中定义 GlobalObjectKeyListView

ListView.builder(
itemCount: category.length,
itemBuilder: (_, int index) {
return Container(
    key: GlobalObjectKey(category[index].id),
Run Code Online (Sandbox Code Playgroud)

您可以从任何地方导航到项目

InkWell(
  onTap: () {
Scrollable.ensureVisible(GlobalObjectKey(category?.id).currentContext);
Run Code Online (Sandbox Code Playgroud)

您添加可滚动动画更改ensureVisible属性

Scrollable.ensureVisible(
  GlobalObjectKey(category?.id).currentContext,
  duration: Duration(seconds: 1),// duration for scrolling time
  alignment: .5, // 0 mean, scroll to the top, 0.5 mean, half
  curve: Curves.easeInOutCubic);
Run Code Online (Sandbox Code Playgroud)

  • 它非常适合小型列表,但对于大型列表,因为项目不是由 listView 构建器渲染的,所以除非渲染项目,否则它不会很好地工作。 (4认同)
  • 我认为这是最好的解决方案。 (3认同)
  • `ListView.builder` 仅在视口中可见时构建其子级,并且外部视口子级还没有上下文。所以 `Scrollable.ensureVisible` 不起作用。我从外部视口子项得到错误 context is null 。https://api.flutter.dev/flutter/widgets/ListView/ListView.builder.html (3认同)

Cop*_*oad 33

如果 ListView 中的小部件都具有相同的高度,则可以像这样实现它:

截屏:

在此处输入图片说明


对于ListView,你可以试试这个,下面的代码将动画到第 10 个索引。

class HomePage extends StatelessWidget {
  final _controller = ScrollController();
  final _height = 100.0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _animateToIndex(10),
        child: Icon(Icons.arrow_downward),
      ),
      body: ListView.builder(
        controller: _controller,
        itemCount: 100,
        itemBuilder: (_, i) => Container(
          height: _height,
          child: Card(child: Center(child: Text("Item $i"))),
        ),
      ),
    );
  }

  _animateToIndex(i) => _controller.animateTo(_height * i, duration: Duration(seconds: 2), curve: Curves.fastOutSlowIn);
}
Run Code Online (Sandbox Code Playgroud)

  • 我在一个列表上尝试了这个,该列表的项目高度不同,并且它没有滚动到精确的位置。您应该添加一个 not,它最适合具有相同高度(或宽度,如果是水平滚动)的项目的列表。我最终使用了 @TWL 的解决方案,它使用了项目的索引。 (2认同)

nat*_*nke 26

该解决方案改进了其他答案,因为它不需要对每个元素的高度进行硬编码。添加ScrollPosition.viewportDimensionScrollPosition.maxScrollExtent产生完整的内容高度。这可用于估计元素在某个索引处的位置。如果所有元素的高度相同,则估计是完美的。

// Get the full content height.
final contentSize = controller.position.viewportDimension + controller.position.maxScrollExtent;
// Index to scroll to.
final index = 100;
// Estimate the target scroll position.
final target = contentSize * index / itemCount;
// Scroll to that position.
controller.position.animateTo(
  target,
  duration: const Duration(seconds: 2),
  curve: Curves.easeInOut,
);
Run Code Online (Sandbox Code Playgroud)

以及一个完整的例子:

用户单击按钮滚动到长列表的第一百个元素

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Flutter Test",
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final controller = ScrollController();
    final itemCount = 1000;
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter Test"),
      ),
      body: Column(
        children: [
          ElevatedButton(
            child: Text("Scroll to 100th element"),
            onPressed: () {
              final contentSize = controller.position.viewportDimension + controller.position.maxScrollExtent;
              final index = 100;
              final target = contentSize * index / itemCount;
              controller.position.animateTo(
                target,
                duration: const Duration(seconds: 2),
                curve: Curves.easeInOut,
              );
            },
          ),
          Expanded(
            child: ListView.builder(
              controller: controller,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text("Item at index $index."),
                );
              },
              itemCount: itemCount,
            ),
          )
        ],
      ),
    );
  }
}

Run Code Online (Sandbox Code Playgroud)


Rém*_*let 17

到目前为止,最简单的解决方案是使用Scrollable.ensureVisible(context).因为它可以为您完成所有工作并使用任何小部件大小.使用获取上下文GlobalKey.

问题是ListView不会呈现不可见的项目.这意味着你的目标很可能不会建在所有.这可以防止你的目标没有context; 阻止您使用该方法而无需更多工作.

最后,最简单的解决方法是将取代你ListViewSingleChilScrollView和包装你的孩子进入Column.示例:

class ScrollView extends StatelessWidget {
  final dataKey = new GlobalKey();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      primary: true,
      appBar: new AppBar(
        title: const Text('Home'),
      ),
      body: new SingleChildScrollView(
        child: new Column(
          children: <Widget>[
            new SizedBox(height: 160.0, width: double.infinity, child: new Card()),
            new SizedBox(height: 160.0, width: double.infinity, child: new Card()),
            new SizedBox(height: 160.0, width: double.infinity, child: new Card()),
            // destination
            new Card(
              key: dataKey,
              child: new Text("data\n\n\n\n\n\ndata"),
            )
          ],
        ),
      ),
      bottomNavigationBar: new RaisedButton(
        onPressed: () => Scrollable.ensureVisible(dataKey.currentContext),
        child: new Text("Scroll to data"),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

注意:虽然这允许轻松滚动到所需的项目; 仅从小预定义列表中考虑此方法.至于更大的列表,你会遇到性能问题.

但它可以Scrollable.ensureVisible与之合作ListView; 虽然需要更多的工作.

  • 请制作一个完整的代码示例,说明使“Scrollable.ensureVisible”与“ListView”一起工作所需的“更多工作”! (5认同)
  • 谢谢,您是否有解决方案或进一步指导处理列表视图与未知高度的项目? (4认同)
  • 如果有人需要在构建后滚动,则可以使用`WidgetsBinding.instance.addPostFrameCallback((_) =&gt; Scrollable.ensureVisible(dataKey.currentContext))`。 (3认同)
  • 如前所述,该解决方案对于作为列实现的短列表效果很好。如何修改它以与包含 SliverLists 的 CustomScrollView 一起使用? (2认同)
  • `ListView` 和 `SingleChildScrollView` 是完全不同的野兽。如果用例适合“SingleChildScrollView”,那么这个问题一开始就不会存在。 (2认同)

Nhậ*_*rần 15

对于人们试图跳转到CustomScrollView 中的小部件。首先,将此插件添加到您的项目中。

然后看看我下面的示例代码:

class Example extends StatefulWidget {
  @override
  _ExampleState createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
  AutoScrollController _autoScrollController;
  final scrollDirection = Axis.vertical;

  bool isExpaned = true;
  bool get _isAppBarExpanded {
    return _autoScrollController.hasClients &&
        _autoScrollController.offset > (160 - kToolbarHeight);
  }

  @override
  void initState() {
    _autoScrollController = AutoScrollController(
      viewportBoundaryGetter: () =>
          Rect.fromLTRB(0, 0, 0, MediaQuery.of(context).padding.bottom),
      axis: scrollDirection,
    )..addListener(
        () => _isAppBarExpanded
            ? isExpaned != false
                ? setState(
                    () {
                      isExpaned = false;
                      print('setState is called');
                    },
                  )
                : {}
            : isExpaned != true
                ? setState(() {
                    print('setState is called');
                    isExpaned = true;
                  })
                : {},
      );
    super.initState();
  }

  Future _scrollToIndex(int index) async {
    await _autoScrollController.scrollToIndex(index,
        preferPosition: AutoScrollPosition.begin);
    _autoScrollController.highlight(index);
  }

  Widget _wrapScrollTag({int index, Widget child}) {
    return AutoScrollTag(
      key: ValueKey(index),
      controller: _autoScrollController,
      index: index,
      child: child,
      highlightColor: Colors.black.withOpacity(0.1),
    );
  }

  _buildSliverAppbar() {
    return SliverAppBar(
      brightness: Brightness.light,
      pinned: true,
      expandedHeight: 200.0,
      backgroundColor: Colors.white,
      flexibleSpace: FlexibleSpaceBar(
        collapseMode: CollapseMode.parallax,
        background: BackgroundSliverAppBar(),
      ),
      bottom: PreferredSize(
        preferredSize: Size.fromHeight(40),
        child: AnimatedOpacity(
          duration: Duration(milliseconds: 500),
          opacity: isExpaned ? 0.0 : 1,
          child: DefaultTabController(
            length: 3,
            child: TabBar(
              onTap: (index) async {
                _scrollToIndex(index);
              },
              tabs: List.generate(
                3,
                (i) {
                  return Tab(
                    text: 'Detail Business',
                  );
                },
              ),
            ),
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        controller: _autoScrollController,
        slivers: <Widget>[
          _buildSliverAppbar(),
          SliverList(
              delegate: SliverChildListDelegate([
            _wrapScrollTag(
                index: 0,
                child: Container(
                  height: 300,
                  color: Colors.red,
                )),
            _wrapScrollTag(
                index: 1,
                child: Container(
                  height: 300,
                  color: Colors.red,
                )),
            _wrapScrollTag(
                index: 2,
                child: Container(
                  height: 300,
                  color: Colors.red,
                )),
          ])),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

是的,这只是一个例子,用你的大脑让它成为现实 在此处输入图片说明


Mic*_*ski 7

StatefulWidget如果您想在构建视图树后立即使小部件可见,这里是解决方案。

通过扩展雷米的答案,您可以使用以下代码实现它:

class ScrollView extends StatefulWidget {
  // widget init
}

class _ScrollViewState extends State<ScrollView> {

  final dataKey = new GlobalKey();

  // + init state called

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      primary: true,
      appBar: AppBar(
        title: const Text('Home'),
      ),
      body: _renderBody(),
    );
  }

  Widget _renderBody() {
    var widget = SingleChildScrollView(
        child: Column(
          children: <Widget>[
           SizedBox(height: 1160.0, width: double.infinity, child: new Card()),
           SizedBox(height: 420.0, width: double.infinity, child: new Card()),
           SizedBox(height: 760.0, width: double.infinity, child: new Card()),
           // destination
           Card(
              key: dataKey,
              child: Text("data\n\n\n\n\n\ndata"),
            )
          ],
        ),
      );
    setState(() {
        WidgetsBinding.instance!.addPostFrameCallback(
              (_) => Scrollable.ensureVisible(dataKey.currentContext!));
    });
    return widget;
  }
}

Run Code Online (Sandbox Code Playgroud)


ana*_*jin 6

我使用找到了一个完美的ListView解决方案。
我忘记了解决方案来自哪里,所以我发布了我的代码。该信用属于其他信用。

21/09/22:编辑。我在这里发布了一个完整的示例,希望它更清楚。

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class CScrollToPositionPage extends StatefulWidget {

CScrollToPositionPage();

@override
State<StatefulWidget> createState() => CScrollToPositionPageState();
}

class CScrollToPositionPageState extends State<CScrollToPositionPage> {
static double TEXT_ITEM_HEIGHT = 80;
final _formKey = GlobalKey<FormState>();
late List _controls;
List<FocusNode> _lstFocusNodes = [];

final __item_count = 30;

@override
void initState() {
    super.initState();

    _controls = [];
    for (int i = 0; i < __item_count; ++i) {
        _controls.add(TextEditingController(text: 'hello $i'));

        FocusNode fn = FocusNode();
        _lstFocusNodes.add(fn);
        fn.addListener(() {
            if (fn.hasFocus) {
                _ensureVisible(i, fn);
            }
        });
    }
}

@override
void dispose() {
    super.dispose();

    for (int i = 0; i < __item_count; ++i) {
        (_controls[i] as TextEditingController).dispose();
    }
}

@override
Widget build(BuildContext context) {
    List<Widget> widgets = [];
    for (int i = 0; i < __item_count; ++i) {
        widgets.add(TextFormField(focusNode: _lstFocusNodes[i],controller: _controls[i],));
    }

    return Scaffold( body: Container( margin: const EdgeInsets.all(8),
        height: TEXT_ITEM_HEIGHT * __item_count,
        child: Form(key: _formKey, child: ListView( children: widgets)))
    );
}

Future<void> _keyboardToggled() async {
    if (mounted){
        EdgeInsets edgeInsets = MediaQuery.of(context).viewInsets;
        while (mounted && MediaQuery.of(context).viewInsets == edgeInsets) {
            await Future.delayed(const Duration(milliseconds: 10));
        }
    }

    return;
}
Future<void> _ensureVisible(int index,FocusNode focusNode) async {
    if (!focusNode.hasFocus){
        debugPrint("ensureVisible. has not the focus. return");
        return;
    }

    debugPrint("ensureVisible. $index");
    // Wait for the keyboard to come into view
    await Future.any([Future.delayed(const Duration(milliseconds: 300)), _keyboardToggled()]);


    var renderObj = focusNode.context!.findRenderObject();
    if( renderObj == null ) {
      return;
    }
    var vp = RenderAbstractViewport.of(renderObj);
    if (vp == null) {
        debugPrint("ensureVisible. skip. not working in Scrollable");
        return;
    }
    // Get the Scrollable state (in order to retrieve its offset)
    ScrollableState scrollableState = Scrollable.of(focusNode.context!)!;

    // Get its offset
    ScrollPosition position = scrollableState.position;
    double alignment;

    if (position.pixels > vp.getOffsetToReveal(renderObj, 0.0).offset) {
        // Move down to the top of the viewport
        alignment = 0.0;
    } else if (position.pixels < vp.getOffsetToReveal(renderObj, 1.0).offset){
        // Move up to the bottom of the viewport
        alignment = 1.0;
    } else {
        // No scrolling is necessary to reveal the child
        debugPrint("ensureVisible. no scrolling is necessary");
        return;
    }

    position.ensureVisible(
        renderObj,
        alignment: alignment,
        duration: const Duration(milliseconds: 300),
    );

}

}
Run Code Online (Sandbox Code Playgroud)


Hem*_*Raj 5

您可以只ScrollController为您的列表视图指定 a并animateTo在单击按钮时调用该方法。

演示animateTo用法的最小示例:

class Example extends StatefulWidget {
  @override
  _ExampleState createState() => new _ExampleState();
}

class _ExampleState extends State<Example> {
  ScrollController _controller = new ScrollController();

  void _goToElement(int index){
    _controller.animateTo((100.0 * index), // 100 is the height of container and index of 6th element is 5
        duration: const Duration(milliseconds: 300),
        curve: Curves.easeOut);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(),
      body: new Column(
        children: <Widget>[
          new Expanded(
            child: new ListView(
              controller: _controller,
              children: Colors.primaries.map((Color c) {
                return new Container(
                  alignment: Alignment.center,
                  height: 100.0,
                  color: c,
                  child: new Text((Colors.primaries.indexOf(c)+1).toString()),
                );
              }).toList(),
            ),
          ),
          new FlatButton(
            // on press animate to 6 th element
            onPressed: () => _goToElement(6),
            child: new Text("Scroll to 6th element"),
          ),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 太复杂。特别是对于未知大小的元素。 (24认同)
  • 有人找到了对不同高度的物品执行此操作的方法吗?这是颤振的总展示:( (8认同)

jit*_*555 5

输出:

使用依赖:

dependencies:
    scroll_to_index: ^1.0.6
Run Code Online (Sandbox Code Playgroud)

代码:(滚动将始终执行第六个索引小部件,因为它在下面添加为硬编码,请尝试使用滚动到特定小部件所需的滚动索引)

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final scrollDirection = Axis.vertical;

  AutoScrollController controller;
  List<List<int>> randomList;

  @override
  void initState() {
    super.initState();
    controller = AutoScrollController(
        viewportBoundaryGetter: () =>
            Rect.fromLTRB(0, 0, 0, MediaQuery.of(context).padding.bottom),
        axis: scrollDirection);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView(
        scrollDirection: scrollDirection,
        controller: controller,
        children: <Widget>[
          ...List.generate(20, (index) {
            return AutoScrollTag(
              key: ValueKey(index),
              controller: controller,
              index: index,
              child: Container(
                height: 100,
                color: Colors.red,
                margin: EdgeInsets.all(10),
                child: Center(child: Text('index: $index')),
              ),
              highlightColor: Colors.black.withOpacity(0.1),
            );
          }),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _scrollToIndex,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
  // Scroll listview to the sixth item of list, scrollling is dependent on this number
  Future _scrollToIndex() async {
    await controller.scrollToIndex(6, preferPosition: AutoScrollPosition.begin);
  }
}
Run Code Online (Sandbox Code Playgroud)