can somebody help to solve the problem? There are two scenarios:
- When Select the category from horizontal list, it should select relevant category and shows highlight on that category, also should scroll the relevant data from vertical list.
- When scrolling the vertical list, it should highlight the correct categories from horizontal list on scroll. Also horizontal list should scroll with vertical list.
I want to add both these features in a page, they are working correctly independently, but when I add both features, They are not working fine
//code to select a category onTap: () {
if (selectedCategoryIndex !=
index) {
setState(() {
selectedCategoryIndex = index;
});
// Only scroll horizontally if it's necessary
if (index > 2) {
// If the selected index is greater than 2, scroll the horizontal list forward
upperController.animateTo(
index * 50.0,
duration: Duration(
milliseconds: 500),
curve: Curves.ease);
} else if (index <
categories.length - 2) {
// If the selected index is less than 2, scroll the horizontal list backward
upperController.animateTo(0,
duration: Duration(
milliseconds: 500),
curve: Curves.ease);
}
// Scroll to the selected index in the vertical list
lowerController.scrollToIndex(index,
preferPosition:
AutoScrollPosition.begin);
// Scroll to the selected index in the menu items list
menuItemsController.scrollToIndex(
index,
preferPosition:
AutoScrollPosition.begin);
print("selectedCategoryIndex");
print(selectedCategoryIndex);
print("index");
print(index);
}
},
//code called in initState, lowerController.addListener(_onVerticalScroll);
void _onVerticalScroll() {
double upperListMaxScroll = upperController.position.maxScrollExtent;
double lowerListMaxScroll = lowerController.position.maxScrollExtent;
double lowerListOffset = lowerController.position.pixels;
// Calculate the scroll percentage
double scrollPercentage = lowerListOffset / lowerListMaxScroll;
double upperListNewOffset = scrollPercentage * upperListMaxScroll;
upperController.jumpTo(upperListNewOffset);
// Calculate the index based on the scroll percentage
int newIndex = (scrollPercentage * categories.length).round(); // Assuming 6 categories
// Ensure that the index doesn't exceed the range of available categories
newIndex = newIndex.clamp(0, categories.length-1);
// Assuming 6 categories
// Adjust newIndex based on dynamic conditions
// int range = 1; // Define the range from start and end
// if (newIndex == range) {
// newIndex = newIndex + 1; // If newIndex is within 'range' from start, add 1
// }
// else if (newIndex >= categories.length - range - 1) {
// newIndex = newIndex - 1; // If newIndex is within 'range' from end, subtract 1
// }
setState(() {
selectedCategoryIndex = newIndex;
});
}