Flutter scroll to specific item in list - flutter

I have a ListView in Flutter that I allow users to dynamically add items to. After adding an item I would like for the list to scroll to the item that was added. I've attached a ScrollController to the ListView so I could use animateTo to scroll, but I'm unsure of how to determine the offset to scroll down to. I had something like this:
_scrollController.animateTo(
addedIndex.toDouble() * 100,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 300),
);
where addedIndex is the order that the item was added to the list. That doesn't quite work though, and seems like it would only work if I could figure out the height of each item in the list, which I'm not sure how to do. Is there a better way to figure out exactly where to scroll to?

First, create a new globalKey.
final GlobalKey globalKey = GlobalKey();
Second, add globalKey to the widget you want to move to.
Then, get the widget location based on globalKey.
RenderBox box = globalKey.currentContext.findRenderObject();
Offset offset = box.localToGlobal(Offset.zero);
The widget position obtained is relative to the current page display status , widget height includes status bar and AppBar height.
status bar height
import 'dart:ui’;
MediaQueryData.fromWindow(window).padding.top
AppBar height 56.0
scrollView's offset need to add
double animationHeight = _controller.offset + offset.dy - MediaQueryData.fromWindow(window).padding.top - 56.0;
_controller.animateTo(animationHeight, duration: Duration(milliseconds: 500), curve: Curves.decelerate);
hope it will help you.

Related

Flutter ListView maxscrollextent not working when list items height not consistent

Please help on scrolling to the end of Flutter ListView,
The list is scrolling to end properly when items are with the same height, but when dynamic height I can't get it to work since months ago.
I tried almost everything to get the list to the end with different children heights like the sample below:
WidgetsBinding.instance!.addPostFrameCallback((_) {
// SchedulerBinding.instance!.addPostFrameCallback((_) {
if (_scrollController.hasClients) {
double position = _scrollController.position.maxScrollExtent;
// _scrollController.jumpTo(position);
_scrollController.animateTo(
position,
duration: Duration(milliseconds: 500),
curve: Curves.easeOut,
);
}
});
also tried putting the scrolltoend procedure inside timer with different durations, tried also jumpTo.. but no success.
Please help

Flutter: how to make card expand to whole screen?

I have some cards in gridview. I want ontap of any card, they should expand to whole screen.
this are the cards,
HAVE TO DELETE IMAGES BECAUSE OF CONFIDENTIALITY
I want them to expand to whole screen like this,
what I have tried,
-> I have tried using OpenContainer widget,
-> custom hero animation
but they both use pageroute but I want them to do it without the page route because there is some content like appbar which would be same for both screen so I don't want them rebuild it everytime user tap on any of the card.
if anyone can lead me to right direction that would be awesome
Take a look at this package: https://pub.dev/packages/animations
if you don't like using pageroute you can just switch your widget using StatefulWidget and Visibility.
And wrap your Card with InkWell then use the onTap / onPressed callback to set change the state so the Visibility will start work now
Create a function
Widget transition(Widget _child) {
return PageTransitionSwitcher(
duration: const Duration(milliseconds: 800),
transitionBuilder: (
Widget child,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
return SharedAxisTransition(
child: child,
animation: animation,
secondaryAnimation: secondaryAnimation,
transitionType: SharedAxisTransitionType.horizontal,
);
},
child: _child,
);
}
Add it on your scaffold:
transition(child:condition ? widget_1 : widget_2);
I'm not 100% sure what you meant by expand to the whole screen, but you probably need to use the Hero() widget, wrap each widget you want to expand with a Hero() widget and give it a tag: 'spo2' for example, make a new screen for the second image, wrap each card with a Hero() and the same tag in the previous screen for the expanded card.
For further reading check out https://api.flutter.dev/flutter/widgets/Hero-class.html
Try using a dialogue and animating the opening.

Flutter - Which widget is the most optimized for live scaling Image with 0 to 1 values?

I want to animate a certain widget as long as the music is playing. The data is obtained continuously from a callback within my code.
void _onMusicFFTData(double normalized){
// TODO: somehow scale image widget with the normalized value
}
My values received from this callback are already interpolated, so I do not want any kind of Curve usage. I want to scale a certain image directly with the values received from the callback. I saw that there is ScaleTransition with scale property.
ScaleTransition(scale: ,)
I could not see any ways to feed normalized 0 to 1 values to this widget. It only takes predefined animation such as
_controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this,
)..repeat(reverse: true);
_animation = CurvedAnimation(
parent: _controller,
curve: Curves.fastOutSlowIn,
);
I can basically use SetState and use a Transform.scale but I dont think thats the right way to animate as it feels unintuitive and more like a workaround. Which widget would help me scale my image widget with 0 to 1 live values during runtime?

How do I set the focus of the list view on the last item in chat list

I have a list view where I show the chat list but when the list view always focus to the first item.
i set stack from bottom true in java and this work for me
Try attach ScrollController controller; on ListView.builder() and when to scroll use this
controller.animateTo(controller.position.maxScrollExtent,
duration: Duration(seconds: 2), curve: Curves.easeIn);

How to specify CustomScrollView to scroll to a Sliver Widget in Flutter?

First, I have a CustomScrollView that has several SilverGrids as children.
CustomScrollView(
controller: _scrollController,
slivers: <Widget>[
_grid1(),
_grid2(),
_grid3(),
],
)
Then I have a button that controls the CustomScrollView to scroll to just show Grid2 when pressed
RaisedButton(onPressed: () {
_scrollController.animateTo( _getGrid2Offset(),
duration: Duration(milliseconds: 200), curve: Curves.linear);
},)
But I don't know how to calculate the position of Grid2 relative to CustomScrollView
The answer is you can't (right now) to calculate this value directly.
You will have to calculate the offset yourself which will cover most of the basic cases. If you have a list of items with the same height then you can do a simple
var offset = index * height + somethingElse
and give this value to the widget. If your case is more complex then you should create a function to calculate it from all the factors like size,type,margin,padding.