Flutter Animation Code Sample(8)

一个AnimationController, 两个Animation,在一个页面中。分别控制AnimatedLogo和AnimatedText。


import 'package:flutter/material.dart';

void main(List<String> args) {

  runApp(

    const MaterialApp(

      home: HomePage(),

      debugShowCheckedModeBanner: false,

    ),

  );

}

class AnimatedLogo extends AnimatedWidget {

  const AnimatedLogo({super.key, required super.listenable});

  @override

  Widget build(BuildContext context) {

    Animation animation = super.listenable as Animation;

    return SizedBox(

      width: animation.value,

      height: animation.value,

      child: const FlutterLogo(),

    );

  }

}

class AnimatedText extends AnimatedWidget {

  const AnimatedText(

      {super.key, required this.text, required super.listenable});

  final String text;

  @override

  Widget build(BuildContext context) {

    Animation animation = super.listenable as Animation;

    return Text(

      text,

      style: TextStyle(fontSize: animation.value),

    );

  }

}

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 animationLogo;

  late Animation animationText;

  @override

  void initState() {

    controller = AnimationController(

      vsync: this,

      duration: const Duration(seconds: 3),

    );

    animationLogo = Tween<double>(begin: 0, end: 300).animate(controller);

    animationText = Tween<double>(begin: 0, end: 100).animate(controller);

    controller.repeat(reverse: true);

    super.initState();

  }

  @override

  void dispose() {

    controller.dispose();

    super.dispose();

  }

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      body: Center(

        child: Column(

          children: [

            AnimatedLogo(listenable: animationLogo),

            AnimatedText(text: "AnimatedText", listenable: animationText),

          ],

        ),

      ),

    );

  }

}

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

推荐阅读更多精彩内容