From b2bf206f13fa5bd9194c96f43fd33e4063adb2a8 Mon Sep 17 00:00:00 2001 From: ndonkoHenri Date: Sat, 20 Jul 2024 12:38:47 +0200 Subject: [PATCH 1/2] fix scrolling issue in `CupertinoPicker` --- .../lib/src/controls/cupertino_picker.dart | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/packages/flet/lib/src/controls/cupertino_picker.dart b/packages/flet/lib/src/controls/cupertino_picker.dart index f723d0ba3..bdf19ad61 100644 --- a/packages/flet/lib/src/controls/cupertino_picker.dart +++ b/packages/flet/lib/src/controls/cupertino_picker.dart @@ -30,6 +30,44 @@ class CupertinoPickerControl extends StatefulWidget { } class _CupertinoPickerControlState extends State { + int _index = 0; + FixedExtentScrollController scrollController = FixedExtentScrollController(); + + @override + void initState() { + super.initState(); + scrollController = FixedExtentScrollController( + initialItem: widget.control.attrInt("selectedIndex", _index)!); + scrollController.addListener(_manageScroll); + } + + void _manageScroll() { + // https://stackoverflow.com/a/75283541 + // Fixes https://github.com/flet-dev/flet/issues/3649 + int previousIndex = 0; + bool isScrollUp = false; + bool isScrollDown = true; + if (previousIndex != scrollController.selectedItem) { + isScrollDown = previousIndex < scrollController.selectedItem; + isScrollUp = previousIndex > scrollController.selectedItem; + + var previousIndexTemp = previousIndex; + previousIndex = scrollController.selectedItem; + + if (isScrollUp) { + scrollController.jumpToItem(previousIndexTemp - 1); + } else if (isScrollDown) { + scrollController.jumpToItem(previousIndexTemp + 1); + } + } + } + + @override + void dispose() { + scrollController.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { debugPrint("CupertinoPicker build: ${widget.control.id}"); @@ -43,7 +81,7 @@ class _CupertinoPickerControlState extends State { }).toList(); double itemExtent = widget.control.attrDouble("itemExtent", _kItemExtent)!; - int? selectedIndex = widget.control.attrInt("selectedIndex"); + int selectedIndex = widget.control.attrInt("selectedIndex", 0)!; double diameterRatio = widget.control.attrDouble("diameterRatio", _kDefaultDiameterRatio)!; double magnification = widget.control.attrDouble("magnification", 1.0)!; @@ -54,6 +92,7 @@ class _CupertinoPickerControlState extends State { Color? backgroundColor = widget.control.attrColor("bgColor", context); Widget picker = CupertinoPicker( + scrollController: scrollController, backgroundColor: backgroundColor, diameterRatio: diameterRatio, magnification: magnification, @@ -63,14 +102,12 @@ class _CupertinoPickerControlState extends State { useMagnifier: useMagnifier, looping: looping, onSelectedItemChanged: (int index) { + _index = index; widget.backend.updateControlState( widget.control.id, {"selectedIndex": index.toString()}); widget.backend .triggerControlEvent(widget.control.id, "change", index.toString()); }, - scrollController: selectedIndex != null - ? FixedExtentScrollController(initialItem: selectedIndex) - : null, children: ctrls, ); From 351004c9eacd932d0dab86b0fd6b2f6b1122a864 Mon Sep 17 00:00:00 2001 From: ndonkoHenri Date: Sat, 20 Jul 2024 12:47:44 +0200 Subject: [PATCH 2/2] move vars out of _manageScroll --- packages/flet/lib/src/controls/cupertino_picker.dart | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/flet/lib/src/controls/cupertino_picker.dart b/packages/flet/lib/src/controls/cupertino_picker.dart index bdf19ad61..d5a26f80a 100644 --- a/packages/flet/lib/src/controls/cupertino_picker.dart +++ b/packages/flet/lib/src/controls/cupertino_picker.dart @@ -31,6 +31,9 @@ class CupertinoPickerControl extends StatefulWidget { class _CupertinoPickerControlState extends State { int _index = 0; + int previousIndex = 0; + bool isScrollUp = false; + bool isScrollDown = true; FixedExtentScrollController scrollController = FixedExtentScrollController(); @override @@ -44,9 +47,6 @@ class _CupertinoPickerControlState extends State { void _manageScroll() { // https://stackoverflow.com/a/75283541 // Fixes https://github.com/flet-dev/flet/issues/3649 - int previousIndex = 0; - bool isScrollUp = false; - bool isScrollDown = true; if (previousIndex != scrollController.selectedItem) { isScrollDown = previousIndex < scrollController.selectedItem; isScrollUp = previousIndex > scrollController.selectedItem; @@ -81,7 +81,6 @@ class _CupertinoPickerControlState extends State { }).toList(); double itemExtent = widget.control.attrDouble("itemExtent", _kItemExtent)!; - int selectedIndex = widget.control.attrInt("selectedIndex", 0)!; double diameterRatio = widget.control.attrDouble("diameterRatio", _kDefaultDiameterRatio)!; double magnification = widget.control.attrDouble("magnification", 1.0)!;