Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to create a transition between indexed stacks in flutter

Tags:

flutter

Is there an animation widget or a way to make transitions between indexed stacks. I am having the stack cover the entire page. Or is there an easier way to transition from one page to another if I made several pages. Thanks new to flutter.

like image 766
Darkends Avatar asked Jun 15 '19 06:06

Darkends


People also ask

What is stack in flutter?

In Flutter, Stack is a widget that positions its children relative to the edges of its box. It can be useful if you want to overlap several widgets in a simple way.

What is a transition in flutter?

In general, a transition is nothing but moving an object from one place to another. In flutter, it is also the same but in terms of widgets like container, button, and also pages since everything in flutter is itself a widget. We will be using a certain transition technique to animate our transition between two pages or widgets.

How to overlap multiple containers in flutter?

If you want to build a beautiful User Interface of the App, you need to know all kinds of Widgets available in Flutter. In this example, I have used Stack () and Positioned () widget to overlap multiple containers together.

What is an indexedstack?

An IndexedStack is a stack where only one element is displayed at one time by its index. It takes children like a usual Stack but in contrast to it, only displays one child at one time. In a way, it’s not a stack and more of a way to easily switch between children when you need to.


2 Answers

In my case, I have used AnimatedSwitcher but the animations did not work. So I found that to animate we need to use a Key for each child.

In my case

 List<ValueKey<int>> _keys = [ValueKey<int>(0),ValueKey<int>(1),ValueKey<int>(2)];

 int _currentIndex = 0;

Now use Key in AnimatedSwitcher as below:

AnimatedSwitcher(
  transitionBuilder: AnimatedSwitcher.defaultTransitionBuilder,
  duration: const Duration(milliseconds: 500),
  child: IndexedStack(
    index: _currentIndex,
    key: ValueKey<int>(_currentIndex),
    children: [
      Page1(key: _keys[0],),
      Page2(key:_keys[1]),
      Page3(key:_keys[2])
    ],
  ),
);

change current index:

setState(() {
  _currentIndex = indexOfTabOrBottomNavItem;
});

NOTE: Using this way, you are not able to keep state of each widget.

UPDATE : I am using animations package in my project. You can use PageTransitionSwitcher from animations package. https://pub.dev/packages/animations

PageTransitionSwitcher(
  duration: Duration(milliseconds: 250),
  transitionBuilder: (widget, anim1, anim2) {
    return FadeScaleTransition(
      animation: anim1,
      child: widget,
    );
  },
  child: IndexedStack(
    index: _currentIndex,
    key: ValueKey<int>(_currentIndex),
    children: [
      Page1(),
      Page2(),
      Page3()
    ],
  ),  
);
like image 61
Sanjay Kumar Avatar answered Oct 06 '22 19:10

Sanjay Kumar


I think what you are looking for is AnimatedSwitcher.

e.g.

AnimatedSwitcher(
  duration: Duration(milliseconds: 300),
  child: _indexedArray[_index],
);

By default AnimatedSwitcher runs a fade-in transition. You can explore transitionBuilder property to run custom animation.

You might also want to take a look at AnimatedCrossFade if you need to switch only between 2 widgets.

Let me know if this helped.

like image 45
George Avatar answered Oct 06 '22 19:10

George