[SOLVED] ListView in flutter with shuffle animation


This Content is from Stack Overflow. Question asked by Mosh Mobile Software Engineer

So in my flutter screen I have these tiles that is each tile has a letter in it. My plan there that these tiles must be expected to be shuffled when the button is click. Currently, I just have little knowledge in regarding with Flutter animation. I need your help guys.


Example of shuffle animation
PS: this picture is not mine. I just want to upload this so then you have idea what does shuffle animation do in my tiles.

Shuffle animation


You can try this (DartPad) :

import 'package:flutter/material.dart';

void main() async => runApp(const MyApp());

class MyApp extends StatefulWidget {

  const MyApp({super.key});

  State<MyApp> createState() => _MyAppState();

class _MyAppState extends State<MyApp> {

  final String word = "TRIPLE";
  final double size = 170;
  final double space = 10;

  late final List<Color> colors = Colors.primaries
  .map((MaterialColor m) => m.shade800)
  late final int containerNumber = word.length;
  late final List<Rect> positions = List.generate(
    (index) => getRectangle(index, containerNumber, size)

  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Material App Bar'),
        body: Center(
          child: SingleChildScrollView(
            child: Column(
              mainAxisSize: MainAxisSize.max,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                const SizedBox(height: 50,),
                  onPressed: shuffle, 
                  child: const Text("Shuffle")
                const SizedBox(height: 50,),
                  height: containerNumber ~/3 * (size+space) - space,
                  width: 3 * (size+space) - space,
                  child: Stack(
                    fit: StackFit.loose,
                    alignment: Alignment.center,
                    children: List.generate(containerNumber, (index) {
                      return AnimatedPositioned.fromRect(
                        rect: positions[index],
                        duration: const Duration(milliseconds: 500),
                        child: Container(
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20),
                            color: colors[index],
                          width: size,
                          height: size,
                          child: Text(
                            style: const TextStyle(
                              fontSize: 60,
                              fontWeight: FontWeight.w900,
                              color: Colors.white

  Rect getRectangle(int index, int length, double size) {
      Offset(index % 3 * (size + space), index ~/3 * (size + space)) 
      & Size.square(size);
  void shuffle() {
    setState(() {

This Question was asked in StackOverflow by Mosh Mobile Software Engineer and Answered by TripleNine It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?