【问题标题】:How to automatically scroll to end of grid view?如何自动滚动到网格视图的末尾?
【发布时间】:2018-04-02 13:11:39
【问题描述】:

当我将项目添加到网格视图的末尾时,我希望用户看到已添加的内容。这是我的意思的一个例子:

用户通过按 + 图标添加项目。问题是在第 14 项之后没有任何反馈表明已添加任何项。添加到列表时如何自动滚动到最后一项?

(奖励积分:当第 n 个项目添加到列表中间的某个位置时,我如何滚动到它)

Here is an answer for scrolling to the end of a list view,但是如果我颠倒列表的顺序,那么有时顶行有“缺失”项目会看起来很奇怪。

这是我的代码:

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',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  @override
  Widget build(BuildContext context) {
    List items = [];
    for (int i = 0; i < _counter; i++) {
      items.add(new Text("Item $i"));
    }
    return new Scaffold(
      appBar: new AppBar(title: new Text("Scroll To End Test")),
      body: new GridView.extent(
        primary: true,
        maxCrossAxisExtent: 150.0,
        children: items,
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: () {
          setState(() {
            _counter++;
          });
        },
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    声明一个滚动控制器并使用它移动到您想要的位置。这是滚动到最后一个元素的示例。请注意,滚动控制器是显式声明的,因此您不能发出 primary: true

    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',
          theme: new ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: new MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => new _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      ScrollController _scrollController;                                 // NEW
    
      @override                                                           // NEW
      void initState() {                                                  // NEW
        super.initState();                                                // NEW
        _scrollController = new ScrollController(                         // NEW
          initialScrollOffset: 0.0,                                       // NEW
          keepScrollOffset: true,                                         // NEW
        );
      }
    
      void _toEnd() {                                                     // NEW
        _scrollController.animateTo(                                      // NEW
          _scrollController.position.maxScrollExtent,                     // NEW
          duration: const Duration(milliseconds: 500),                    // NEW
          curve: Curves.ease,                                             // NEW
        );                                                                // NEW
      }                                                                   // NEW
    
      @override
      Widget build(BuildContext context) {
        List items = [];
        for (int i = 0; i < _counter; i++) {
          items.add(new Text("Item $i"));
        }
        return new Scaffold(
          appBar: new AppBar(
            title: new Text("Scroll To End Test"),
            actions: <Widget>[                                            // NEW
              new IconButton(                                             // NEW
                  icon: new Icon(Icons.arrow_downward), onPressed: _toEnd)// NEW
            ],                                                            // NEW
          ),
          body: new GridView.extent(
            //primary: true, // REMOVED
            maxCrossAxisExtent: 150.0,
            children: items,
            controller: _scrollController,                                // NEW
          ),
          floatingActionButton: new FloatingActionButton(
            onPressed: () {
              setState(() {
                _counter++;
              });
            },
            tooltip: 'Increment',
            child: new Icon(Icons.add),
          ),
        );
      }
    }
    

    关于“奖励点”我对 ScrollControllers 不是很熟练,但据我所知,.animateTo( ) 方法要求作为第一个输入为双精度,设置滚动点:原则上,将 i -您要滚动到的第

    var cursor = i/(# items) * _scrollController.position.maxScrollExtent
    

    根据您的网格每行有多少对象来调整此值。我知道这只是一个草图,但我认为它可以引导你到达那里。

    【讨论】:

    • 谢谢!我不知道 _scrollController.position.maxScrollExtent
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多