Scroll list depending on another list scrolling Flutter - flutter

How I can make the scroll of a list depending on another list scrolling for example :
class ConectLists extends StatefulWidget {
const ConectLists({Key? key}) : super(key: key);
#override
State<ConectLists> createState() => _ConectListsState();
}
class _ConectListsState extends State<ConectLists> {
ScrollController scrollConroller1 = ScrollController();
ScrollController scrollConroller2 = ScrollController();
#override
void initState() {
// TODO: implement initState
super.initState();
}
#override
void dispose() {
// TODO: implement dispose
scrollConroller1.dispose();
scrollConroller2.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
SizedBox(
height: 8,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('List 1'),
Text('List 2'),
],
),
SizedBox(
height: 8,
),
Container(
color: Colors.black.withOpacity(0.5),
width: double.infinity,
height: 4,
),
Expanded(
child: Row(
children: [
Expanded(
flex: 1,
child: ListView.builder(
controller: scrollConroller1,
itemBuilder: (context, index) => Card(
elevation: 3,
child: SizedBox(
height: 40,
child:
Center(child: Text('First list item $index')))),
itemCount: 50,
),
),
Container(
color: Colors.black.withOpacity(0.5),
width: 4,
height: double.infinity,
),
Expanded(
child: ListView.builder(
controller: scrollConroller2,
itemBuilder: (context, index) => Card(
elevation: 3,
child: SizedBox(
height: 40,
child: Center(
child: Text('Second list item $index')))),
itemCount: 25,
),
),
],
),
),
],
),
);
}
}
I need to make list 2 scroll when List1 scroll with controlling the speed of the list2 scrolling (different scroll speed) for example or reverse the direction for example..
Is there a lite way to do this in Fultter ?

You can easily achieve this by adding listeners to your ScrollController like so :
controller: scrollConroller1..addListener(() {
scrollConroller2.animateTo(scrollConroller1.offset,
curve: Curves.easeInOut,
duration: const Duration(milliseconds: 450));
}),
Basically you listen on scroll changes and you assign ones' Offset to the other list. However, when the first list's length is larger than the second list,the second list will keep on bouncing at the bottom (on iOS devices). You could fix that by checking if the first list's offset is larger than the second list's maxScrollExtent :
controller: scrollConroller1..addListener(() {
if (scrollConroller1.offset <= scrollConroller2.position.maxScrollExtent){
scrollConroller2.animateTo(scrollConroller1.offset,
curve: Curves.easeInOut,
duration: const Duration(milliseconds: 450));
}
}),

You could add a listener in your init state to make scrollConroller2 jump to the postion scrollConroller1 is at as below.
Credit to esentis for the fix when first list's offset is larger
than the second list's maxScrollExtent :
#override
void initState() {
super.initState();
//Your other code in init state
scrollConroller1.addListener(() {
if (scrollConroller1.offset <=
scrollConroller2.position.maxScrollExtent) {
setState(() {
double value2 = scrollConroller1.offset;
scrollConroller2.jumpTo(value2);
});
}
});
}
To scroll in reverse, you can set the listener instead to:
scrollConroller1.addListener(() {
if (scrollConroller1.offset <=
scrollConroller2.position.maxScrollExtent) {
setState(() {
double value2 = scrollConroller2.position.maxScrollExtent -
scrollConroller1.offset;
scrollConroller2.jumpTo(value2);
});
}
});

To control list scroll while maintaining the scroll offset that will be based on height ratio, Therefore the jump offset will be
jumpPoss = (math.min(l1maxHeight, l2maxHeight) * scrollOffset) /
math.max(l1maxHeight, l2maxHeight);
late ScrollController scrollController1 = ScrollController()
..addListener(() {
double scrollOffset = scrollController1.offset;
final double l1maxHeight = scrollController1.position.maxScrollExtent;
final double l2maxHeight = scrollController2.position.maxScrollExtent;
double jumpPoss = (math.min(l1maxHeight, l2maxHeight) * scrollOffset) /
math.max(l1maxHeight, l2maxHeight);
scrollController2.jumpTo((jumpPoss));
});
You can follow #Tonny Bawembye's answer if you need to stop scrolling on max limit.

Related

Is there a listener that can call a function to move on to next set of data in Carousel (Page View)?

