如何链接 SlideTransition

huangapple go评论66阅读模式
英文:

How to chain the SlideTransition

问题

I use animation making my widget move.

when I click play button, I want the widget

  1. move from 1 to 2
  2. waiting for a few seconds
  3. move from 2 to 3 and disappear

Now I just completed 1 and have no idea how to achieve 2 and 3

Here is my code in State.

  @override
  void initState() {
    animationController = AnimationController(duration: widget.duration, vsync: this);
    animation = Tween(
        begin: const Offset(1.0, 0),
        end: const Offset(0.0, 0)
    ).animate(animationController);
    animationController.forward();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {

      },
      child: SlideTransition(
        position: animation,
        child: getBarrageWidget(),//can be any widget, not important.
      ),
    );
  }

Any help will be appreciated.

英文:

如何链接 SlideTransition

I use animation making my widget move.

when I click play button, I want the widget

  1. move from 1 to 2
  2. waiting for few seconds
  3. move from 2 to 3 and disappear

Now I just complete 1 and have no idea how to achieve 2 and 3

Here is my code in State.

  @override
  void initState() {
    animationController = AnimationController(duration: widget.duration, vsync: this);
    animation = Tween(
        begin: const Offset(1.0, 0),
        end: const Offset(0.0, 0)
    ).animate(animationController);
    animationController.forward();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {

      },
      child: SlideTransition(
        position: animation,
        child: getBarrageWidget(),//can be any widget, not important.
      ),
    );
  }

Any help will be appreciate.

答案1

得分: 0

在Flutter中没有提供预置的动画管理器,所以你需要自己管理动画。

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key});

  @override
  Widget build(BuildContext context) {
    return OrientationBuilder(
      builder: (context, orientation) {
        return const MaterialApp(
          title: 'Demo',
          debugShowCheckedModeBanner: false,
          home: MyHomePage(),
        );
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  late AnimationController _animationController;
  late AnimationController _animationController2;
  late AnimationController _animationController3;
  late Animation<Offset> _animation;
  late Animation<Offset> _animation2;
  late Animation<Offset> _animation3;
  late Animation<Offset> _animation4;
  late Animation<double> _fadeAnimation;

  bool isFirstAnimationCompleted = false;
  bool isSecondAnimationCompleted = false;

  bool isAnimating = false;
  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 3),
    );

    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 3),
    );

    _animationController.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        Future.delayed(const Duration(seconds: 3), () {
          _animationController2.forward();
        });
        isFirstAnimationCompleted = true;
        setState(() {});
      }
    });

    _animationController2 = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 1500),
    );

    _animationController2.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        isSecondAnimationCompleted = true;
        Future.delayed(const Duration(milliseconds: 500), () {
          setState(() {});
          _animationController3.forward();
        });
      }
    });

    _animation = Tween(begin: const Offset(-1.0, 0), end: const Offset(1.0, 0))
        .animate(_animationController);
    _animation2 = Tween(begin: const Offset(-2.0, 0), end: const Offset(0.0, 0))
        .animate(_animationController);

    _animation3 = Tween(begin: const Offset(0.0, 0), end: const Offset(1.0, 0))
        .animate(_animationController2);
    _animation4 = Tween(begin: const Offset(-1.0, 0), end: const Offset(0.0, 0))
        .animate(_animationController2);

    _animationController3 = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 500),
    );

    _fadeAnimation =
        Tween<double>(begin: 1.0, end: 0.0).animate(_animationController3);
  }

  @override
  void dispose() {
    _animationController.dispose();
    _animationController2.dispose();
    _animationController3.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final child = Stack(
      children: [
        SlideTransition(
          position: _animation,
          child: Container(
            color: Colors.green,
            width: double.infinity,
            height: 400,
            child: const Center(
              child: Text(
                'Hello World',
                style: TextStyle(fontSize: 42),
              ),
            ),
          ),
        ),
        SlideTransition(
          position: !isFirstAnimationCompleted ? _animation2 : _animation3,
          child: Container(
            color: Colors.orange,
            width: double.infinity,
            height: 400,
            child: const Center(
              child: Text(
                'Hello World Again',
                style: TextStyle(fontSize: 42),
              ),
            ),
          ),
        ),
        if (isFirstAnimationCompleted)
          SlideTransition(
            position: _animation4,
            child: Container(
              color: Colors.red,
              width: double.infinity,
              height: 400,
              child: const Center(
                child: Text(
                  'Hello World Third Time',
                  style: TextStyle(fontSize: 42),
                ),
              ),
            ),
          ),
      ],
    );
    return Scaffold(
      appBar: AppBar(),
      body: isFirstAnimationCompleted && isSecondAnimationCompleted
          ? FadeTransition(
              opacity: _fadeAnimation,
              child: child,
            )
          : child,
      floatingActionButton: FloatingActionButton(
        onPressed: _animationController.forward,
        child: const Icon(
          Icons.play_arrow,
        ),
      ),
    );
  }
}
英文:

