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.pswp.cn/news/274535.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274535.shtml
英文地址,請注明出處:http://en.pswp.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;下面我們用正則表達式限制文本框只能輸入數字、小數點、英文字母、漢字等各類代碼。 1.文本框只能輸入數字代碼(小數點也不能輸入) <input οnkeyup"this.valuethis.value.replace(/\D/g,)" …

前端必讀書籍推薦

大家好&#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;對于大多數…

css3學習手記

完全匹配屬性選擇器&#xff1a;[idvalue] 包含匹配選擇器&#xff1a;[id*value] 首字符匹配選擇器&#xff1a;[id^value] 尾字符匹配選擇器&#xff1a;[id$value] 陰影&#xff1a;box-shadow:3px 2px 1px #000 /*其他瀏覽器*/ -webkit-box-shadow:3px 2px 1px #…

如何進入游戲行業_進入設計行業

如何進入游戲行業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…

【ASP】簡單Url編碼和Url解碼實例

描述&#xff1a;在數據傳輸或讀取時&#xff0c;有些敏感數據不宜顯示傳輸&#xff0c;特別涉及機密或財務信息&#xff0c;因此使用編碼技術尤為重要。 簡單實例&#xff1a; URL編碼代碼&#xff1a; protected void Button1_Click(object sender, EventArgs e) { …