Create a movable stack widget function using flutter

Create a movable stack widget function using flutter

This post focuses on a super secret Flutter project I designed for desktop and web that uses a canvas and draggable node interface. This tutorial will show how I use stacks to accomplish draggable functionality with widgets.

As shown below.

Draggable widget example shake

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

random_color:

We can then create the HomeView that contains our stack

class HomeView extends StatefulWidget {
  @override
  _HomeViewState createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  List<Widget> movableItems = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
          children: movableItems,
        ));
  }
}

The functionality is very simple. We will have a MoveableStackItem stateful widget. It keeps track of its own position and color. The color is set on initialization and the position is updated via GestureDetector .

class _MoveableStackItemState extends State<MoveableStackItem> {
  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 MoveableStackItem view. We will do this via a floating action button in HomeView.

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

That's it. Now you have a movable Stack on your view.

This is the end of this article about creating a movable stack widget in flutter. For more related flutter stack widget content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Over 1 million StackOverflow Flutter 20 questions (recommended)

<<:  In-depth explanation of Mysql deadlock viewing and deadlock removal

>>:  Detailed explanation of mysql deadlock checking and deadlock removal examples

Recommend

JavaScript counts the number of times a character appears

This article example shares the specific code of ...

SQL function to merge a field together

Recently, I need to query all the fields in a rel...

Vue virtual Dom to real Dom conversion

There is another tree structure Javascript object...

4 ways to modify MySQL root password (summary)

Method 1: Use the SET PASSWORD command First log ...

Nginx dynamically forwards to upstream according to the path in the URL

In Nginx, there are some advanced scenarios where...

How to use the Linux nl command

1. Command Introduction nl (Number of Lines) adds...

Summary of solutions to common Linux problems

1. Connect Centos7 under VMware and set a fixed I...

Web Design Tips: Simple Rules for Page Layout

Repetition: Repeat certain page design styles thr...

JavaScript canvas to achieve scratch lottery example

This article shares the specific code of JavaScri...

MySQL Optimization: InnoDB Optimization

Study plans are easily interrupted and difficult ...

How to get the current time using time(NULL) function and localtime() in Linux

time(); function Function prototype: time_t time(...

js to achieve simple drag effect

This article shares the specific code of js to ac...