There is no animator set in flutter so you will have to manage the animation yourself.

import &#39;package:flutter/material.dart&#39;;

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return OrientationBuilder(
      builder: (context, orientation) {
        return const MaterialApp(
          title: &#39;Demo&#39;,
          debugShowCheckedModeBanner: false,
          home: MyHomePage(),
        );
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State&lt;MyHomePage&gt; createState() =&gt; _MyHomePageState();
}

class _MyHomePageState extends State&lt;MyHomePage&gt; with TickerProviderStateMixin {
  late AnimationController _animationController;
  late AnimationController _animationController2;
  late AnimationController _animationController3;
  late Animation&lt;Offset&gt; _animation;
  late Animation&lt;Offset&gt; _animation2;
  late Animation&lt;Offset&gt; _animation3;
  late Animation&lt;Offset&gt; _animation4;
  late Animation&lt;double&gt; _fadeAnimation;

  bool isFirstAnimationCompleted = false;
  bool isSecondAnimationCompleted = false;

  bool isAnimating = false;
  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 3),
    );

    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 3),
    );

    _animationController.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        Future.delayed(const Duration(seconds: 3), () {
          _animationController2.forward();
        });
        isFirstAnimationCompleted = true;
        setState(() {});
      }
    });

    _animationController2 = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 1500),
    );

    _animationController2.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        isSecondAnimationCompleted = true;
        Future.delayed(const Duration(milliseconds: 500), () {
          setState(() {});
          _animationController3.forward();
        });
      }
    });

    _animation = Tween(begin: const Offset(-1.0, 0), end: const Offset(1.0, 0))
        .animate(_animationController);
    _animation2 = Tween(begin: const Offset(-2.0, 0), end: const Offset(0.0, 0))
        .animate(_animationController);

    _animation3 = Tween(begin: const Offset(0.0, 0), end: const Offset(1.0, 0))
        .animate(_animationController2);
    _animation4 = Tween(begin: const Offset(-1.0, 0), end: const Offset(0.0, 0))
        .animate(_animationController2);

    _animationController3 = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 500),
    );

    _fadeAnimation =
        Tween&lt;double&gt;(begin: 1.0, end: 0.0).animate(_animationController3);
  }

  @override
  void dispose() {
    _animationController.dispose();
    _animationController2.dispose();
    _animationController3.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final child = Stack(
      children: [
        SlideTransition(
          position: _animation,
          child: Container(
            color: Colors.green,
            width: double.infinity,
            height: 400,
            child: const Center(
              child: Text(
                &#39;Hello World&#39;,
                style: TextStyle(fontSize: 42),
              ),
            ),
          ),
        ),
        SlideTransition(
          position: !isFirstAnimationCompleted ? _animation2 : _animation3,
          child: Container(
            color: Colors.orange,
            width: double.infinity,
            height: 400,
            child: const Center(
              child: Text(
                &#39;Hello World Again&#39;,
                style: TextStyle(fontSize: 42),
              ),
            ),
          ),
        ),
        if (isFirstAnimationCompleted)
          SlideTransition(
            position: _animation4,
            child: Container(
              color: Colors.red,
              width: double.infinity,
              height: 400,
              child: const Center(
                child: Text(
                  &#39;Hello World Third Time&#39;,
                  style: TextStyle(fontSize: 42),
                ),
              ),
            ),
          ),
      ],
    );
    return Scaffold(
      appBar: AppBar(),
      body: isFirstAnimationCompleted &amp;&amp; isSecondAnimationCompleted
          ? FadeTransition(
              opacity: _fadeAnimation,
              child: child,
            )
          : child,
      floatingActionButton: FloatingActionButton(
        onPressed: _animationController.forward,
        child: const Icon(
          Icons.play_arrow,
        ),
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年3月1日 14:56:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定