gpt4 book ai didi

listview - 转换到另一条路线时,如何使 ListView 保留其滚动?

转载 作者:行者123 更新时间:2023-12-03 14:55:14 25 4
gpt4 key购买 nike

我想在我的 flutter 应用程序中完成向右滑动的过渡。
问题是路由转换有点创建我想从中转换的页面的新实例,所以 ListView滚动重置。
See a video
这就是我创建新路线的方式

/// @oldRoute needed cause this route transition utilizes `SlideStackRightRoute`
Route createSettingsRoute(Widget oldRoute) {
return SlideStackRightRoute(exitPage: oldRoute, enterPage: SettingsRoute());
}
最后幻灯片过渡类本身
import 'package:flutter/material.dart';

/// Creates cupertino-like route transition, where new route pushes old from right to left
class SlideStackRightRoute extends PageRouteBuilder {
final Widget enterPage;
final Widget exitPage;
static var exBegin = Offset(0.0, 0.0);
static var exEnd = Offset(-0.5, 0.0);
static var entBegin = Offset(1.0, 0.0);
static var entEnd = Offset.zero;
static var curveIn = Curves.easeOutSine;
static var curveOut = Curves.easeInSine;

SlideStackRightRoute({@required this.exitPage, @required this.enterPage})
: super(
transitionDuration: Duration(milliseconds: 400),
pageBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) =>
enterPage,
transitionsBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) =>
Stack(
children: <Widget>[
SlideTransition(
position: Tween(begin: exBegin, end: exEnd)
.chain(CurveTween(curve: curveIn))
.chain(CurveTween(curve: curveOut))
.animate(animation),
child: Container(
foregroundDecoration: BoxDecoration(
color: Colors.black.withOpacity(animation.value / 2),
),
child: exitPage),
),
SlideTransition(
position: Tween(begin: entBegin, end: entEnd)
.chain(CurveTween(curve: curveIn))
.chain(CurveTween(curve: curveOut))
.animate(animation),
child: enterPage,
)
],
),
);
}

最佳答案

我解决这个问题的方法是将偏移量保存到页面类中的静态变量中,并将 Controller 添加到 ListView 构建器中。例如

static double _previousScrollOffset = 0;
final ScrollController _controller = new ScrollController(initialScrollOffset: _previousScrollOffset);

然后每当我导航到新路线时,我都会保存状态。例如
onTap: () {
_previousScrollOffset = _controller.offset;
Navigator.pushNamed<bool>(
context, ROUTE_ADD_HOUSING + "/" + housing.uuid);
},

关于listview - 转换到另一条路线时,如何使 ListView 保留其滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58359812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com