flutter 透明度动画_Flutter中的动画填充+不透明度动画✨

flutter 透明度动画

Flutter SDK provides us with many widgets which help us in animating elements on screen easily by implicitly managing the animations i.e. we need not worry about creating and managing intances of AnimationController during the lifecycle of the app. Two of those widgets which we’ll be seeing in this article are AnimatedPadding and AnimatedOpacity.

˚Flutter SDK为我们提供了许多小工具,帮助我们通过隐含管理动画动画很容易在屏幕上的元素,即我们不必担心创建和管理的intances AnimationController应用程序的生命周期中。 我们将在本文中看到的两个小部件是AnimatedPaddingAnimatedOpacity

In this article, I am going to firstly explain the basic usage of each widget and then eventually we’ll be combining both the widgets and come up with a cool animation like shown above.

在本文中,我将首先解释每个小部件的基本用法,然后最终我们将把这两个小部件组合在一起,并给出一个如上所示的酷动画。

动画填充 (Animated Padding)

Animated Padding, as the word suggests is a kind of padding widget which will animate the change in padding taking place at runtime.

Animated Padding,正如这个词所暗示的那样,是一种填充小部件,它将动画化在运行时发生的填充变化。

It is similar to the Padding widget except that it takes one extra required parameter which is Duration. The Duration attribute is used to specify the duration over which the animation should take place. The Duration could be seconds, milliseconds, minutes and also days if you are planning to create an animation which lasts days 😅.