I've set up this Carousel using a PageView.builder. It displays 5 tiles at a time.
Once the user has swiped all the way over to the right & pulls on the last tile (see image)...I'd like to move onto the next set of 5 tiles in an array.
Is there an event handler for this? I've managed to set up a listener that can determine when the user has swiped to the last tile, but cannot figure out how to tell when they're pulling on this so it can be refreshed.
Appreciate any help I can get on this. Code below :)
import 'package:flutter/material.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
class RecommendationPanel extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return _buildRecommendationPanel();
}
}
class _buildRecommendationPanel extends State<RecommendationPanel> {
PageController _pageController = PageController();
#override
void initState() {
_pageController = PageController(viewportFraction: 1.0);
_pageController.addListener(_scrollListener);
super.initState();
}
void dispose() {
_pageController.dispose();
super.dispose();
}
_scrollListener() {
if (_pageController.offset >= _pageController.position.maxScrollExtent &&
!_pageController.position.outOfRange) {
setState(() {
//This is working in the sense that it tells when they're on the final tile
//I want it so knows when you drag to the right
print('Final tile');
//I could refresh the list and then just move everything back to #1 in the view...i.e. the last card [index 4] can now shift to 5
//_pageController.jumpToPage(0);
});
}
if (_pageController.offset <= _pageController.position.minScrollExtent &&
!_pageController.position.outOfRange) {
setState(() {
//Need to figure out how to work this - there's going to have to be another variable checking what place in the top N recommended array it is, and then adjust accordingly
print('Back to first tile');
//_pageController.jumpToPage(3);
});
}
}
#override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
//You may want to use aspect ratio here for tablet support
height: 270.0,
child: PageView.builder(
physics: const AlwaysScrollableScrollPhysics(),
itemCount: 5,
scrollDirection: Axis.horizontal,
controller: _pageController,
itemBuilder: (BuildContext context, int itemIndex) {
//I could potentially call something here to update the slider index
return _buildCarouselItem(context, itemIndex);
},
),
),
Container(
height: 30,
child: Center(
child: SmoothPageIndicator(
controller: _pageController,
count: 5,
effect: WormEffect(
spacing: 8.0,
dotHeight: 10,
dotWidth: 10,
activeDotColor: Colors.orange,
),
),
),
),
],
);
}
Widget _buildCarouselItem(BuildContext context, int itemIndex) {
List<String> labels = [
'Pub',
'Bar',
'Football Match',
'Nightclub',
'Book Festival',
'Six',
'Seven',
'Eight',
'Nine',
'Ten',
];
return Padding(
padding: EdgeInsets.symmetric(horizontal: 2.0),
child: Container(
height: double.infinity,
//color: Colors.red,
child: Column(
children: [
Container(
child: Card(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
child: Container(
// In here is where I should build each individual tile
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.all(Radius.circular(10.0)),
),
child: Container(
width: double.infinity,
height: 260,
child: Text(labels[itemIndex]),
),
),
),
),
],
),
),
);
}
Widget buildIndicator(BuildContext context, int itemIndex, int count) {
return AnimatedSmoothIndicator(
activeIndex: itemIndex,
count: count,
effect: WormEffect(
dotColor: Colors.grey,
activeDotColor: Colors.orange,
),
);
}
}

Flutter GestureDetector Not Working during AutoScroll

I was trying to detect when a user tapped a container during autoscroll. I am trying to build a scrolling game where a user taps containers inside a listview. However, my gesture detector onTap method does not work during scrolling. It only works after I am done scroling. How do I detect taps while autoscroll goes through my listview. This is my code:
class ColumnScreen extends StatefulWidget {
const ColumnScreen({Key? key}) : super(key: key);
#override
_ColumnScreenState createState() => _ColumnScreenState();
}
class _ColumnScreenState extends State<ColumnScreen> {
final ScrollController _controller = ScrollController();
void _scrollDown() {
_controller.animateTo(
_controller.position.maxScrollExtent,
duration: Duration(seconds: 2),
curve: Curves.linear,);
}
#override
void initState() {
WidgetsBinding.instance!.addPostFrameCallback((_){
//write or call your logic
//code will run when widget rendering complete
_scrollDown();
});
super.initState();
}
#override
Widget build(BuildContext context) {
final screenHeight = MediaQuery.of(context).size.height;
final screenWidth = MediaQuery.of(context).size.width;
return RotatedBox(
quarterTurns: 2,
child: Container(
height: screenHeight,
width: screenWidth/4,
color: Colors.yellow,
child: ScrollConfiguration(
behavior: ScrollConfiguration.of(context).copyWith(scrollbars: false),
child: ListView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: 5,
controller: _controller,
itemBuilder: (context, index)
{
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTapDown: (_){
print("Tap at ${index}");
},
child: Column(
children: [
Container(
height: screenHeight/4.5,
width: screenWidth/4,
color: Colors.grey,
child: Text("Index: ${index}"),
),
Divider(
thickness: 1,
color: Colors.black,
),
],
),
);
}
),
)
),
);
}
}

How to create a constant looping auto-scroll in Flutter?

