Building Custom Scroll Physics and Simulations with Dart's Flutter Framework

in #utopian-io6 years ago (edited)

flutter-logo.jpg

What Will I Learn?

Write here briefly the details of what the user is going to learn in a bullet list.

  • You will learn how to define and create Custom Physics Simulations in Flutter
  • You will learn how to apply these Physics Simulations to Scrolling Widgets
  • You will learn how to use the Layout Builder Widget
  • You will learn about the Render Life Cycle for Flutter
  • You will learn how to build an Infinite List View widget using Extents
  • You will learn about Flutter Constraints and how they work in the framework

Requirements

System Requirements:
OS Support for Flutter:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • A basic knowledge of Newtonian physics
  • A fair understanding of Mobile development and Imperative or Object Oriented Programming
  • Some understanding of rendering and rendering life cycles for other frameworks like React

Resources for Flutter and this Project:

Sources:

Flutter Logo (Google): https://flutter.io/

Difficulty

  • Intermediate

Description

In this Flutter Video Tutorial, we create a custom scroll animation for an infinite list view widget to demonstrate various core Flutter concepts. These concepts include, Physics Simulations, Constraints, Layout Builder Widgets, The Flutter Render Life Cycle, and Custom Scroll Physics Objects. This example works to help the user gain a basic grasp of these concepts so that they can better understand how the framework works overall.

Outline and Overview

Flutter is a Beta technology that was inspired by various other component based frameworks. While many of the concepts in Flutter are similar to these other frameworks, some of them are new and foreign. The render life cycle for instance is very different from the render cycle of component based web frameworks. This idea of Constraints and the layout builder widget is also fairly different when compared to what many UX developers are used to.

Outline for this Tutorial

Item 1: Rendering Widgets

Rendering is a very important part of any UX framework. In Flutter, there is a streamlined process that the rendering pipeline follows when the widgets need to be rebuilt. The primary Isolate sends a message to the Skia engine which then asks the native platform for the next vsync event. Once that vsync event happens, the Dart Code is executed and various steps are then preformed in sequence. The most relevant part of this process for our tutorial is the layout stage which happens after the widgets receive paint.

repaint.jpg

In this image you can see our infinite list view application and with the debug paint overlay active. This overlay gives us a little insight into how a flutter application is rendered. The big green arrows signify that the painting happens from top to bottom and the small green arrows on the sides of the screens show the max width for each of the constraints attached to the widgets. The larger green arrows pointing towards the text show the constants of the text widget that contains the index numbers.

Item 2: Contraints

Widgets in Flutter are rendered using the RenderBox Objects as an underlying basic element. These render boxes are given constraints by their parent widgets and they use those constraints as a guideline to how they should be rendered. Constraints are not specific sizes but rather a maximum and minimum width and height that the widget is allowed to exist in.

constraints.jpg

You can see in the builder callback function for the layout builder widget that the Box Constraint object is being passed in by its parent which is the material application widget. We further use these constraints in the Constrained Box widget that surrounds our list view. We specify the max width and max height for the children widget using these constraints. In this example, our Max Height can be infinite.

Item 3: Building an Infinite List View

Using this concept of Constraints to our advantage we are able to create an Infinite list view widget. To accomplish this, we ignore the item count property of the List View builder widget and instead fill in the item extent property. With the max width constraint fixed to a size of 400.0 pixels, we can now use this item extent property to change the size of the height for each of the items in our list view.

list_view.jpg

We specify our item extent and it indirectly changes the height of each of the List View widgets. The Item Extent itself lets us control the extent or area of each of these widgets. This, together with the set Max Width Constraints, will properly size the List View item's heights in the list view viewport.

Item 4: Adding Custom Scroll Physics

We are able to modify the scroll physics of our list view so that it will be friction-less by extending the Simulation and Scroll Physics Objects. The Simulation class allows us to modify the position and velocity of the list view. It lets us model the physics of the widget compared to time and a single dimension. We can then make use the create ballistic simulation method in the Scroll Physics class to specifically attach this simulation to the scrolling movement of the list view.

normal-spin.gif

This animated GIF shows the normal scroll physics of a scroll-able list view in flutter. These physics act a bit like a ballistic pendulum with the user's finger acting as the ballistic object and the scrolling following the motion of the pendulum. Once the motion for the scrolling list view stops, however, then the movement comes to a complete halt.

fast_flick.gif

In this animated GIF we can see the implemented scroll physics of our list view. Once we the scroll-able hits its max velocity, it will continue at that velocity indefinitely unless the user takes action to stop it. We can also set the velocity to be a specific number which will cause it to run until the user puts their finger down on the list view. This is all possible because we have an infinite list view.

Project GitHub Repository:

The Source Code for this project can be found here

Video Tutorial

Projects and Series

Stand Alone Projects:
Building a Calculator
Movie Searcher Application

Minesweeper Game

Weather Application

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hey @tensor

We're already looking forward to your next contribution!

Decentralised Rewards

Share your expertise and knowledge by rating contributions made by others on Utopian.io to help us reward the best contributions together.

Utopian Witness!

Vote for Utopian Witness! We are made of developers, system administrators, entrepreneurs, artists, content creators, thinkers. We embrace every nationality, mindset and belief.

Want to chat? Join us on Discord https://discord.me/utopian-io

Thank you for your contribution @tensor. It has been reviewed and scored.


Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.

[utopian-moderator]

That was quick. Thank you as always for moderating my content. If you have any feedback feel free to leave it.

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 61599.30
ETH 3389.34
USDT 1.00
SBD 2.50