Flutter 颤振栅格视图滚动到底部

Flutter 颤振栅格视图滚动到底部,flutter,gridview,Flutter,Gridview,我有一个带有GridView的屏幕。当我移动到此页面时,我有GridView项目计数,我想打开此页面并从底部开始显示GridView。现在我知道了 WidgetsBinding.instance.addPostFrameCallback((_) { if (_parameters.scrollToBottom && _controller.hasClients) { _controller.jumpTo(_controller.position.

我有一个带有
GridView
的屏幕。当我移动到此页面时,我有
GridView
项目计数,我想打开此页面并从底部开始显示
GridView
。现在我知道了

   WidgetsBinding.instance.addPostFrameCallback((_) {
      if (_parameters.scrollToBottom && _controller.hasClients) {
        _controller.jumpTo(_controller.position.maxScrollExtent);
      }
    });

但我认为这不是一个好办法。我能用另一种方法做这件事吗?

这是另一种方法。请试试这个

import 'dart:async';

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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 ScrollController gridScrollController = ScrollController();
  List<String> images = [
    "https://images.unsplash.com/photo-1614961234425-dedd96e5e699?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=500&q=60",
    "https://images.unsplash.com/photo-1614961234425-dedd96e5e699?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=500&q=60",
    "https://images.unsplash.com/photo-1614961234425-dedd96e5e699?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=500&q=60",
    "https://images.unsplash.com/photo-1614961234425-dedd96e5e699?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=500&q=60",
    "https://images.unsplash.com/photo-1614961234425-dedd96e5e699?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=500&q=60",
    "https://images.unsplash.com/photo-1614961234425-dedd96e5e699?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=500&q=60",
    "https://images.unsplash.com/photo-1614961234425-dedd96e5e699?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=500&q=60",
    "https://images.unsplash.com/photo-1614961234425-dedd96e5e699?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=500&q=60",
    "https://images.unsplash.com/photo-1614961234425-dedd96e5e699?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=500&q=60",
    "https://images.unsplash.com/photo-1614961234425-dedd96e5e699?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=500&q=60",
    "https://images.unsplash.com/photo-1614961234425-dedd96e5e699?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=500&q=60",
    "https://images.unsplash.com/photo-1614961234425-dedd96e5e699?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8M3x8fGVufDB8fHw%3D&auto=format&fit=crop&w=500&q=60",
  ];

  @override
  void initState() {
    super.initState();
    /*Or you can also use Timer*/
    // Timer(
    //   Duration(seconds: 1),
    //   () => gridScrollController
    //       .jumpTo(gridScrollController.position.maxScrollExtent),
    // );
    
    Future.delayed(const Duration(milliseconds: 500), () {

      gridScrollController
          .jumpTo(gridScrollController.position.maxScrollExtent);

    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(
        padding: EdgeInsets.symmetric(
          horizontal: 12.0,
        ),
        child: Expanded(
          child: GridView.builder(
            controller: gridScrollController,
            itemCount: images.length,
            shrinkWrap: true,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                crossAxisSpacing: 8.0,
                mainAxisSpacing: 10.0),
            itemBuilder: (BuildContext context, int index) {
              return Image.network(images[index]);
            },
          ),
        ),
      ),
    );
  }
}

您需要颠倒
GridView
从下到上的子视图布局方式。幸运的是,
GridView
具有反向属性

下面是一个关于如何使用它的示例:

GridView.count(
  reverse: true, // Set reverse to true
  crossAxisCount: 2,
  children: List.generate(100, (index) {
    return Center(
      child: Text(
        'Item $index',
      ),
    );
  }),
);

您是否介意解释一下为什么它不是一个好的解决方案,有任何错误或效率问题?我认为这个解决方案不是一个好的解决方案,我想知道这样做的最佳实践谢谢您的回答,但我认为这是一个坏的解决方案,我只想打开并查看
gridView
底部的项目。我不希望在
gridView
的顶部呈现项目,它不会呈现底部项目,而是会滚动到页面底部。您也可以尝试此操作,请参考上面更新的代码
if(gridScrollController.hasClients){setState((){gridScrollController.jumpTo(gridScrollController.position.maxScrollExtent);});}
GridView.count(
  reverse: true, // Set reverse to true
  crossAxisCount: 2,
  children: List.generate(100, (index) {
    return Center(
      child: Text(
        'Item $index',
      ),
    );
  }),
);