How can I create a Column widget with the functionality of an AnimatedListview in Flutter - flutter

I have a Column that I need to behave like an AnimatedList would: to animate the widgets when I add or remove them. The reason I can't use AnimatedList (as far as I can tell) is because my children are Cards wrapped in Flexible widgets. I only have 11 of these hard coded cards and I need them all to be visible should the user decide to have all of them "active".
Here's the desired effect but with terrible code:
I'm currently using the provider package to control the column's children. Adding in new Cards is simple enough, as they just run their animation on initState. Removing them involves a convoluted way of triggering the reverse animation, listening for when it finishes and then triggering a function that removes it from the Column.

Related

Flutter Recreating the Hero Transition replacing Navigator with a custom Animator

Looking at the Flutter Hero Transition, it appears to move the tagged Widgets to an Overlay class that exists in all Navigator Widgets but sits above the main content in the stack.
If this is correct, it allows the Hero to widgets to still respond to the Route scope and its animators but exist above the actual route content. How is this actually done efficiently? Surely this involves taking an entire Widget and storing it in a state for the duration of the animation. That Widget still has to respond to intrinsic responses from its original position such as slivers responding to active scroll actions.
Recreating this could be done with state management but I wondered how the standard hero actually does this. It seems like Widgets are effectively duplicated and then conditionally rendered on the screen defaulting to the overlay during the route animation and swapping out the original widget with an Offstage or similar. Is this how it is done?
The reason for trying to understand it is the need to replicate this behaviour in situations where Navigator is not an effective use case for a transition taking place internally on a page. I built an accordion style navigator but still want a hero transition to take place on the AppBar / NavigationBar. I know that this could be done with Navigator but it doesn't suit the use case. I could also predefine the AppBar content for each internal navigator state of the accordion but that is a lot of additional code.

Is building one large better than building small ~75-100 widgets

Consider the case that I have 3 adjacent list view with each list view having a sized widget of some height. Each list view has around 24 child items. I'm trying to scale the widget using onScaleUpdate in GestureDetector.
onScaleUpdate I want to change the height of each child item of all the 3 listviews.
Is rebuilding all child better or should I rebuild the whole widget?
As #Yeasin Sheikh pointed out, using ListView.builder is good because it builds only the needed children (the ones inside the screen and just a few ones outside as precaution). And as to actually answering your question, I'm no Flutter expert, but using ListView.builder I don't think it makes that much difference, Flutter is intelligent enough to solve this by itself.

Animate Flutter DataTable

I'm relatively new to Flutter and am now trying to animate some of my widgets.
I'm currently struggling to animate my DataTable. I have a dynamically built DataTable that has to be able to add and delete values. I'm trying to animate insertions and deletions of a new Column. Right now this is happening instantaneously.
I've seen that Flutter offers a lot of animated widgets out of the box already. For example AnimatedList which would probably be perfect. Unfortunately, I haven't found any alternatives like that for the DataTable widget.
How would one go on to animate something like this?

In which circumstances should you use listview instead of listview.builder?

Like the topic I was wondering of there are any circumstances one should use the regular ListView instead of the ListView.builder in flutter, like if there are few items in a list could the ListView give better performance?
ListView has actually four different ways to use it , But let discuss ListView and ListView.builder
ListView : It has a children
property that takes a collection of static widgets. A ListView takes a small number of other widgets and makes it scrollable. Why a “small number”? Because this is designed to be a static list, one that you, the developer, simply types into the build() method
by hand.
ListView.builder : ListView’s alternative constructor, ListView.builder receives two
parameters, an itemCount and an ItemBuilder property that is a
function. This makes the ListView lazy-loaded. The itemBuilder function
dynamically creates children widgets on demand. As the user scrolls close
to the bottom of the list, itemBuilder creates new items to be scrolled into
view. And when we scroll something far enough off the screen, it is paged
out of memory and disposed of. Pretty cool.
Reference : taken from Rap Payne's Beginning App Development with Flutter (Great Book for beginners! , not an affiliate link).
official documentation for ListView .
ListView is the most commonly used scrolling widget. It displays its children one after another in the scroll direction. So if you just want to show some widgets below earch other and you need to scroll them you use ListView.
ListView.builder is a way of constructing the list where children’s (Widgets) are built on demand. However, instead of returning a static widget, it calls a function which can be called multiple times (based on itemCount ) and it’s possible to return different widget at each call.
I guess the simple answer you were looking for is:
Use ListView.builder whenever you iterate over an array of 'similar' elements.
Use ListView when items in your list are completely different from one another.
Think of ListView as scrollable Column

How many setState() calls is overkill for Flutter?

I am new to Flutter and reactive programming is also new thing for me.
Let's say I want to build a timer with Flutter.
I add a Scaffold with all the necessary stuff in it and I add a IconButton which starts the Stopwatch and Text which displays elapsed time. I also add Timer.periodic to periodically (every 0.5 second) update the text.
Text Widget controls it's own state by checking if Stopwatch is running and updating it's values.
So now let's say I want to have more complicated logic that changes the text based on some actions with other buttons which are the siblings of Text. However it is not possible to call setState of Text widget directly from sibling widgets. As I understand the point of reactive paradigm is that the state can be passed down the Tree. However if I make my Scaffold as StatefulWidget and update the state of the parent every 0.5 second it will redraw my entire Scaffold with all it's children. So eventually when the Scaffold gets big enough it will have to update everything instead of single Text widget.
Am I correct? And is there any solution to this. I read something about Streams and Sinks however it looks very complicated and I think that there should be another solution.
You don't need to rebuild the whole tree, if the state only changed in a sub widget, ideally you want to call set state in that widget so only that part of the tree (the one whose state changed) is rebuilt.
Streams aren't really that complicated, it's a good way for you to send messages between different components in your app, which is what you're trying to do here.
In your case you can also use a ValueNotifier to store state in the parent widget, or maybe an AnimationController, and send its listener down to the sub widget that needs be updated on change.
In any case, the state is lifted to a parent widget, which then becomes accessible to the sub widget through a listener, or a stream. When the listener triggers a signal, you rebuild the sub widget only.
Extract out widget and call setState() form that widget and it's don't render all the widget again