I am seeking to create a constant scroll of a dynamic number of images across my screen (similar to a news ticker) in Flutter. I want this to be automatic and a constant speed, that also loops.
The simplest solution I have found is to use the Carousel Package which ticks almost all the boxes, except one. I am unable to get a constant scroll speed
A possible solution was to adjust autoPlayInterval to zero, but unfortunately, this paramater appears to need a value of around 50 or greater to run - therefore creating an even scroll.
Any idea on how to tweak it this with this package? Or another suitable solution?
Simplified code:
#override
Widget build(BuildContext context) {
return Container(
child: CarouselSlider(
items: DynamicImages.list
.map(
(e) => Padding(
padding: const EdgeInsets.all(8.0),
child: Image.asset('assets/images/$e.png'),
),
)
.toList(),
options: CarouselOptions(
autoPlay: true,
autoPlayCurve: Curves.linear,
autoPlayInterval: Duration(milliseconds: 0), /// carousel will not run if set to zero
autoPlayAnimationDuration: Duration(milliseconds: 1000)
),
),
);
}
}
I found a workaround that still takes advantage of the carousel_slider package. Instead of using autoplay, you just give it a CarouselController.
Start the animation by calling nextPage() in initState. Then in the carousel, set onPageChanged to call nextPage() again, which will continuously scroll from page to page with constant speed. Set the duration for the two nextPage() calls to whatever you want, as long as it's the same duration.
The only pause I experience is when it reaches the end of the list, where it needs to loop back to the first image. But it's negligible enough for my use case.
class _WelcomeScreenState extends State<WelcomeScreen> {
List<String> _images = [
"assets/image.jpg",
// ...
];
final CarouselController _carouselController = CarouselController();
#override
void initState() {
super.initState();
// Add this to start the animation
WidgetsBinding.instance.addPostFrameCallback((_) {
_carouselController.nextPage(duration: Duration(milliseconds: 2000));
});
}
#override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async => false,
child: ScreenBackgroundContainer(
child: Scaffold(
body: SafeArea(
child: Container(
height: double.maxFinite,
width: double.maxFinite,
child: SingleChildScrollView(
physics: BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: Column(
children: [
// Carousel
CarouselSlider(
carouselController: _performerCarouselController,
options: CarouselOptions(
pageSnapping: false,
height: 146,
viewportFraction: 114 / MediaQuery.of(context).size.width,
// Add this
onPageChanged: (_, __) {
_performerCarouselController.nextPage(duration: Duration(milliseconds: 2000));
}),
items: _performerImages
.map((image) => ClipRRect(
borderRadius: BorderRadius.circular(16.0),
child: Image.asset(image, width: 90, fit: BoxFit.cover)))
.toList(),
),
],
),
),
),
),
),
),
);
}
}
Screenshot
So, I've been working on this since posting and come up with a solution. I am posting my answer in case it helps any others in the future.
Basically, instead of using Carousel package, I used ListView.builder which then continuously grows as needed.
Of note, I needed to use WidgetsBinding.instance.addPostFrameCallback((timeStamp) {}); to get this to work.
It would still be great to see any other solutions, (as I am sure the below workaround could be improved).
import 'package:flutter/material.dart';
class ScrollLoop extends StatefulWidget {
const ScrollLoop({Key? key}) : super(key: key);
#override
_ScrollLoopState createState() => _ScrollLoopState();
}
class _ScrollLoopState extends State<ScrollLoop> {
ScrollController _controller = ScrollController();
/// [_list] is growable and holds the assets which will scroll.
List<String> _list = [
"assets/images/image1.png",
/// etc...
];
/// [_list2] holds duplicate data and is used to append to [_list].
List<String> _list2 = [];
/// [_listAppended] ensures [_list] is only appended once per cycle.
bool _listAppended = false;
#override
void initState() {
_list2.addAll(_list);
/// To auto-start the animation when the screen loads.
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
_startScroll();
});
/// The [_controller] will notify [_list] to be appended when the animation is near completion.
_controller.addListener(
() {
if (_controller.position.pixels >
_controller.position.maxScrollExtent * 0.90) {
if (_listAppended == false) {
_list.addAll(_list2);
_listAppended = true;
}
}
/// The [_controller] will listen for when the animation cycle completes,
/// so this can immediately re-start from the completed position.
if (_controller.position.pixels ==
_controller.position.maxScrollExtent) {
_listAppended = false;
setState(() {});
WidgetsBinding.instance!.addPostFrameCallback(
(timeStamp) {
_startScroll();
},
);
}
},
);
super.initState();
}
#override
void didChangeDependencies() {
super.didChangeDependencies();
}
void _startScroll() {
_controller.animateTo(_controller.position.maxScrollExtent,
duration: Duration(milliseconds: 8000), curve: Curves.linear);
}
#override
void dispose() {
_controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
final _size = MediaQuery.of(context).size;
return AbsorbPointer(
child: Material(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: ListView.builder(
shrinkWrap: true,
controller: _controller,
scrollDirection: Axis.horizontal,
itemCount: _list.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: _size.width / 4,
height: _size.height / 10,
child: Image.asset(_list[index]),
),
);
},
),
),
],
),
),
),
);
}
}

