Flutter Animation Tutorial #9 - Animated Lists

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago

Key Takeaways

Creates animated lists in Flutter

Full Transcript

all right then gang so in this video i want to start to animate these items onto the page inside the list because currently they just pop onto the screen whenever the app first starts instead i want to slide them on from right to left and maybe have some kind of stagger effect so that the first one comes on first then the next then the next then the next with 100 or 200 milliseconds between each one so there's several different ways we can do this and i'm going to show you in this video how to use animated list widgets so right here you can see currently we're creating this list using a list view builder now to animate these tiles onto the page onto the screen instead we're going to replace this with an animated list widget so let me first do that animated list and now inside this we need to update some of these different properties before we do that let me spell this correctly animated list and this property right here this item count needs to be instead initial item count because at some point there might be different amounts of items right it's not always going to be the same amount of items we might take items out add items in etc so this is going to be initial item count so when the app first loads how many items are going to be there now we also need to update this item builder right here to have a third argument and that is going to be an animation argument which we can access then inside this thing over here so now we've done that we need to also update a couple of things at the top so if we scroll right to the top currently we have this global key right here now this is for a normal list we need to turn this into a key for an animated list so let me get rid of this and replace it with this so now we're still using a global key it's called the same thing but this time we have this type associated with it it's an animated list state so it's a special type of global key for animated lists so now we have that set up we can work on the animation of our list items so i'm going to scroll back down to our widget tree down here where we return currently the trip tiles now instead i want to wrap this inside a transition widget in particular a slide transition widget and this is going to allow us to slide on the individual list items when we're building them right here so let me for now get rid of that and instead i'm going to return a slide transition widget okay so inside here we need a couple of different properties first of all we need a child property and this is actually going to be the thing that we're trying to transition on now that was the thing we just deleted which is from the trip tiles and then index so remember we get access to index as we cycle through the trip tiles and if we recall the trip tiles is the list of widgets right here starts completely empty but then inside init state we add the trips we cycle through this list of trip objects and we add a new widget to this list for each trip object by passing it into build tile this returns a list tile so all we're doing is outputting a sequence of tiles right here but we're wrapping them in a slide transition so that for each one ultimately we're going to slide it onto the page okay now we also need another property and that property is going to be the position property so when we're using a slide transition we're basically transitioning the position of something on a page and to do that we use a special type of tween and this tween is gonna be an offset tween so what i'm gonna do is just paste this up here i'm externalizing this tween so we don't have to write it out all here inside the position and all we're doing is creating a tween which is of type offset i'm calling it underscore offset i'm setting it equal to a tween all i do is specify that the beginning is this offset and the end is this offset right here so an offset basically determines the offset of a widget in both the x and y directions now a value of zero in any given direction means that it won't have an offset at all and a value of one in a given direction means it will be offset by either the widget's full width or height depending on the direction of the offset so if we give our list items and offsets in the x direction of one it means it's going to be completely off the page over here by the width of the list item itself which is the full width of the screen so it's going to start way over here to the right of the screen right in the y direction there's no offset so it stays where it is vertically and that's the beginning position right off the screen now the end position what it's going to twin two is zero zero so it's rightful position right here zero offset so we're creating this tween so that we can transition from over here off the screen to on the screen and now we're gonna use that down here inside the position property so right here we can use this animation property that we get and from that i'm going to say dot drive and we're going to drive an animation using this offset right here that's all we're doing we're taking an animation and we're driving it using this offset and that basically controls this slide transition for us so now this child is going to be transitioned onto the screen now currently this won't work and i can demonstrate that if i refresh then it's not going to work over here right they don't animate on and that's because we first have to tell flutter every time that we add a new tile over here we also need to update the current state of the global key to say we've added a new item and that way flutter can keep track of the items added to the list and animate them so let me do this after we've added a new widget to the trip tiles let me also update this thing right here the global key current state so we do that by taking the list key that we have this thing right here and getting the current state and then using the method insert item and in here is the index of the item that we've inserted so to begin with if we add a new item to this list right here it's going to be at position zero right so we take the length of the trip tiles which will be one because we've added one item and we minus one to get that index of zero and we do that for each one so if we've added two and it's the second widget the length is two minus one which is one and that is the index of the second item we've added to the list of widgets right so now we're telling the current state in certain item and this allows flutter to understand from this key right here the global key when to animate in a new item so we've done that but again this still won't work so if i save it and refresh over here you're still going to notice that this doesn't work and we actually get an error and it says the method insert item was called on null and that's because we're trying to access the current state before the build method even runs and registers this thing right here with the list so to combat this we should only call the function after the build method has run now to do this in flutter we can use a special method so what i'm going to do is copy something and just paste it up here then i'll explain it so inside init states i'm going to paste in this thing right here so widgetsbinding.instance.add post frame callback and this takes in a function and basically all we're doing is saying okay we want to fire this function right here only when this build method has run right here so we're waiting until that's run and then inside that we're firing at trips so we can remove it from here so we know now when we're firing this thing right here it's only gonna fire after the build method has run so at that point we know that we have a current state so this is a nice little thing we can use to make sure that the build method has run on a widget before we do something so you can copy this to your cheat sheet for flutter if you want widgetsbinding.instance.ad post frame callback takes in a function which will fire after the build method has run so now if i save this fingers crossed this should all work and we see them all slide onto the page so if i refresh or restart we can see that again they all slide onto the page which is quite nice so that's cool but they do all animate in together now i'd like to stagger them a little bit so in the next video i'm going to show you a trick we can use to do that

Original Description

Hey gang, in this Flutter animations tutorial we'lltake a look at how to create animations for our list items. 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: + https://github.com/iamshaunjp/flutter-animations 🐱‍💻 🐱‍💻 Other Related Free Courses & Links: + Flutter Tutorial for Beginners - https://www.youtube.com/watch?v=1ukSR1GRtMU&list=PL4cUxeGkcC9jLYyp2Aoh6hcWuxFDX6PBJ + Flutter & Firebase Tutorial - https://www.youtube.com/watch?v=sfA3NWDBPZ4&list=PL4cUxeGkcC9j--TKIdkb3ISfRbJeJYQwC + Get VS Code - https://code.visualstudio.com/ + Flutter Animation docs - https://flutter.dev/docs/development/ui/animations/overview 🐱‍💻 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

📰
Web Development training and Placement in Electronic City — Full Stack (HTML, CSS, JS, React, Node…
Learn full-stack web development with HTML, CSS, JS, React, and Node.js and get placement assistance in Electronic City
Medium · JavaScript
📰
Document Object Model [DOM] CRUD Operations
Learn to perform CRUD operations on the Document Object Model (DOM) to dynamically manipulate web page content
Dev.to · Madhan Raj
📰
I Found a Surprisingly Fun Way to Practice Frontend Development
Practice frontend development in a fun way by building football-themed applications on VibeCode Arena
Dev.to AI
📰
The Enter key that submits your form while a Japanese user is still typing
Learn how to prevent the Enter key from submitting a form while a Japanese user is still typing, and why it matters for user experience
Dev.to · greymoth
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →