MrRBM97
MrRBM97

Reputation: 169

Slide Widget Under Another Animated Builder Flutter

I have a page with two widgets that i am looking to animate. I currently have created the rough animation that I am looking for, the only problem im having is that I want the larger of the two widgets to slide under the other and not over the top. I have included the code below:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Row(
        children: <Widget>[
          AnimatedBuilder(
            animation: transitionAnimation,
            builder: (context, child) {
              return SlideTransition(
                  position: Tween<Offset>(
                    begin: Offset(-1, 0),
                    end: Offset(0, 0),
                  ).animate(CurvedAnimation(
                      curve: Interval(.01, 0.25, curve: Curves.easeInExpo),
                      parent: transitionAnimation)),
                  child: child);
            },
            child: Container(
              width: MediaQuery.of(context).size.width * 0.15,
              color: Colors.black,
            ),
          ),
          AnimatedBuilder(
            animation: transitionAnimation,
            builder: (context, child) {
              return SlideTransition(
                  position: Tween<Offset>(
                    begin: Offset(-2, 0),
                    end: Offset(0, 0),
                  ).animate(CurvedAnimation(
                      curve: Interval(0, 1, curve: Curves.easeIn),
                      parent: transitionAnimation)),
                  child: child);
            },
            child: Container(
              width: MediaQuery.of(context).size.width * 0.85,
              color: Colors.orange,
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.of(context).push(MaterialPageRoute(
            builder: (context) => FirstPage(),
          ));
        },
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );

I am happy with the speed and direction of the animation, the only problem is, as I said, that I would like the orange widget to slide beneath the black rather than above it. I have tried to use a stack, but I wasn't successful in getting the orange widget on the right-hand side of the page.

Any ideas would be greatly appreciated!

Upvotes: 1

Views: 1846

Answers (1)

bluenile
bluenile

Reputation: 6029

To slide the Orange widget from beneath the Black widget you can try doing the following:

  1. Replace Row with Stack
  2. Interchange the positions of AnimatedBuilder

Please see the code below. You may also try the code on DartPad https://dartPad.dev/790dd8c7ef8119d0b560419fdca1727b

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  AnimationController transitionAnimation;
  @override
  void initState() {
    super.initState();
    transitionAnimation = AnimationController(
      duration: const Duration(seconds: 5),
      vsync: this,
    );
    transitionAnimation.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Positioned(
            left: MediaQuery.of(context).size.width * 0.15,
            child: AnimatedBuilder(
              animation: transitionAnimation,
              builder: (context, child) {
                return SlideTransition(
                    position: Tween<Offset>(
                      begin: const Offset(-2, 0),
                      end: const Offset(0, 0),
                    ).animate(CurvedAnimation(
                        curve: const Interval(0, 1, curve: Curves.easeIn),
                        parent: transitionAnimation)),
                    child: child);
              },
              child: Container(
                height: MediaQuery.of(context).size.height,
                width: MediaQuery.of(context).size.width * 0.85,
                color: Colors.orange,
              ),
            ),
          ),
          Positioned(
            left: 0,
            child: AnimatedBuilder(
              animation: transitionAnimation,
              builder: (context, child) {
                return SlideTransition(
                    position: Tween<Offset>(
                      begin: const Offset(-1, 0),
                      end: const Offset(0, 0),
                    ).animate(CurvedAnimation(
                        curve:
                            const Interval(.01, 0.25, curve: Curves.easeInExpo),
                        parent: transitionAnimation)),
                    child: child);
              },
              child: Container(
                height: MediaQuery.of(context).size.height,
                width: MediaQuery.of(context).size.width * 0.15,
                color: Colors.black,
              ),
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          transitionAnimation.repeat();
//           Navigator.of(context).push(MaterialPageRoute(
//             builder: (context) => FirstPage(),
//           ));
        },
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }
}

Upvotes: 2

Related Questions