• flutter中的动画


    1、AnimatedOpacity

    控制widget淡入或淡出效果  

      

    栗子是点击MaterialButton后,通过更新opacity变量,控制details的opacity;

    import 'package:flutter/material.dart';
    
    const owl_url =
        'https://raw.githubusercontent.com/flutter/website/master/src/images/owl.jpg';
    
    class FadeInDemo extends StatefulWidget {
      _FadeInDemoState createState() => _FadeInDemoState();
    }
    
    class _FadeInDemoState extends State<FadeInDemo> {
      double opacity = 0.0;
    
      @override
      Widget build(BuildContext context) {
        return Column(children: <Widget>[
          Image.network(owl_url),
          MaterialButton(
            child: Text(
              'Show details',
              style: TextStyle(color: Colors.blueAccent),
            ),
            onPressed: () => setState(() {
              opacity = opacity == 1 ? 0.0 : 1;
            }),
          ),
          AnimatedOpacity(
            opacity: opacity,
            duration: Duration(seconds: 2),
            child: Column(
              children: <Widget>[
                Text('Type: Owl'),
                Text('Age: 39'),
                Text('Employment: None'),
              ],
            ),
          )
        ]);
      }
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            body: Center(
              child: FadeInDemo(),
            ),
          ),
        );
      }
    }
    
    void main() {
      runApp(
        MyApp(),
      );
    }
    View Code

    2、AnimatedContainer

    控制widget的样式属性

    栗子是通过点击changebutton使AnimatedContainer widget随即获取新的样式

    import 'dart:math';
    import 'package:flutter/material.dart';
    
    double randomBorderRadius() {
      return Random().nextDouble() * 64;
    }
    
    double randomMargin() {
      return Random().nextDouble() * 64;
    }
    
    Color randomColor() {
      return Color(0xFFFFFFFF & Random().nextInt(0xFFFFFFFF));
    }
    
    const  _duration = Duration(milliseconds: 1000);
    
    class AnimatedContainerDemo extends StatefulWidget {
      _AnimatedContainerDemoState createState() => _AnimatedContainerDemoState();
    }
    
    class _AnimatedContainerDemoState extends State<AnimatedContainerDemo> {
      Color color;
      double borderRadius;
      double margin;
    
      @override
      initState() {
        super.initState();
        color = randomColor();
        borderRadius = randomBorderRadius();
        margin = randomMargin();
      }
    
      void change() {
        setState(() {
          color = randomColor();
          borderRadius = randomBorderRadius();
          margin = randomMargin();
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Column(
              children: <Widget>[
                SizedBox(
                   128,
                  height: 128,
                  child: AnimatedContainer(
                    margin: EdgeInsets.all(margin),
                    duration: _duration,
                    decoration: BoxDecoration(
                      color: color,
                      borderRadius: BorderRadius.circular(borderRadius),
                    ),
                  ),
                ),
                MaterialButton(
                  color: Theme.of(context).primaryColor,
                  child: Text(
                    'change',
                    style: TextStyle(color: Colors.white),
                  ),
                  onPressed: () => change(),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: AnimatedContainerDemo(),
        );
      }
    }
    
    void main() async {
      runApp(
        MyApp(),
      );
    }
    View Code

    3、FadeInImage

    一般用于网络图片未加载完的placeholder与加载后的图片替换动画

    栗子很简单,就是上面的描述

    FadeInImage.assetNetwork(
                  placeholder:'images/pic8.jpg',///本地图片
                  image:'http://pics.sc.chinaz.com/files/pic/pic9/201909/zzpic19910.jpg',
                )
    View Code

    3、FadeInImage

  • 相关阅读:
    网络编程之即时通信程序(聊天室)(一)通信流程简介及通信协议定制
    C#常用加密方法解析
    ASP.NET常用数据绑定控件优劣总结
    使用XPO开发时可以参考使用的架构
    渠道会上的体会
    如何利用第三方SDK开发MSN机器人以及实现语音视频?
    对 XPO 的一些问题的解答
    c++ 参数传递 之引用形参 GIS
    指针与引用的区别 GIS
    c++ const 修饰数组 GIS
  • 原文地址:https://www.cnblogs.com/webcabana/p/12133198.html
Copyright © 2020-2023  润新知