Skip to content

Instantly share code, notes, and snippets.

@shakir915
Created May 20, 2025 10:35
Show Gist options
  • Save shakir915/7ed8ff4cecf086bd008305187ae826f2 to your computer and use it in GitHub Desktop.
Save shakir915/7ed8ff4cecf086bd008305187ae826f2 to your computer and use it in GitHub Desktop.
Horizontal srollview make it item fully visible on click
import 'package:flutter/cupertino.dart';
import 'package:jezly/components/screenUtil/size_extension.dart';
import 'package:jezly/components/tablet/phoneModeInTablet.dart';
import 'package:jezly/helper/primitiveHelpers.dart';
Future<void> scrollToTarget(GlobalKey<State<StatefulWidget>> targetKey, ScrollController scrollController, {double? extra}) async {
await waitForBuildIfPending();
final context = targetKey.currentContext;
if (context != null) {
try {
final isRTL = Directionality.of(context) == TextDirection.rtl;
RenderBox? targetRenderObject = context.findRenderObject() as RenderBox;
final globalOffset = targetRenderObject.localToGlobal(Offset.zero);
final itemRect = Rect.fromLTWH(globalOffset.dx, globalOffset.dy, targetRenderObject.size.width, targetRenderObject.size.height);
final RenderBox? scrollBox = scrollController.position.context.storageContext.findRenderObject() as RenderBox?;
if (scrollBox == null) return;
final scrollBoxLeft = scrollBox.localToGlobal(Offset.zero).dx;
final scrollBoxRight = scrollBox.localToGlobal(Offset(scrollBox.size.width, scrollBox.size.height)).dx;
double scrollTo = -1.0;
if (!isRTL) {
if (itemRect.left < scrollBoxLeft) {
scrollTo = scrollController.offset + (itemRect.left - scrollBoxLeft) - (extra ?? 30.w);
if (scrollTo < 0) scrollTo = 0;
} else if (itemRect.right > scrollBoxRight) {
scrollTo = scrollController.offset + itemRect.right - scrollBox.size.width + (extra ?? 30.w);
scrollTo = scrollTo.clamp(0.0, scrollController.position.maxScrollExtent);
}
} else {
final maxExtent = scrollController.position.maxScrollExtent;
if (itemRect.right > scrollBoxRight) {
scrollTo = scrollController.offset - (itemRect.right - scrollBoxRight) - (extra ?? 30.w);
if (scrollTo < 0) scrollTo = 0;
} else if (itemRect.left < scrollBoxLeft) {
scrollTo = scrollController.offset - (itemRect.left - scrollBoxLeft) + (extra ?? 30.w);
if (scrollTo > maxExtent) scrollTo = maxExtent;
}
}
if (scrollTo >= 0) {
scrollController.animateTo(
scrollTo,
duration: const Duration(milliseconds: 500),
curve: Curves.ease,
);
}
} catch (e, s) {
print(s);
}
}
}
@shakir915
Copy link
Author

Widget TabBarItems(index, name, id) {
return InkWellNoSplash(
key: businessCateringId == id ? targetKey : null,
onTap: () async {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment