Flutter Animation Code Sample (9)

使用AnimatedBuilder,并且使用多个GrowTransition

import 'package:flutter/material.dart';

void main(List<String> args) {

  runApp(

    const MaterialApp(

      home: HomePage(),

      debugShowCheckedModeBanner: false,

    ),

  );

}

class GrowTransition extends StatelessWidget {

  const GrowTransition(

      {super.key, required this.widget, required this.animation});

  final Widget widget;

  final Animation animation;

  @override

  Widget build(BuildContext context) {

    return AnimatedBuilder(

      animation: animation,

      builder: (context, child) {

        return SizedBox(

          width: animation.value,

          height: animation.value,

          child: widget,

        );

      },

    );

  }

}

class HomePage extends StatefulWidget {

  const HomePage({super.key});

  @override

  State<HomePage> createState() => _HomePageState();

}

class _HomePageState extends State<HomePage>

    with SingleTickerProviderStateMixin {

  late AnimationController controller;

  late Animation animation;

  @override

  void initState() {

    controller = AnimationController(

      vsync: this,

      duration: const Duration(seconds: 3),

    );

    animation = Tween<double>(begin: 0, end: 300)

        .chain(CurveTween(curve: Curves.fastOutSlowIn))

        .animate(controller);

    controller.repeat(reverse: true);

    super.initState();

  }

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      body: Center(

        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,

          children: [

            GrowTransition(

              widget: const FlutterLogo(),

              animation: animation,

            ),

            GrowTransition(

                widget: const FittedBox(child: Text("ABC")),

                animation: animation),

          ],

        ),

      ),

    );

  }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容