gpt4 book ai didi

recursion - 多个可滚动小部件的滚动同步

转载 作者:IT王子 更新时间:2023-10-29 06:57:23 24 4
gpt4 key购买 nike

多个可滚动小部件的滚动同步:

如果滚动第一个列表,我想滚动第二个列表;如果滚动第二个列表,我想滚动第一个列表。这将是递归的,任何人都可以帮助解决这个问题,在此先感谢。

import 'package:flutter/cupertino.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
ScrollController firstScroll = ScrollController();
ScrollController secondScrollController = ScrollController();

@override
void initState() {
super.initState();
firstScroll.addListener(() {
//THIS IS called when scroll is triggered,
secondScrollController
.jumpTo(firstScroll.offset); // THIS will sync the scroll;
});

secondScrollController.addListener(() {
//THIS IS called when scroll is triggered,
firstScroll
.jumpTo(secondScrollController.offset); // THIS will sync the scroll;
});
}

@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
SingleChildScrollView(
// this is the first scroll
scrollDirection: Axis.horizontal,
controller: firstScroll, // THIS IS THE FIRST SCROLL CONTROLLER
child: Container(
//TODO: add your content here here
),
),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
controller: secondScrollController,
// HERE YOU SET THE SECOND CONTROLLER
child: Container(
//TODO: add your content here
),
)
],
),
);
}
}

最佳答案

那是因为每次调用 jumpTo 方法时,它都会调用第一个,而第一个又会调用第二个,这样就会出现无限循环。

解决方案是您创建自己的 ScrollController,它拥有无需通知即可跳转到另一个位置的方法。

这是您可以创建的自定义滚动 Controller :

            class CustomScrollController extends ScrollController {
CustomScrollController({
double initialScrollOffset = 0.0,
keepScrollOffset = true,
debugLabel,
}) : super(
initialScrollOffset: initialScrollOffset,
keepScrollOffset: keepScrollOffset,
debugLabel: debugLabel);

@override
_UnboundedScrollPosition createScrollPosition(
ScrollPhysics physics,
ScrollContext context,
ScrollPosition oldPosition,
) {
return _UnboundedScrollPosition(
physics: physics,
context: context,
oldPosition: oldPosition,
initialPixels: initialScrollOffset,
);
}

void jumpToWithoutGoingIdleAndKeepingBallistic(double value) {
assert(positions.isNotEmpty, 'ScrollController not attached.');
for (_UnboundedScrollPosition position
in new List<ScrollPosition>.from(positions))
position.jumpToWithoutGoingIdleAndKeepingBallistic(value);
}
}

class _UnboundedScrollPosition extends ScrollPositionWithSingleContext {
_UnboundedScrollPosition({
ScrollPhysics physics,
ScrollContext context,
ScrollPosition oldPosition,
double initialPixels,
}) : super(
physics: physics,
context: context,
oldPosition: oldPosition,
initialPixels: initialPixels,
);

/// There is a feedback-loop between aboveController and belowController. When one of them is
/// being used, it controls the other. However if they get out of sync, for timing reasons,
/// the controlled one with try to control the other, and the jump will stop the real controller.
/// For this reason, we can't let one stop the other (idle and ballistics) in this situation.
void jumpToWithoutGoingIdleAndKeepingBallistic(double value) {
if (pixels != value) {
forcePixels(value);
}
}
}

只需调用 jumpToWithoutGoingIdleAndKeepingBallistic 而不是 jumpTo

这里有一个工作示例:

https://gist.github.com/diegoveloper/75e55ca2e4cee03bff41a26254d6fcf6

结果

enter image description here

关于recursion - 多个可滚动小部件的滚动同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55710296/

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