Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use flutter to create removable stack widget functionality

Shulou Source: shulou.com Published: 2022-06-03 07:19:36 09月24日 Update

This article shows you how to use flutter to create mobile stack widgets, the content is very detailed, interested friends can refer to, hope to help you.

This article mainly introduces a super secret Flutter project I designed for desktops and Web using canvas and draggable node interfaces.

As shown below.

We will dynamically add items to the stack and distinguish them, and I will use the RandomColor typer. So we have to add that bag.

Random_color:

Then we can create a HomeView that contains our stack

Class HomeView extends StatefulWidget {@ override _ HomeViewState createState () = > _ HomeViewState ();} class _ HomeViewState extends State {List movableItems = []; @ override Widget build (BuildContext context) {return Scaffold (body: Stack (children: movableItems,));}}

The function is very simple. We will have a MoveableStackItem stateful widget. It tracks its location and color. The color is set during initialization, and the location is updated by updating the GestureDetector.

Class _ MoveableStackItemState extends State {double xPosition = 0; double yPosition = 0; Color color; @ override void initState () {color = RandomColor (). RandomColor (); super.initState ();} @ override Widget build (BuildContext context) {return Positioned (top: yPosition, left: xPosition, child: GestureDetector (onPanUpdate: (tapInfo) {setState (() {xPosition + = tapInfo.delta.dx) YPosition + = tapInfo.delta.dy;});}, child: Container (width: 150, height: 150, color: color,),);}}

The last thing to do is to add a new one to the MoveableStackItem view. We will do this through the floating operation button in HomeView.

Return Scaffold (floatingActionButton: FloatingActionButton (onPressed:) {setState (() {movableItems.add (MoveableStackItem ());});},), body: Stack (children: movableItems,))

okay. Now you have a removable Stack on your view.

So much for sharing on how to use flutter to create mobile stack widgets. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Tags: Features widgets movement location content stack more views items colors help good interest dynamic guys buddies that is buttons articles desktops Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS Huawei Shulou Tech Info OPPO Reno