Home > Net >  Flutter move a container at constant speed
Flutter move a container at constant speed

Time:04-13

I need to move container at constant speed, my code moves it to the required location but not at constant speed it moves at different speeds each time.

double xVal = currentXLocationOfTheContainer;
double yVal = currentYLocationOfTheContainer;

static double xDistance = requiedXLocationOfTheContainer - currentXLocationOfTheContainer;
static double yDistance = requiedXLocationOfTheContainer - currentYLocationOfTheContainer; 

Timer.periodic(const Duration(milliseconds: 10), (timer) {
  setState(() {
    xVal = xVal   xDistance/100;
    yVal = yVal   yDistance/100;

  });
}); 

CodePudding user response:

the formula is V = Distance/Duration so for constant speed we need constant Distance in constant time.

for Example 2000 pixel per second Or 2 pixel per Millisecond

Fist way : const distance.


double xVal = currentXLocationOfTheContainer;
double yVal = currentYLocationOfTheContainer;

static double xDistance = requiedXLocationOfTheContainer - currentXLocationOfTheContainer;
static double yDistance = requiedXLocationOfTheContainer - currentYLocationOfTheContainer; 

Timer.periodic(const Duration(milliseconds: 10), (timer) {
  setState(() {
    xVal = xVal   (xDistance > 0 ? 20 : -20);
    yVal = yVal   (yDistance > 0 ? 20 : -20);

  });
});

note:speed in above code is 2 * sqrt(2) = 2.8284 pixel per millisecond


second way : const time.

for real Distance we use https://en.wikipedia.org/wiki/Pythagorean_theorem

import 'dart:math';

double xVal = currentXLocationOfTheContainer;
double yVal = currentYLocationOfTheContainer;

static double xDistance = requiedXLocationOfTheContainer - currentXLocationOfTheContainer;
static double yDistance = requiedXLocationOfTheContainer - currentYLocationOfTheContainer; 

var realDisatanse = sqrt((xDistance*xDistance)   (yDistance*yDistance));
var speedPerMillisecond = 2;
var time = realDisatanse / speedPerMillisecond; 

Timer.periodic(const Duration(milliseconds: time), (timer) {
  setState(() {
    xVal = xVal   xDistance;
    yVal = yVal   yDistance;

  });
});

note:speed in above code is 2 pixel per millisecond

CodePudding user response:

You can use AnimatedPositioned widget which has default curve = Curves.linear

Animation as per documentation: https://flutter.github.io/assets-for-api-docs/assets/animation/curve_linear.mp4

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final targetX = 200;
  final targetY = 200;

  int currentX = 0;
  int currentY = 0;

  @override
  void initState() {
    super.initState();
    Future.delayed(const Duration(seconds: 3), () {
      if (mounted) {
        setState(() {
          currentX = targetX;
          currentY = targetY;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('AnimatedPositionedExample')),
      body: Stack(
        children: [
          AnimatedPositioned(
            child: Container(color: Colors.lime, height: 50, width: 50),
            left: currentX.toDouble(),
            top: currentY.toDouble(),
            duration: const Duration(seconds: 1),
            curve: Curves.linear,
          )
        ],
      ),
    );
  }
}
  • Related