2019独角兽企业重金招聘Python工程师标准>>>

淡入淡出部件

作为UI开发人员,我们经常需要在屏幕上显示和隐藏元素。 但是,在屏幕上或屏幕外快速弹出元素会让最终用户感到不安。 相反,我们可以使用不透明动画淡入淡出元素,以创建流畅的体验。

在Flutter中,我们可以使用AnimatedOpacity部件来完成这项任务。

路线

  • 显示一个盒子以淡入淡出
  • 定义一个StatefulWidget
  • 显示切换可视性的按钮
  • 淡入淡出盒子

1.显示一个盒子以淡入淡出

首先,我们需要一些淡入淡出的东西! 在这个例子中,我们将在屏幕上绘制一个绿色框。

new Container(width: 200.0,height: 200.0,color: Colors.green,
);

2.定义一个StatefulWidget

现在我们有了一个用于设置动画的绿色框,我们需要一种方法来了解该框是否可见或不可见。 为了达到这个目的,我们可以使用一个StatefulWidget。

StatefulWidget是创建State对象的类。 State对象拥有关于我们应用程序的一些数据,并提供了更新数据的方法。 当我们更新数据时,我们也可以使用Flutter用这些更改重建我们的UI。

在我们的例子中,我们将有一块数据:一个布尔值,表示按钮是可见还是不可见。

为了构造一个StatefulWidget,我们需要创建两个类:一个StatefulWidget和一个相应的State类。 专业提示:Android Studio和VSCode的Flutter插件包含快速生成此代码的稳定片段!

// The StatefulWidget's job is to take in some data and create a State class.
// In this case, our Widget takes in a title, and creates a _MyHomePageState.
class MyHomePage extends StatefulWidget {final String title;MyHomePage({Key key, this.title}) : super(key: key);@override_MyHomePageState createState() => new _MyHomePageState();
}// The State class is responsible for two things: holding some data we can
// update and building the UI using that data.
class _MyHomePageState extends State<MyHomePage> {// Whether the green box should be visible or invisiblebool _visible = true;@overrideWidget build(BuildContext context) {// The green box will go here with some other Widgets!}
}

3.显示切换可视性的按钮

现在我们有一些数据来确定我们的绿色框是否应该是可见或不可见的,我们需要一种方式来更新这些数据。 在我们的情况下,如果该框可见,我们想隐藏它。 如果该框隐藏,我们想要显示它!

为了达到这个目的,我们会显示一个按钮。 当用户按下按钮时,我们会将布尔值从true更改为false,或将false更改为true。 我们需要使用setState进行更改,这是State类中的一个方法。 这将让Flutter知道它需要重建部件。

注意:有关处理用户输入的更多信息,请参阅食谱手册的处理手势部分。

new FloatingActionButton(onPressed: () {// Make sure we call setState! This will tell Flutter to rebuild the// UI with our changes!setState(() {_visible = !_visible;});},tooltip: 'Toggle Opacity',child: new Icon(Icons.flip),
);

4.淡入淡出盒子

我们在屏幕上有一个绿色的盒子。 我们有一个按钮来将可见性切换为true或false。 那么我们如何淡入淡出盒子? 随着AnimatedOpacity部件!

AnimatedOpacity部件需要三个参数:

  • opacity: 从0.0(不可见)到1.0(完全可见)的值。
  • duration: 动画完成需要多长时间
  • child: 动画作用的部件。 在我们的案例中,绿色框。
new AnimatedOpacity(// If the Widget should be visible, animate to 1.0 (fully visible). If// the Widget should be hidden, animate to 0.0 (invisible).opacity: _visible ? 1.0 : 0.0,duration: new Duration(milliseconds: 500),// The green box needs to be the child of the AnimatedOpacitychild: new Container(width: 200.0,height: 200.0,color: Colors.green,),
);

完整例子

import 'package:flutter/material.dart';void main() => runApp(new MyApp());class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {final appTitle = 'Opacity Demo';return new MaterialApp(title: appTitle,home: new MyHomePage(title: appTitle),);}
}// The StatefulWidget's job is to take in some data and create a State class.
// In this case, our Widget takes in a title, and creates a _MyHomePageState.
class MyHomePage extends StatefulWidget {final String title;MyHomePage({Key key, this.title}) : super(key: key);@override_MyHomePageState createState() => new _MyHomePageState();
}// The State class is responsible for two things: holding some data we can
// update and building the UI using that data.
class _MyHomePageState extends State<MyHomePage> {// Whether the green box should be visible or invisiblebool _visible = true;@overrideWidget build(BuildContext context) {return new Scaffold(appBar: new AppBar(title: new Text(widget.title),),body: new Center(child: new AnimatedOpacity(// If the Widget should be visible, animate to 1.0 (fully visible). If// the Widget should be hidden, animate to 0.0 (invisible).opacity: _visible ? 1.0 : 0.0,duration: new Duration(milliseconds: 500),// The green box needs to be the child of the AnimatedOpacitychild: new Container(width: 200.0,height: 200.0,color: Colors.green,),),),floatingActionButton: new FloatingActionButton(onPressed: () {// Make sure we call setState! This will tell Flutter to rebuild the// UI with our changes!setState(() {_visible = !_visible;});},tooltip: 'Toggle Opacity',child: new Icon(Icons.flip),), // This trailing comma makes auto-formatting nicer for build methods.);}
}

