-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
Get Current Index #62
Comments
@knopp I think, if you put indexForOffset method to ListController, I can achieve this. I have 3 questions for you:
I'm trying to accomplish 2 features;
Does my solution ok, or could you offer better way to do this? (3) Thank you very much!
|
I don't quite understand what you're trying to accomplish here. What exactly is "current item"? I can't really expose indexForOffset and offsetForIndex, since they are largerly estimations and change over time as items are being laid out. |
@knopp Actually, I'm trying to make very similar to ListWheelScrollView, difference is variable itemExtent support. We can define current item;
In my case, in my real code, I'm adding padding to SuperListView as half of the viewport size. So "current item" is center of viewport. I'm calling listController.animateToItem with alignment: 0.5 , so I can navigate my "current item". My problem is when user scrolls, how can I know what is my "current item"? |
There is It seems that for something like this you will probably want to go down to render objects and check the layout of your list item render object against list view viewport, which will give you the most correct answer. |
@knopp I tried visibleRange but no luck, it's giving me only visible first and last item. For render object part, it is too complicated for me, I don't know event where to start. Also who want to snap item should put some logic which is available already in this package. What about this solution:
You said "indexForOffset" is not reliable and can change over time, but in this particular feature, it is reliable for us.
Also another approach, you can make public this method with more accurate naming and we can do this logic.
For this one extra method, this package can use like ListWheelScrollView with variable height. Thanks, |
Can you attach a video of this in action? The |
The video: RPReplay_Final1717971863.MP4Here is the code:
|
I think the solution here would be for |
Exactly! Could you add this feature? This way, we can use this package as snap list like ListWheelScrollView. More, because of reporting all visible item's index, offset and extent, maybe there would be some other use cases for different needs. |
@ibrahimdevs Hope this can help you. 2024-09-16.14.52.01.movimport 'package:flutter/material.dart';
import 'package:scrollview_observer/scrollview_observer.dart';
import 'package:super_sliver_list/super_sliver_list.dart';
void main() {
var randomList = List.generate(
1000,
(i) => "$i - ${"Lorem ipsum " * ((i % 10) + 1)}",
);
runApp(
MaterialApp(
home: MyApp(list: randomList),
), // use MaterialApp
);
}
class MyApp extends StatefulWidget {
const MyApp({super.key, required this.list});
final List<String> list;
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final ScrollController scrollController = ScrollController();
final ListController listController = ListController();
late ListObserverController observerController = ListObserverController(
controller: scrollController,
);
var focusedItemIndex = 0;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
listController.jumpToItem(
index: 505,
scrollController: scrollController,
alignment: 0.5,
);
await Future.delayed(const Duration(milliseconds: 100));
observerController.dispatchOnceObserve();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: LayoutBuilder(
builder: (BuildContext ctx, BoxConstraints constraint) {
double _topPadding = constraint.maxHeight * 0.5;
return Stack(
children: [
_buildListView(constraint, _topPadding),
Positioned(
top: _topPadding,
left: 0,
right: 0,
child: Container(height: 1, color: Colors.red),
),
],
);
},
),
);
}
Widget _buildListView(
BoxConstraints constraint,
double topPadding,
) {
Widget resultWidget = SuperListView.builder(
controller: scrollController,
listController: listController,
padding: EdgeInsets.only(
top: topPadding,
bottom: constraint.maxHeight - topPadding,
),
itemBuilder: (buildContext, index) {
return AnimatedContainer(
duration: const Duration(milliseconds: 250),
child: Container(
color:
index == focusedItemIndex ? Colors.amberAccent : Colors.white,
child: Text(
widget.list[index],
style: const TextStyle(fontSize: 24),
),
),
);
},
itemCount: widget.list.length,
physics: const ClampingScrollPhysics(),
);
resultWidget = ListViewObserver(
controller: observerController,
leadingOffset: topPadding,
child: resultWidget,
onObserve: (result) {
setState(() {
focusedItemIndex = result.firstChild?.index ?? 0;
});
print('firstIndex: ${result.firstChild?.index}');
},
customTargetRenderSliverType: (obj) {
return 'RenderSuperSliverList' == obj.runtimeType.toString();
},
);
return resultWidget;
}
} |
Firstly, thanks for great package! I'm already fan of SuperEditor, this package saved me again :)
I'm trying to create snapList, I think it will be very easy if I can get current Index. I'm listening ScrollEndNotification on NotificationListener, but I don't know which is the current item. Is there any workaround for this? If there would be a function gives us index of item based on alignment?
Thanks,
The text was updated successfully, but these errors were encountered: