AnimatedSwitcher doesn't switch between Widgets - flutter

Goal: Switching between two very simple Widgets containing simple Text with the help of AnimatedSwitcher.
Below is my code:
Whenever i press the button which should trigger the animation from the first widget to the second widget, nothing happens.
Both prints appear in the console : 'before' and 'after'.
Is this some kind of bug or why isn't it working?

Since both of your widgets are pretty much the same, flutter needs some kind of differentiation between them. The most straight forward approach would be using a UniqKey() on both of your containers.
...
Container(key: UniqKey(), ...),

Related

How to use SizeTransition with Text inside a Row without overflows

Problem summary
I'm building simple animation in which, simply, a panel expands to the right on a onTap event. The layout can be summarized as follows:
The panel has many tiles. Each tile has its own:
leading icon
title text
trailing icon
Each tile, when expanded, shows the three widgets above
Each tile, when shrinked, just shows the leading icon
The panel is scrollable to hold many icons such as these
At the end of the panel there's a simple icon which, when tapped, triggers the "expand" or "shrink" animation
The animation is built with an AnimatedContainer on the panel and a SizeTransition on the single tiles (via one single controller on the parent class)
The problem is simple: when the tiles shrink, their inner text overflows in the row.
Sample code
I just made this reproducible example on DartPad.
The obvious solution isn't helping me out
The most obvious solution is to simply wrap the Text inside a Flexible Widget: this advised by Flutter's docs and it makes sense, but unluckily it completely breaks my Layout.
Flexible introduces a flex factor that in this context is 100% unwanted.
I want my leading icons to be always at the end of the row, so my Spacer widget should "prevail" there.
I can't just play with flex factors there, as it would unintendedly hide text depending on its value.
Another code smell
Another thing I really don't like is the fact that I have to build a controller on the parent class, but I'm not actually using it on the parent class (I'm just exploiting it so I can give it to the children).
I'm new to animations in Flutter so I'm kinda confused on how I should orchestrate the whole thing here.
Any help will be appreciated / upvoted / accepted. Thank you!
As far as I understood you in a right way you just need set sizes for Row inside of SizeTransition instead of Container sizes
Here is your modified sample https://dartpad.dev/?id=a2408d29a1e8c6ce7a1cef8f21e7491d
I'd try an OverflowBox or a FittedBox (wrapping your text), depending on the result you want to achieve.

Optimize a GridView of data inside a pageview

I'm working on a custom calendar package but I'm having some performance difficulties while doing that.
My calendar layout is built from PageViews and each pageview contains a Gridview and each GridView contains 42 children representing the days of the month. Like this:-
Issue -
The problem is with scrolling the pageview. When I scroll to the new PageView the new PageView gets build and it builds its GridView and GridView builds its Children. All these tasks while scrolling leads to jank in scrolling animation.
Note: Scrolling to previous PageViews is smooth because I'm using AutomaticKeepAlives in PageView which stops PageView from rebuilding constantly on scroll.
DartPad -
The example DartPad for the above calendar can be found here - https://dartpad.dev/?id=762d835c5b9acadd785ee9269294c1e6
Now, what I'm looking for are the ways through which I can improve its performance.
Maybe I can somehow reuse the components for GridView children's dates by manually caching them by storing them as fields somewhere. But I don't know if that would work as these date styles have non-constant properties like the trailing one has different colors and the general one has a different one.
Maybe I can somehow preload pageviews in advance? I don't know if that would work when swiping fast.
Also, if it matters, currently I'm using Riverpod in my package for managing the calendar state.
So, if there is any way to improve the performance then please help me with that.
Current dirty solutions I found to this problem are:-
Setting viewportFraction: 0.99 in PageController of PageView. (It will help as it preloads the previous page and forward page). Suggested here #504337877.
Or use this preload_page_view package. Suggested here #636388237.
Currently, flutter doesn't support pre caching the PageView pages in advance but there are several issues about it on Github like this - #31191 , #45632 , and a closed pull request #42107 due to IOS related issue.
I failed to reuse the same widget in multiple places without rebuilding them.
And even if I succeed with the above I might fail to reuse states of child widgets of PageView as changing Pages means the whole child of PageView to be rebuilt as the index is new. So I cannot cache child widgets until I can reuse states in PageView.builder and GridView.Builder. A similar case for ListView is described in #49126.
If I'm wrong about the reusing same widget at multiple places then please correct me with a working small example as I'm more than happy to learn more. You can use this DartPad as starting point. The task is to bring the rebuild of customContainer from 42 to 1.
Not accepted as an answer as there could be more great answers to this in the future.

How can I create a Column widget with the functionality of an AnimatedListview in 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.

Flutter onPressed() vs onTap()

I've been using Flutter for about 8 weeks.
It's awesome, no question.
However, there has been something bugging me since I've started, and that is, why do some widgets have an onTap method whilst others have onPressed? Google has not been able to give me any answers.
If this is not relevant or on-topic enough for Stack Overflow, sorry about that, please point me to the right community to ask.
In my experience, onTap() is used for any gesture capture and onPressed() especially for buttons.
Just like in real life, when you have to use a button, you'll press it.
Hope this will solve your troubles even though I'm not totally sure about that.
Currently they both are same. Just use onPressed with Buttons and onTap with other widgets.
Flutter is still new so there are lots of overlaps. May be in future updates they will make it more streamline . Like they will merge many widgets which are almost same.
The Source of differents between onTap and onPressed is GestureDetector used in buttons.
onTap should give you response in the first moment touching the screen.
onPressed should be equvalent to .onTapDown property - the last moment touching the screen.

How to update StatefulWidget from other class?

First of all, I'm a real beginner with Flutter, so I have probably not even grasped the basic design patterns yet. My problem is probably simple for someone who understands flutter. Here goes:
I have a tabbed GUI in my app and a floating action button. Each tab has a list that I want to update using that action button (FAB). The easy way to do this seemed to have one action button for each tab, but I read that you should not do this in flutter. So I made the button "global" and call a method on the current tab when the button is pressed. The method then adds some data that should be shown in the list, but the problem is that the list of course isn't actually updated just because I add data. The setState() is never called inside that list widget.
All the exampels I've found are very simple with the FAB being inside the list having access to its setState() method. But in my case I'm outside the list. How do I solve this? Notifier? Stream? I'm lost...
I think the correct solution is to use a "Stack" and "Positioned" widged like:
- Stack
- Scaffold
- TabBar
- Positioned
- FloatingActionButton