首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在颤动中将小部件滚动出屏幕

在颤动中将小部件滚动出屏幕
EN

Stack Overflow用户
提问于 2020-12-22 21:19:33
回答 2查看 158关注 0票数 1

我想把这个容器滚出屏幕,这样它就会向上运动,并在2-3秒内消失。我正在使用这个页面作为闪屏。align小部件包装的容器代码如下:

代码语言:javascript
复制
Scaffold(
   // backgroundColor: Colors.black,
    body: Align(
      alignment: Alignment.topCenter,
      child: Transform.scale(
        scale: 1.5,
        child: Container(
          height: 400,
          decoration: BoxDecoration(
              color: Colors.deepOrange, borderRadius: BorderRadius.circular(200)),
        ),
      ),
    ),
  );
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-22 22:03:43

可以使用SlideTransition小部件为容器的位置设置动画:

代码语言:javascript
复制
Animation<Offset> animation;
AnimationController animationController;

@override
void initState() {
  super.initState();
  animationController = AnimationController(
    vsync: this,
    duration: Duration(seconds: 1),
  );

  animation = Tween<Offset>(begin: Offset.zero, end: Offset(0, -1.5)) //negative to go upwards
      .animate(animationController);
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Stack(
      children: [
        Align(
          alignment: Alignment.topCenter,
          child: SlideTransition(
            position: animation,
            child: Transform.scale(
              scale: 1.5,
              child: Container(
                height: 400,
                decoration: BoxDecoration(
                  color: Colors.deepOrange,
                  borderRadius: BorderRadius.circular(200),
                ),
              ),
            ),
          ),
        ),
        Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: RaisedButton(
            child: Text('Animate'),
            onPressed: () {        //the button that trigger the animation
              if (animation.status == AnimationStatus.completed) {
                animationController.reverse();
              } else if (animation.status == AnimationStatus.dismissed) {
                animationController.forward();
              }
            },
          ),
        ),
      ],
    ),
  );
}

在示例中,我显示了一个触发动画的按钮,但您可以在initState中执行此操作。

并且您需要为一个动画添加SingleTickerProviderStateMixin混合,或者为两个或更多动画添加TickerProviderStateMixin

代码语言:javascript
复制
class _YourScreenState extends State<YourScreen> with TickerProviderStateMixin {
票数 2
EN

Stack Overflow用户

发布于 2021-07-02 07:22:00

您可以使用流行的Spring package很好地做到这一点。

示例:

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'package:spring/spring.dart';

class SpringExample extends StatelessWidget {
  
  // For the rotation animation to loop.
  final SpringController springController =
      SpringController(initialAnim: Motion.loop);

  @override
  Widget build(BuildContext context) {
       return Spring.scale(
           start: 1,
           end: 1.5,
           animDuration: const Duration(seconds: 3),
           child: Spring.slide(
             slideType: SlideType.slide_out_top,
             animDuration: const Duration(seconds: 3),
             child: Spring.rotate(
               springController: springController,
               child: Container(
                 height: 400,
                 decoration: BoxDecoration(
                     color: Colors.deepOrange,
                     borderRadius: BorderRadius.circular(200)),
               ),
             ),
           ));
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65409453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档