它类似于Padding小部件,除了它需要一个额外的必需参数( DurationDuration属性用于指定动画发生的持续时间。 该Duration可以是secondsmillisecondsminutesdays ,如果你打算创造出持续数天😅的动画。

In this example we’ll be animating the padding around a Container with the help of AnimatedPadding when the Container is tapped itself.

在这个例子中,我们将动画周围填充Container的帮助下AnimatedPaddingContainer被窃听本身。

The widget tree is pretty simple. We have a Container widget which is wrapped with GestureDetector to detect the onTap event. It is then wrapped with AnimatedPadding because we want to animate the padding around the Container widget and the whole thing is centered with the help of Center widget.

小部件树非常简单。 我们有一个用GestureDetector包装的Container小部件,以检测onTap事件。 然后使用AnimatedPadding进行包装,因为我们要对Container小部件周围的填充进行动画处理,而整个内容将在Center小部件的帮助下Center

To the AnimatedPadding widget, we are passing a variable named _padding whose value we are updating inside setState().

AnimatedPadding小部件,我们传递了一个名为_padding的变量,该变量的值将在setState()中更新。

Image for post

Let’s pass a Curve to the curve attribute of AnimatedPadding. I personally like Curves.bounceOut.

让我们将Curve传递给AnimatedPaddingcurve属性。 我个人喜欢Curves.bounceOut

AnimatedPadding(
...
curve: Curves.bounceOut,
...
)
Image for post

动画不透明度 (Animated Opacity)

Animated Opacity, as the word suggests is a widget by means of which we can control the opacity of its child in an animated way, meaning the opacity will animate over a period of time depending upon the curve passed along with. The Duration field just like the previous widget is a required parameter here.

动画不透明度,顾名思义是一个小部件,通过它我们可以以动画方式控制其子级的不透明度,这意味着不透明度将在一段时间内进行动画处理,具体取决于传递的曲线。 就像上一个小部件一样,“ Duration字段是必填参数。

In this example, we will simply be toggling the opacity of a Text widget on the tap of RaisedButton.

在此示例中,我们只需在RaisedButton的水龙头上切换Text小部件的不透明度RaisedButton

Image for post

Note: The opacity passed should be a double between 0 and 1 but not less than zero or greater than 1.

注意:传递的不透明度应为0到1之间的两倍,但不小于0或大于1。

Below I am toggling opacity between 1 and some non-zero double value less than 1 (I don’t remeber exactly, it is 0.3 I think).

在下面,我在1和一些小于1的非零double值之间切换不透明度(我不记得,我认为是0.3)。

Image for post
Toggling opacity between 1 and some number greater than 0 but less than 1
在1和大于0但小于1的数字之间切换不透明度

结合动画填充和动画不透明度 (Combining both Animated Padding and Animated Opacity)

Now that we’ve seen a basic example of both the widgets, let us now build a proper example combining both of them.

现在我们已经看到了这两个小部件的基本示例,现在让我们构建一个将这两个小部件组合在一起的适当示例。

We’ll be displaying a list of widgets, each of which when tapped animates to show the user that it has been selected. As you might have guessed we will be playing around with the opacity and padding of the widget which is been tapped upon to animate and draw user’s attention towards it.

我们将显示一个小部件列表,点击它们时会显示每个小部件的动画,以向用户显示已被选择。 正如您可能已经猜到的那样,我们将使用小部件的不透明性和填充性,利用它们来制作动画并吸引用户对其的注意。

The widget inside the list is a simple Container with some height and color representing an item in the list. It can be any widget but in our case just to keep the article to the point, I am simply using a Container widget.

列表内的小部件是一个简单的Container具有一些高度和颜色,表示列表中的项目。 它可以是任何小部件,但在我们的情况下,只是为了使文章保持重点,我只是在使用Container小部件。

This is what the item in our list looks like. It is a Container which is wrapped with AnimatedOpacity to control the opacity of the Container and then wrapped with AnimatedPadding to control the spacing relative to the adjacent items in the list.

这就是我们列表中的内容。 它是一个用AnimatedOpacity包装的Container ,以控制Container的不透明度,然后用AnimatedPadding包装,以控制相对于列表中相邻项目的间距。

The variable isSelected is what we’ll be updating which will then cause the change in values for opacity and padding which will then render our animation.

变量isSelected是我们将要更新的变量,它将导致opacitypadding值的更改,然后将呈现动画。

First let’s just try this widget alone without putting it in the list along with other items to ensure that at least it is animating.

首先,让我们单独尝试此小部件,而不将其与其他项一起放入列表中,以确保至少它是动画的。

Image for post

As we’ll be having a list of items with each item having its own isSelected instance, lets create a model class for the same.

由于我们将拥有一个项目列表,每个项目都有自己的isSelected实例,因此让我们为它创建一个模型类。

class ItemModel {
bool isSelected;
ItemModel({this.isSelected = false});
}

We need to create a List of our ItemModel so as to keep track of selection status of each item in the list. So create a new list like below:

我们需要创建ItemModelList ,以便跟踪列表中每个项目的选择状态。 因此,如下创建一个新列表:

List<ItemModel> _items = [];

Let’s write a function to populate some items in the list we just created.

让我们编写一个函数,以填充刚创建的列表中的某些项目。

And call this function in initState() so that we have our items ready before the list is to be rendered.

并在initState()调用此函数,以便在呈现列表之前准备好项目。

@overridevoid initState() {
populateItems();
super.initState();
}

And of course the code for rendering the UI will change too as we are now going to display a list of items where each widget is of the type ListItem which we created earlier.

当然,呈现UI的代码也将发生变化,因为我们现在将显示项目列表,其中每个小部件都是我们先前创建的ListItem类型。

We’ll be using ListView.builder to render our list of items.

我们将使用ListView. builder ListView. builder以显示我们的项目列表。

After tying up all the pieces, the final code should look like below:

捆绑所有内容后,最终代码应如下所示:

Let’s run the code

让我们运行代码

Image for post

And that is it. We’ve created a simple yet elegant animation combining AnimatedPadding and AnimatedOpacity !

就是这样。 我们创建了一个简单而优雅的动画,结合了AnimatedPaddingAnimatedOpacity

Link to repository:

链接到存储库:

Thank you for reading my article. If you enjoyed reading it, please make sure to give some claps. Follow me for more articles on Flutter.

感谢您阅读我的文章。 如果您喜欢阅读,请确保鼓掌。 跟随我以获取有关Flutter的更多文章。

Below are my other profiles:

以下是我的其他个人资料:

  • Twitter

    推特

  • LinkedIn

    领英

  • Github

    Github

  • Instagram

    Instagram

翻译自: https://blog.prototypr.io/animated-padding-animated-opacity-in-flutter-fd3b710d058e

flutter 透明度动画

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274535.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

阿里 P10 是怎样的存在?

谈起中国顶尖的程序员&#xff0c;很多人首先会想到之前的雷军、张小龙&#xff0c;还有现在的多隆、行癫、道哥等人&#xff0c;但今天我想聊一聊的这位大神&#xff0c;他的技术成就也同样令人瞩目。19 年获得国家技术发明二等奖、20 年获得国家计算机协会颁发的“ CCF 杰出工…

vba交付图表设计_您是在为交付目的而“设计”吗?

vba交付图表设计重点 (Top highlight)It’s a regular Monday morning. All the design team is organizing the tasks for the ongoing week and reviewing requirements and deadlines for the various projects at the studio or company you work at. Suddenly, among the …

前端必读书籍推荐

大家好&#xff0c;我是若川。持续组织了近一年的源码共读活动&#xff0c;感兴趣的可以 点此扫码加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试系…

window程序设计学会_是时候我们学会设计合适的饼图了

window程序设计学会Pie charts are common in data science — next to the 饼形图在数据科学中很常见- bar chart and the line plot, the pie chart is incredibly standard and simple. A circle is split into several slices, with each slice’s angle representing how…

「非广告」程序员如何才能尽量避免被裁?

大家好&#xff0c;我是若川。持续组织了近一年的源码共读活动&#xff0c;感兴趣的可以 点此扫码加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试系…

使用css制作三角,兼容IE6,用到的标签divsspan

使用css来制作三角&#xff0c;在日常用得较多。恰好这几天项目中有用到&#xff0c;之前只是从网上copy下来代码直接用&#xff0c;但是今天在用的时候遇到一些问题&#xff0c;于是借此机会把这个css绘制三角好好研究下吧。 我们分别写一个<div>,<s>,<span>…

培训师 每小时多少钱_每个产品设计师需要了解的品牌知识

培训师 每小时多少钱重点 (Top highlight)These days, it pays to know about brand. The rise of startups has created thousands of new brand design opportunities, and people of all disciplines are working to help brands compete in a crowded world. Increasingly,…

Android 绑定远程服务出现 Not Allowed to bind service

E/AndroidRuntime(3783): Caused by: java.lang.SecurityException: Not allowed to bind to service Intent { actcom.lenovo.pleiades.conntek.pad } 主要原因是服务中设有签名保护&#xff0c;该服务上一次是通过A设备中的Eclipse签名的&#xff0c;这一次是通过B设备中的Ec…

axios 发布 v1.1.0 据说导致很多网站瘫痪~那么如何自动提升版本号呢~

- 大家好&#xff0c;我是若川。友情提醒&#xff0c;今天还是周二。就不发长篇技术文了~近日&#xff0c;axios 发布了 v1.1.0 版本&#xff0c;调用 axios.get 时报错&#xff0c;据说导致请求无效很多网站瘫痪。目前官方已发布了 v1.1.1 v1.1.2 修复了该问题。让我想起群友在…

七月时忙碌而充实的_如何减少忙碌而更有效

七月时忙碌而充实的In our hectic modern world, we believe that rushing from one task to the next and managing multiple priorities shows everyone that we are productive.在忙碌的现代世界中&#xff0c;我们相信从一项任务过渡到下一项任务并处理多项优先事项可以向所…

github上阅读源码很费劲?你可能不知道这几个神器

大家好&#xff0c;我是若川。今天周四&#xff0c;再熬一天就可以放假了。有时候在github浏览到合适的仓库时&#xff0c;总想着打开看看源码&#xff0c;但又不想克隆。推荐几个在线编辑器打开github仓库的网站~最后欢迎大家在文末投票&#xff0c;看看大概有多少人知道或者不…

Codeforces Round #149 (Div. 2)【AK】

吐槽&#xff1a;比赛刚开始codeblocks出了点问题。。边看题边弄编译器。。。囧。。 D居然一直没看。。因为E题意好懂。。然后sb地卡了一场E。。。战斗力太不稳定。。。 A... A 1 #include<cstdio>2 #include<cstring>3 #include<iostream>4 #define N 10001…

ui设计卡片阴影_UI设计形状和对象基础知识:阴影和模糊

ui设计卡片阴影第三部分 (Part 3) Welcome to the third part of the UI Design super-basics. This time we’ll cover two of the most commonly used effects — shadows and blurs.欢迎使用UI设计超级基础的第三部分。 这次我们将介绍两种最常用的效果- 阴影和模糊 。 Und…

干货 | 带你玩转前端性能优化!【留言送书】

大家好&#xff0c;我是若川。之前送过三次Vue的书&#xff0c;现在又和博文视点合作再次争取了几本书&#xff0c;具体送书规则看文末。很多人可能有耐心花费一两个小时在一家火锅店门口排队&#xff0c;但几乎没有人愿意等30s去加载一个短视频。事实上&#xff0c;对于大多数…

如何进入游戏行业_进入设计行业

如何进入游戏行业We’re living in some weird-ass times. One of the unfortunate results of a global pandemic is loss of jobs and financial security. While people continue to deal with this, the prospect of entering a new field — especially one that’s sligh…

据说99%的人不知道 vue-devtools 还能直接打开对应组件文件?

大家好&#xff0c;我是若川。据说 99% 的人不知道 vue-devtools 还能直接打开对应组件文件&#xff1f;本文原理揭秘曾经写过以上这篇文章&#xff0c; 也是源码共读中的第一期(点击文末阅读原文直达)。这个功能如下图所示。欢迎大家来投票&#xff0c;你的投票很重要。如果不…

ux设计中的各种地图_UX设计中的格式塔原理

ux设计中的各种地图Gestalt Theory is the theory of visual perception and how our brain pieces together reality. The theory sheds light on how cognition factors into the way viewers read a piece of design. In the German language “Gestalt” means form or sha…

JetBrains下一代IDE:Fleet 公共预览版发布

大家好&#xff0c;我是若川。持续组织了近一年的源码共读活动&#xff0c;感兴趣的可以 点此扫码加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试系…

善用工具_如何善用色彩心理学

善用工具There’s a problem with my movement. Most of us in the profession of trying to change the world have little skills or training in the actual craft of influencing human beings to do stuff — especially stuff that is new to them such as composting, p…

看源码的第一步,我猜很多人搞错了~

大家好&#xff0c;我是若川。今天在江西人的前端群里&#xff0c;有个小伙伴问&#xff0c;vueuse 的 vitepress &#xff08;也就是官方文档仓库&#xff09;怎么搭建的&#xff0c;怎么都没有 index.json&#xff08;引用的一个文件&#xff09;。本文简单记录下流程&#x…