转载于:https://my.oschina.net/u/3647851/blog/1791911

Flutter 构建完整应用手册-动画相关推荐

  1. Flutter 构建一个完整的聊天应用程序

    在本教程中,我将向您展示如何使用 Flutter 构建一个完整的聊天应用程序.对于这一部分,我们将创建应用程序的 UI 原型,然后我将向您展示如何使用 firebase 创建后端服务并创建聊天系统. ...

  2. Unity提出ProtoRes模型:稀疏可变的输入也能构建完整人体姿态

    清华大数据软件团队官方微信公众号来源:机器之心 本文约2400字,建议阅读7分钟 稀疏约束通常会引起姿态参数的扰动,其背后的主要原因是缺乏归纳偏置来解决从一小组约束中恢复完整姿势的不适定问题. 人体姿 ...

  3. 开始使用 Flutter 构建 Windows 桌面应用吧!

    作者 / Chris Sells, Product Manager, Flutter developer experience 我们的使命是为开发者提供一个开源的高效框架,帮助他们在任何平台上构建美观 ...

  4. Flutter之自定义路由切换动画

    Flutter之自定义路由切换动画 在Flutter中,我们可以通过Navigator来实现路由管理,包括路由的跳转和返回等.默认情况下,Flutter提供了一些简单的路由切换动画,但是有时候我们需要 ...

  5. 【Flutter 实战】17篇动画系列文章带你走进自定义动画

    老孟导读:Flutter 动画系列文章分为三部分:基础原理和核心概念.系统动画组件.8篇自定义动画案例,共17篇. 动画核心概念 在开发App的过程中,自定义动画必不可少,Flutter 中想要自定义 ...

  6. Maya初学者完整的3D动画大师班视频教程

    Maya初学者完整的3D动画大师班视频教程 时间13小时30分 包括课程项目文件 1280X720 MP4 语言:英语+中文字幕(根据原英文字幕机译更准确)+原英文字幕 教程大小解压后:8.38G M ...

  7. ROS探索总结(十六)(十七)(十八)(十九)——HRMRP机器人的设计 构建完整的机器人应用系统 重读tf 如何配置机器人的导航功能

    ROS探索总结(十六)--HRMRP机器人的设计 1. HRMRP简介         HRMRP(Hybrid Real-time Mobile Robot Platform,混合实时移动机器人平台 ...

  8. 2022年为什么要使用Flutter构建应用程序?

    2022年为什么要使用Flutter构建应用程序 一级目录 二级目录 三级目录 什么是Flutter? 为什么跨平台如此重要? 单个代码库,单个技术栈. Flutter 擅长的地方 缩短上市时间 单个 ...

  9. 用Zebra 在Linux 上构建路由器实战手册

    用Zebra 在Linux 上构建路由器实战手册 一.Zebra 介绍 Zebra 是一个 TPC/IP 路由软件,支持 BGP-4.BGP-4+.OSPFv2. OSPFv3.RIPv1.RIPv2 ...

最新文章

  1. 马斯克:“星链”卫星已能提供服务
  2. 智能车竞赛技术报告 | 智能车视觉 - 武汉理工大学 - WHY
  3. mysql内置函数,在mysql里面利用str_to_date()把字符串转换为日期格式
  4. MySQL 类型转换
  5. python玩转android_如何用python玩跳一跳 ?(安卓版)
  6. sql server 加密_SQL Server始终被加密,以适合您的环境进行敏感数据加密
  7. 查询很慢会导致锁表吗_MySQL的insert into select 引发锁表
  8. myBatis-plus异常提示For input string: {0=null}
  9. (转)uml 交互视图
  10. 【Git教程】入门安装客户端与服务器
  11. Lucene PriorityQueue JDK PriorityQueue
  12. 配置 HTTP 代理(WIN10)、SOCKS 代理(WIN10)和编写 PAC 自动配置脚本
  13. .Net framework3.5装不上解决之道错误代码 0x800F0906、0x800F081F
  14. 广州优漫动游公司:高级UI/UE交互设计就业班学什么?
  15. Linux下Nginx的卸载、安装以及基本使用
  16. 「数据游戏」:使用 ARIMA 算法预测三日后招商银行收盘价
  17. 计算机组成原理研究生试题三,计算机组成原理研究生入学试题.doc
  18. nubia android root权限,中兴NX402 (Nubia Z5 Mini Android 4.2)ROOT教程,一键获取ROOT权限
  19. Python学习笔记(数据结构)
  20. c++中的点号(.),冒号(:)和双冒号(::)运算符

热门文章

  1. 面试官问:为什么SpringBoot的 jar 可以直接运行?
  2. CTO说:Service层的接口是不是多此一举
  3. 面试官:消息队列这些我必问!
  4. 如何设计 QQ、微信、微博、Github 等等,第三方账号登陆 ?(附表设计)
  5. 超详细的Guava RateLimiter限流原理解析
  6. 天池大赛 + CV语义分割 + 78万奖金:全国数字生态创新大赛来了!
  7. 沈向洋:读论文的三个层次
  8. 一文详尽系列之EM算法
  9. 一周内,在闲鱼上被疯狂转了 2 万次的 Linux 命令大全!!!
  10. 欠阿里云一分钱,会是什么样的后果...