Flutter 滚动控制器如何工作,它们如何执行

发布于 2025-01-12 16:36:19 字数 499 浏览 0 评论 0原文

Flutter :我是 flutter 的新手,我想知道它们在幕后如何工作(当我们将滚动控制器附加到可滚动小部件时实际会发生什么。)。我阅读了文档,但我很难理解。

                  controller: ** _controller **,
                  scrollDirection: Axis.vertical,
                  child: Wrap(
                    // direction: Axis.horizontal,
                    crossAxisAlignment: WrapCrossAlignment.center,
                    spacing: 4.0,
                    runSpacing: 4.0,
                    children: *MY WIDGET*,
                  ),```

Flutter : i am new to flutter , i wanted to know how do they work behind the scenes(what actually happens when we attach a scroll controller to a scrollable widget.) . i read the docs but its hard for me to understand.

                  controller: ** _controller **,
                  scrollDirection: Axis.vertical,
                  child: Wrap(
                    // direction: Axis.horizontal,
                    crossAxisAlignment: WrapCrossAlignment.center,
                    spacing: 4.0,
                    runSpacing: 4.0,
                    children: *MY WIDGET*,
                  ),```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光是把杀猪刀 2025-01-19 16:36:19

这是一个很好的问题,也是我很好奇的问题。如果您使用 ListView,它会子类化另一个名为 BoxScrollView 的类,而该类本身又是 ScrollView 的子类,实际上是 ScrollView如果您查看以下代码,它会保留 ScrollController 实例:

  /// {@template flutter.widgets.scroll_view.controller}
  /// An object that can be used to control the position to which this scroll
  /// view is scrolled.
  ///
  /// Must be null if [primary] is true.
  ///
  /// A [ScrollController] serves several purposes. It can be used to control
  /// the initial scroll position (see [ScrollController.initialScrollOffset]).
  /// It can be used to control whether the scroll view should automatically
  /// save and restore its scroll position in the [PageStorage] (see
  /// [ScrollController.keepScrollOffset]). It can be used to read the current
  /// scroll position (see [ScrollController.offset]), or change it (see
  /// [ScrollController.animateTo]).
  /// {@endtemplate}
  final ScrollController? controller;

如果您查看 ScrollViewbuild() 函数,您会看到这颗宝石:

  @override
  Widget build(BuildContext context) {
    final List<Widget> slivers = buildSlivers(context);
    final AxisDirection axisDirection = getDirection(context);

    final ScrollController? scrollController =
        primary ? PrimaryScrollController.of(context) : controller;
    final Scrollable scrollable = Scrollable(
      dragStartBehavior: dragStartBehavior,
      axisDirection: axisDirection,
      controller: scrollController,

你看到了将 controller 作为参数的 Scrollable 实例?

我不会粘贴更多代码,因为它可能会很长,但我建议您继续查看 ScrollablleScrollView 的源代码以获取更好地了解它们的工作原理。

This is a good question and something I was curious about as well. If you're using ListView, it subclasses another class called BoxScrollView which itself subclasses ScrollView and it's in fact ScrollView that holds onto the ScrollController instance if you look at this code:

  /// {@template flutter.widgets.scroll_view.controller}
  /// An object that can be used to control the position to which this scroll
  /// view is scrolled.
  ///
  /// Must be null if [primary] is true.
  ///
  /// A [ScrollController] serves several purposes. It can be used to control
  /// the initial scroll position (see [ScrollController.initialScrollOffset]).
  /// It can be used to control whether the scroll view should automatically
  /// save and restore its scroll position in the [PageStorage] (see
  /// [ScrollController.keepScrollOffset]). It can be used to read the current
  /// scroll position (see [ScrollController.offset]), or change it (see
  /// [ScrollController.animateTo]).
  /// {@endtemplate}
  final ScrollController? controller;

If you then look in the build() function of ScrollView you'll see this gem:

  @override
  Widget build(BuildContext context) {
    final List<Widget> slivers = buildSlivers(context);
    final AxisDirection axisDirection = getDirection(context);

    final ScrollController? scrollController =
        primary ? PrimaryScrollController.of(context) : controller;
    final Scrollable scrollable = Scrollable(
      dragStartBehavior: dragStartBehavior,
      axisDirection: axisDirection,
      controller: scrollController,

You see the Scrollable instance which takes the controller in as a parameter?

I'm not going to paste more code since it can get quite long but I suggest that you go ahead and look at the source code for Scrollablle and also ScrollView to get a better understanding of how they work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文