Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scrolling issues in CupertinoPicker #3678

Merged
merged 2 commits into from
Jul 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions packages/flet/lib/src/controls/cupertino_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,44 @@ class CupertinoPickerControl extends StatefulWidget {
}

class _CupertinoPickerControlState extends State<CupertinoPickerControl> {
int _index = 0;
int previousIndex = 0;
bool isScrollUp = false;
bool isScrollDown = true;
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
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}");
Expand All @@ -43,7 +81,6 @@ class _CupertinoPickerControlState extends State<CupertinoPickerControl> {
}).toList();

double itemExtent = widget.control.attrDouble("itemExtent", _kItemExtent)!;
int? selectedIndex = widget.control.attrInt("selectedIndex");
double diameterRatio =
widget.control.attrDouble("diameterRatio", _kDefaultDiameterRatio)!;
double magnification = widget.control.attrDouble("magnification", 1.0)!;
Expand All @@ -54,6 +91,7 @@ class _CupertinoPickerControlState extends State<CupertinoPickerControl> {
Color? backgroundColor = widget.control.attrColor("bgColor", context);

Widget picker = CupertinoPicker(
scrollController: scrollController,
backgroundColor: backgroundColor,
diameterRatio: diameterRatio,
magnification: magnification,
Expand All @@ -63,14 +101,12 @@ class _CupertinoPickerControlState extends State<CupertinoPickerControl> {
useMagnifier: useMagnifier,
looping: looping,
onSelectedItemChanged: (int index) {
_index = index;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider using setState when updating _index.

To ensure the UI updates correctly, consider wrapping the assignment of _index in a setState call.

Suggested change
_index = index;
setState(() {
_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,
);

Expand Down