Is there a way to do a horizontal scroll with the centered selected item?

I am trying to achieve the result as in the example. I need to do a horizontal scroll with an item selection. Moreover, the selected list item is always centered when scrolling. I tried using TabBar, but it selected item always changes position.
Example:
https://i.stack.imgur.com/M153Z.jpg
This is the example, modify it as your requirement !
First you need to specify the title page controller's viewport fraction with .05
final titleController = PageController(viewportFraction: 0.5);
final contentController = PageController();
In the initState method add controller to contentController
contentController.addListener(() {
setState(() {
titleController.animateToPage(contentController.page,Duration(milliseconds:100));
});
});
Then add two pageviews to column
Column(
children: <Widget>[
Container(
height:100.0
child: PageView(
controller: titleController,
children: <Widget>[
Text('Title 1'),
Text('Title 2'),
//more....
]
)
),
PageView(
controller: contentController,
children: <Widget>[
Page1
Page2
//more....
]
)
],
)
From the details you provided, I feel you need this. This is a horizontal scroll, too with central item popped-up and selected.
import 'package:flutter/material.dart';
import 'dart:math';
/// circular images pageview
class CircularImagesPageView extends StatefulWidget {
/// constructor
const CircularImagesPageView(
{this.scaleFraction = 0.7,
this.fullScale = 1.0,
this.pagerHeight = 200.0,
this.currentPage = 2,
this.students,
this.indexChanged});
#override
_CircularImagesPageViewState createState() => _CircularImagesPageViewState();
/// scale fraction
final double scaleFraction;
/// full scale
final double fullScale;
/// pager height
final double pagerHeight;
/// current page
final int currentPage;
/// list students
final List<Map<String, String>> students;
/// index changed
final Function(int index) indexChanged;
}
class _CircularImagesPageViewState extends State<CircularImagesPageView> {
// control parameters
final double _viewPortFraction = 0.5;
PageController _pageController;
int _currentPage = 2;
double _page = 0.0;
#override
void initState() {
_currentPage = widget.currentPage;
_page = _currentPage.toDouble();
_pageController = PageController(
initialPage: _currentPage, viewportFraction: _viewPortFraction);
super.initState();
}
#override
Widget build(BuildContext context) {
return ListView(
physics: const NeverScrollableScrollPhysics(),
children: <Widget>[
const SizedBox(
// height: 20,
),
Container(
height: widget.pagerHeight,
child: NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification notification) {
if (notification is ScrollUpdateNotification) {
setState(() {
_page = _pageController.page;
});
}
return true;
},
child: PageView.builder(
onPageChanged: (int pos) {
setState(() {
_currentPage = pos;
widget.indexChanged(pos);
});
},
physics: const BouncingScrollPhysics(),
controller: _pageController,
itemCount: widget.students.length,
itemBuilder: (BuildContext context, int index) {
final double scale = max(
widget.scaleFraction,
(widget.fullScale - (index - _page).abs()) +
_viewPortFraction);
return circleOffer(widget.students[index]['image'], scale);
},
),
),
),
],
);
}
Widget circleOffer(String image, double scale) {
return Align(
alignment: Alignment.bottomCenter,
child: Container(
margin: const EdgeInsets.only(bottom: 10),
height: widget.pagerHeight * scale,
width: widget.pagerHeight * scale,
child: Card(
elevation: 4,
clipBehavior: Clip.antiAlias,
shape: CircleBorder(
side: BorderSide(color: Colors.grey.shade200, width: 5)),
child: Padding(
padding: const EdgeInsets.all(10.0),
child: image == null
? Image.asset(
assetName: IMAGE_USER_AVATAR,
fit: BoxFit.contain,
)
: Image.network(
image,
fit: BoxFit.cover,
),
),
),
),
);
}
}
To use this widget in your code you simply do like this.
Container(
height: MediaQuery.of(context).size.height * 0.25,
width: double.infinity,
child: CircularImagesPageView(
pagerHeight:
MediaQuery.of(context).size.height * 0.25,
students: _childrenList,
currentPage: _selectedChildIndex,
indexChanged: (int index) {
setState(() {
_selectedChildIndex = index;
_reloadHistoryList = true;
});
},
)

Flutter: Scrolling to a widget in ListView

How can I scroll to a special widget in a ListView?
For instance I want to scroll automatically to some Container in the ListView if I press a specific button.
ListView(children: <Widget>[
Container(...),
Container(...), #scroll for example to this container
Container(...)
]);
By far, the easiest solution is to use Scrollable.ensureVisible(context). As it does everything for you and work with any widget size. Fetching the context using GlobalKey.
The problem is that ListView won't render non-visible items. Meaning that your target most likely will not be built at all. Which means your target will have no context ; preventing you from using that method without some more work.
In the end, the easiest solution will be to replace your ListView by a SingleChildScrollView and wrap your children into a Column. Example :
class ScrollView extends StatelessWidget {
final dataKey = new GlobalKey();
#override
Widget build(BuildContext context) {
return new Scaffold(
primary: true,
appBar: new AppBar(
title: const Text('Home'),
),
body: new SingleChildScrollView(
child: new Column(
children: <Widget>[
new SizedBox(height: 160.0, width: double.infinity, child: new Card()),
new SizedBox(height: 160.0, width: double.infinity, child: new Card()),
new SizedBox(height: 160.0, width: double.infinity, child: new Card()),
// destination
new Card(
key: dataKey,
child: new Text("data\n\n\n\n\n\ndata"),
)
],
),
),
bottomNavigationBar: new RaisedButton(
onPressed: () => Scrollable.ensureVisible(dataKey.currentContext),
child: new Text("Scroll to data"),
),
);
}
}
NOTE : While this allows to scroll to the desired item easily, consider this method only for small predefined lists. As for bigger lists you'll get performance problems.
But it's possible to make Scrollable.ensureVisible work with ListView ; although it will require more work.
Unfortunately, ListView has no built-in approach to a scrollToIndex() function. You’ll have to develop your own way to measure to that element’s offset for animateTo() or jumpTo(), or you can search through these suggested solutions/plugins or from other posts like flutter ListView scroll to index not available
(the general scrollToIndex issue is discussed at flutter/issues/12319 since 2017, but still with no current plans)
But there is a different kind of ListView that does support scrollToIndex:
ScrollablePositionedList
dependency: scrollable_positioned_list
You set it up exactly like ListView and works the same, except you now have access to a ItemScrollController that does:
jumpTo({index, alignment})
scrollTo({index, alignment, duration, curve})
Simplified example:
ItemScrollController _scrollController = ItemScrollController();
ScrollablePositionedList.builder(
itemScrollController: _scrollController,
itemCount: _myList.length,
itemBuilder: (context, index) {
return _myList[index];
},
)
_scrollController.scrollTo(index: 150, duration: Duration(seconds: 1));
Please not that although the scrollable_positioned_list package is published by google.dev, they explicitly state that their packages are not officially supported Google products. - Source
Screenshot (Fixed height content)
If your items have fixed height, then you can use the following approach.
class HomePage extends StatelessWidget {
final ScrollController _controller = ScrollController();
final double _height = 100.0;
void _animateToIndex(int index) {
_controller.animateTo(
index * _height,
duration: Duration(seconds: 2),
curve: Curves.fastOutSlowIn,
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.arrow_downward),
onPressed: () => _animateToIndex(10),
),
body: ListView.builder(
controller: _controller,
itemCount: 20,
itemBuilder: (_, i) {
return SizedBox(
height: _height,
child: Card(
color: i == 10 ? Colors.blue : null,
child: Center(child: Text('Item $i')),
),
);
},
),
);
}
}
For people are trying to jump to widget in CustomScrollView.
First, add this plugin to your project.
Then look at my example code below:
class Example extends StatefulWidget {
#override
_ExampleState createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
AutoScrollController _autoScrollController;
final scrollDirection = Axis.vertical;
bool isExpaned = true;
bool get _isAppBarExpanded {
return _autoScrollController.hasClients &&
_autoScrollController.offset > (160 - kToolbarHeight);
}
#override
void initState() {
_autoScrollController = AutoScrollController(
viewportBoundaryGetter: () =>
Rect.fromLTRB(0, 0, 0, MediaQuery.of(context).padding.bottom),
axis: scrollDirection,
)..addListener(
() => _isAppBarExpanded
? isExpaned != false
? setState(
() {
isExpaned = false;
print('setState is called');
},
)
: {}
: isExpaned != true
? setState(() {
print('setState is called');
isExpaned = true;
})
: {},
);
super.initState();
}
Future _scrollToIndex(int index) async {
await _autoScrollController.scrollToIndex(index,
preferPosition: AutoScrollPosition.begin);
_autoScrollController.highlight(index);
}
Widget _wrapScrollTag({int index, Widget child}) {
return AutoScrollTag(
key: ValueKey(index),
controller: _autoScrollController,
index: index,
child: child,
highlightColor: Colors.black.withOpacity(0.1),
);
}
_buildSliverAppbar() {
return SliverAppBar(
brightness: Brightness.light,
pinned: true,
expandedHeight: 200.0,
backgroundColor: Colors.white,
flexibleSpace: FlexibleSpaceBar(
collapseMode: CollapseMode.parallax,
background: BackgroundSliverAppBar(),
),
bottom: PreferredSize(
preferredSize: Size.fromHeight(40),
child: AnimatedOpacity(
duration: Duration(milliseconds: 500),
opacity: isExpaned ? 0.0 : 1,
child: DefaultTabController(
length: 3,
child: TabBar(
onTap: (index) async {
_scrollToIndex(index);
},
tabs: List.generate(
3,
(i) {
return Tab(
text: 'Detail Business',
);
},
),
),
),
),
),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
controller: _autoScrollController,
slivers: <Widget>[
_buildSliverAppbar(),
SliverList(
delegate: SliverChildListDelegate([
_wrapScrollTag(
index: 0,
child: Container(
height: 300,
color: Colors.red,
)),
_wrapScrollTag(
index: 1,
child: Container(
height: 300,
color: Colors.red,
)),
_wrapScrollTag(
index: 2,
child: Container(
height: 300,
color: Colors.red,
)),
])),
],
),
);
}
}
Yeah it's just a example, use your brain to make it this idea become true
This solution improves upon other answers as it does not require hard-coding each elements' heights. Adding ScrollPosition.viewportDimension and ScrollPosition.maxScrollExtent yields the full content height. This can be used to estimate the position of an element at some index. If all elements are the same height, the estimation is perfect.
// Get the full content height.
final contentSize = controller.position.viewportDimension + controller.position.maxScrollExtent;
// Index to scroll to.
final index = 100;
// Estimate the target scroll position.
final target = contentSize * index / itemCount;
// Scroll to that position.
controller.position.animateTo(
target,
duration: const Duration(seconds: 2),
curve: Curves.easeInOut,
);
And a full example:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: "Flutter Test",
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
final controller = ScrollController();
final itemCount = 1000;
return Scaffold(
appBar: AppBar(
title: Text("Flutter Test"),
),
body: Column(
children: [
ElevatedButton(
child: Text("Scroll to 100th element"),
onPressed: () {
final contentSize = controller.position.viewportDimension + controller.position.maxScrollExtent;
final index = 100;
final target = contentSize * index / itemCount;
controller.position.animateTo(
target,
duration: const Duration(seconds: 2),
curve: Curves.easeInOut,
);
},
),
Expanded(
child: ListView.builder(
controller: controller,
itemBuilder: (context, index) {
return ListTile(
title: Text("Item at index $index."),
);
},
itemCount: itemCount,
),
)
],
),
);
}
}
You can use GlobalKey to access buildercontext.
I use GlobalObjectKey with Scrollable.
Define GlobalObjectKey in item of ListView
ListView.builder(
itemCount: category.length,
itemBuilder: (_, int index) {
return Container(
key: GlobalObjectKey(category[index].id),
You can navigate to item from anywhere
InkWell(
onTap: () {
Scrollable.ensureVisible(GlobalObjectKey(category?.id).currentContext);
You add scrollable animation changing property of ensureVisible
Scrollable.ensureVisible(
GlobalObjectKey(category?.id).currentContext,
duration: Duration(seconds: 1),// duration for scrolling time
alignment: .5, // 0 mean, scroll to the top, 0.5 mean, half
curve: Curves.easeInOutCubic);
You can just specify a ScrollController to your listview and call the animateTo method on button click.
A mininmal example to demonstrate animateTo usage :
class Example extends StatefulWidget {
#override
_ExampleState createState() => new _ExampleState();
}
class _ExampleState extends State<Example> {
ScrollController _controller = new ScrollController();
void _goToElement(int index){
_controller.animateTo((100.0 * index), // 100 is the height of container and index of 6th element is 5
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut);
}
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(),
body: new Column(
children: <Widget>[
new Expanded(
child: new ListView(
controller: _controller,
children: Colors.primaries.map((Color c) {
return new Container(
alignment: Alignment.center,
height: 100.0,
color: c,
child: new Text((Colors.primaries.indexOf(c)+1).toString()),
);
}).toList(),
),
),
new FlatButton(
// on press animate to 6 th element
onPressed: () => _goToElement(6),
child: new Text("Scroll to 6th element"),
),
],
),
);
}
}
Here is the solution for StatefulWidget if you want to made widget visible right after building the view tree.
By extending Remi's answer, you can achieve it with this code:
class ScrollView extends StatefulWidget {
// widget init
}
class _ScrollViewState extends State<ScrollView> {
final dataKey = new GlobalKey();
// + init state called
#override
Widget build(BuildContext context) {
return Scaffold(
primary: true,
appBar: AppBar(
title: const Text('Home'),
),
body: _renderBody(),
);
}
Widget _renderBody() {
var widget = SingleChildScrollView(
child: Column(
children: <Widget>[
SizedBox(height: 1160.0, width: double.infinity, child: new Card()),
SizedBox(height: 420.0, width: double.infinity, child: new Card()),
SizedBox(height: 760.0, width: double.infinity, child: new Card()),
// destination
Card(
key: dataKey,
child: Text("data\n\n\n\n\n\ndata"),
)
],
),
);
setState(() {
WidgetsBinding.instance!.addPostFrameCallback(
(_) => Scrollable.ensureVisible(dataKey.currentContext!));
});
return widget;
}
}
Output:
Use Dependency:
dependencies:
scroll_to_index: ^1.0.6
Code: (Scroll will always perform 6th index widget as its added below as hardcoded, try with scroll index which you required for scrolling to specific widget)
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final scrollDirection = Axis.vertical;
AutoScrollController controller;
List<List<int>> randomList;
#override
void initState() {
super.initState();
controller = AutoScrollController(
viewportBoundaryGetter: () =>
Rect.fromLTRB(0, 0, 0, MediaQuery.of(context).padding.bottom),
axis: scrollDirection);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(
scrollDirection: scrollDirection,
controller: controller,
children: <Widget>[
...List.generate(20, (index) {
return AutoScrollTag(
key: ValueKey(index),
controller: controller,
index: index,
child: Container(
height: 100,
color: Colors.red,
margin: EdgeInsets.all(10),
child: Center(child: Text('index: $index')),
),
highlightColor: Colors.black.withOpacity(0.1),
);
}),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _scrollToIndex,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
// Scroll listview to the sixth item of list, scrollling is dependent on this number
Future _scrollToIndex() async {
await controller.scrollToIndex(6, preferPosition: AutoScrollPosition.begin);
}
}
I found a perfect solution to it using ListView.
I forgot where the solution comes from, so I posted my code. This credit belongs to other one.
21/09/22:edit. I posted a complete example here, hope it is clearer.
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
class CScrollToPositionPage extends StatefulWidget {
CScrollToPositionPage();
#override
State<StatefulWidget> createState() => CScrollToPositionPageState();
}
class CScrollToPositionPageState extends State<CScrollToPositionPage> {
static double TEXT_ITEM_HEIGHT = 80;
final _formKey = GlobalKey<FormState>();
late List _controls;
List<FocusNode> _lstFocusNodes = [];
final __item_count = 30;
#override
void initState() {
super.initState();
_controls = [];
for (int i = 0; i < __item_count; ++i) {
_controls.add(TextEditingController(text: 'hello $i'));
FocusNode fn = FocusNode();
_lstFocusNodes.add(fn);
fn.addListener(() {
if (fn.hasFocus) {
_ensureVisible(i, fn);
}
});
}
}
#override
void dispose() {
super.dispose();
for (int i = 0; i < __item_count; ++i) {
(_controls[i] as TextEditingController).dispose();
}
}
#override
Widget build(BuildContext context) {
List<Widget> widgets = [];
for (int i = 0; i < __item_count; ++i) {
widgets.add(TextFormField(focusNode: _lstFocusNodes[i],controller: _controls[i],));
}
return Scaffold( body: Container( margin: const EdgeInsets.all(8),
height: TEXT_ITEM_HEIGHT * __item_count,
child: Form(key: _formKey, child: ListView( children: widgets)))
);
}
Future<void> _keyboardToggled() async {
if (mounted){
EdgeInsets edgeInsets = MediaQuery.of(context).viewInsets;
while (mounted && MediaQuery.of(context).viewInsets == edgeInsets) {
await Future.delayed(const Duration(milliseconds: 10));
}
}
return;
}
Future<void> _ensureVisible(int index,FocusNode focusNode) async {
if (!focusNode.hasFocus){
debugPrint("ensureVisible. has not the focus. return");
return;
}
debugPrint("ensureVisible. $index");
// Wait for the keyboard to come into view
await Future.any([Future.delayed(const Duration(milliseconds: 300)), _keyboardToggled()]);
var renderObj = focusNode.context!.findRenderObject();
if( renderObj == null ) {
return;
}
var vp = RenderAbstractViewport.of(renderObj);
if (vp == null) {
debugPrint("ensureVisible. skip. not working in Scrollable");
return;
}
// Get the Scrollable state (in order to retrieve its offset)
ScrollableState scrollableState = Scrollable.of(focusNode.context!)!;
// Get its offset
ScrollPosition position = scrollableState.position;
double alignment;
if (position.pixels > vp.getOffsetToReveal(renderObj, 0.0).offset) {
// Move down to the top of the viewport
alignment = 0.0;
} else if (position.pixels < vp.getOffsetToReveal(renderObj, 1.0).offset){
// Move up to the bottom of the viewport
alignment = 1.0;
} else {
// No scrolling is necessary to reveal the child
debugPrint("ensureVisible. no scrolling is necessary");
return;
}
position.ensureVisible(
renderObj,
alignment: alignment,
duration: const Duration(milliseconds: 300),
);
}
}
To achieve initial scrolling at a particular index in a list of items
on tap of the floating action button you will be scrolled to an index of 10 in a list of items
class HomePage extends StatelessWidget {
final _controller = ScrollController();
final _height = 100.0;
#override
Widget build(BuildContext context) {
// to achieve initial scrolling at particular index
SchedulerBinding.instance.addPostFrameCallback((_) {
_scrollToindex(20);
});
return Scaffold(
appBar: AppBar(),
floatingActionButton: FloatingActionButton(
onPressed: () => _scrollToindex(10),
child: Icon(Icons.arrow_downward),
),
body: ListView.builder(
controller: _controller,
itemCount: 100,
itemBuilder: (_, i) => Container(
height: _height,
child: Card(child: Center(child: Text("Item $i"))),
),
),
);
}
// on tap, scroll to particular index
_scrollToindex(i) => _controller.animateTo(_height * i,
duration: Duration(seconds: 2), curve: Curves.fastOutSlowIn);
}
I am posting a solution here in which List View will scroll 100 pixel right and left . you can change the value according to your requirements. It might be helpful for someone who want to scroll list in both direction
import 'package:flutter/material.dart';
class HorizontalSlider extends StatelessWidget {
HorizontalSlider({Key? key}) : super(key: key);
// Dummy Month name
List<String> monthName = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"July",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
];
ScrollController slideController = new ScrollController();
#override
Widget build(BuildContext context) {
return Container(
child: Flex(
direction: Axis.horizontal,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: () {
// Here monthScroller.position.pixels represent current postion
// of scroller
slideController.animateTo(
slideController.position.pixels - 100, // move slider to left
duration: Duration(
seconds: 1,
),
curve: Curves.ease,
);
},
child: Icon(Icons.arrow_left),
),
Container(
height: 50,
width: MediaQuery.of(context).size.width * 0.7,
child: ListView(
scrollDirection: Axis.horizontal,
controller: slideController,
physics: ScrollPhysics(),
children: monthName
.map((e) => Padding(
padding: const EdgeInsets.all(12.0),
child: Text("$e"),
))
.toList(),
),
),
GestureDetector(
onTap: () {
slideController.animateTo(
slideController.position.pixels +
100, // move slider 100px to right
duration: Duration(
seconds: 1,
),
curve: Curves.ease,
);
},
child: Icon(Icons.arrow_right),
),
],
),
);
}
}
The simplest way is to call this method inside your InitState method. (not the build to evict unwanted errors)
WidgetsBinding.instance.addPostFrameCallback((_) => Scrollable.ensureVisible(targetKey.currentContext!))
WidgetsBinding.instance.addPostFrameCallback will guarantee that the list is builded and the this automatic search for your target and move the scroll to it. You can then customize the animation of the scroll effect on the Scrollable.ensureVisible method
Note: Remember to add the targetKey (a GlobalKey) to the widget you want to scroll to.
Adding with Rémi Rousselet's answer,
If there is a case you need to scroll past to end scroll position with addition of keyboard pop up, this might be hided by the keyboard. Also you might notice the scroll animation is a bit inconsistent when keyboard pops up(there is addition animation when keyboard pops up), and sometimes acts weird. In that case wait till the keyboard finishes animation(500ms for ios).
BuildContext context = key.currentContext;
Future.delayed(const Duration(milliseconds: 650), () {
Scrollable.of(context).position.ensureVisible(
context.findRenderObject(),
duration: const Duration(milliseconds: 600));
});
You can also simply use the FixedExtentScrollController for same size items with the index of your initialItem :
controller: FixedExtentScrollController(initialItem: itemIndex);
The documentation : Creates a scroll controller for scrollables whose items have the same size.
Simply use page view controller.
Example:
var controller = PageController();
ListView.builder(
controller: controller,
itemCount: 15,
itemBuilder: (BuildContext context, int index) {
return children[index);
},
),
ElevatedButton(
onPressed: () {
controller.animateToPage(5, //any index that you want to go
duration: Duration(milliseconds: 700), curve: Curves.linear);
},
child: Text(
"Contact me",),
You can use the controller.jumpTo(100) after the loading finish