flutter - 如何获得 ScrollController 的全尺寸

我已将 ScrollController 附加到 [SliverAppBar, SliverList] 的 CustomScrollView

在默认情况下,我会使用 reverse:true 和 animateTo (0.0) 将滚动移动到添加的最后一个元素,但在这种情况下,使用 reverse 也会反转 SliverAppBar/SliverList 的顺序!

所以我想使用 animateTo ( sizeOfScrollableAfterElementAdded ) 但我找不到这个值。

谢谢!

最佳答案

您可以使用 _scrollController.position.maxScrollExtent 滚动到最后。确保在帧后回调中执行此操作,以便它包含您刚刚添加的新项目。

这是一个随着更多项目的添加而滚动到末尾的条形列表示例。

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

void main() {
  runApp(new MyApp());
}

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

class MyHomePage extends StatefulWidget {
  State createState() => new MyHomePageState();
}


class MyHomePageState extends State<MyHomePage> {
  ScrollController _scrollController = new ScrollController();

  List<Widget> _items = new List.generate(40, (index) {
    return new Text("item $index");
  });

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.arrow_downward),
        onPressed: () {
          setState(() {
            _items.add(new Text("item ${_items.length}"));
          });
          SchedulerBinding.instance.addPostFrameCallback((_) {
            _scrollController.animateTo(
              _scrollController.position.maxScrollExtent,
              duration: const Duration(milliseconds: 300),
              curve: Curves.easeOut,
            );
          });
        },
      ),
      body: new CustomScrollView(
        controller: _scrollController,
        slivers: [
          new SliverAppBar(
            title: new Text('Sliver App Bar'),
          ),
          new SliverList(
            delegate: new SliverChildBuilderDelegate(
              (context, index) => _items[index],
              childCount: _items.length,
            ),
          ),
        ],
      ),
    );
  }
}

https://stackoverflow.com/questions/44141148/

相关文章:

dart - 使 BoxDecoration 图像褪色/透明

android - 如何在 Flutter 中添加 Webview?

ios - Flutter:如何创建一个新项目

dart - TabController 构造函数中的 `vsync` 属性

dart - 如何从另一个小部件设置状态?

ios - 无法导入使用 Cocoapods 安装的依赖项

flutter - InkWell 在 Flutter 中的 GridTile 中的图像顶部产生波纹

flutter - 如何在 Flutter 中对齐单个小部件?

android - flutter : How to change Android minSdkVe

flutter - 如何获取 Flutter 应用的构建和版本号