Animation 在颤振中设置循环动画

Animation 在颤振中设置循环动画,animation,dart,flutter,mobile-development,flutter-animation,Animation,Dart,Flutter,Mobile Development,Flutter Animation,我正在尝试制作一个颤振排序算法的动画。到目前为止,我已经编写了算法,并通过一次迭代一次而不是整个排序过程来获得某种动画,但您必须不断点击按钮进行排序,一次一项。我一直在努力寻找一种方法来激活这个过程。这是我的密码: import 'package:flutter/material.dart'; import 'dart:math'; List<double> rectHeights = new List<double>(); int n = 2; void main(

我正在尝试制作一个颤振排序算法的动画。到目前为止,我已经编写了算法,并通过一次迭代一次而不是整个排序过程来获得某种动画,但您必须不断点击按钮进行排序,一次一项。我一直在努力寻找一种方法来激活这个过程。这是我的密码:

import 'package:flutter/material.dart';
import 'dart:math';

List<double> rectHeights = new List<double>();
int n = 2;

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Sorting',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  int _selectedIndex = 0;
  final _widgetOptions = [
    Text('Index 0: Sort'),
    Text('Index 1: Shuffle'),
  ];

  @override
  void initState() {
    super.initState();
    Random random = new Random();
    for (int i = 0; i < 35; i++) {
      double ranNum = random.nextDouble() * 600;
      rectHeights.add(ranNum);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: EdgeInsets.all(8.0),
        child: Center(
          child: Row(
            children: rectangles(),
          ),
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        iconSize: 50.0,
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(icon: Icon(Icons.sort), title: Text('Sort', style: TextStyle(fontSize: 20.0),)),
            BottomNavigationBarItem(icon: Icon(Icons.shuffle), title: Text('Shuffle', style: TextStyle(fontSize: 20.0),)),
          ],
        currentIndex: _selectedIndex,
        fixedColor: Colors.blue,
        onTap: _onItemTapped,
      ),
    );
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
    switch(_selectedIndex) {
      case 0:
          setState(() {
            insertSortOnce(rectHeights, 1);
          });
          break;
      case 1:
        setState(() {
          shuffle(rectHeights);
          n = 2;
        });

    }
  }
}


List<Widget> rectangles() {
  List<Widget> rects = new List<Widget>();
  for (double height in rectHeights) {
    var rect = Padding(
      padding: EdgeInsets.symmetric(horizontal: 1.0),
      child: Container(
        width: 8.0,
        height: height,
        decoration: BoxDecoration(
            shape: BoxShape.rectangle,
            color: Colors.blue
        ),
      ),
    );
    rects.add(rect);
  }
  return rects;
}

void insertSort(values, choice) {
  int i, j;
  double key, temp;
  for (i = 1; i < values.length; i++) {
    key = values[i];
    j = i - 1;
    switch (choice) {
      case 1:
        while (j >= 0 && key < values[j]) {
          temp = values[j];
          values[j] = values[j + 1];
          values[j + 1] = temp;
          j--;
        }
        break;
      case 2:
        while (j >= 0 && key > values[j]) {
          temp = values[j];
          values[j] = values[j + 1];
          values[j + 1] = temp;
          j--;
        }
        break;
    }
  }
}

void insertSortOnce(values, choice) {
  int i, j;
  double key, temp;
  for (i = 1; i < n; i++) {
    key = values[i];
    j = i - 1;
    switch (choice) {
      case 1:
        while (j >= 0 && key < values[j]) {
          temp = values[j];
          values[j] = values[j + 1];
          values[j + 1] = temp;
          j--;
        }
        break;
      case 2:
        while (j >= 0 && key > values[j]) {
          temp = values[j];
          values[j] = values[j + 1];
          values[j + 1] = temp;
          j--;
        }
        break;
    }
  }
  n++;
}

List shuffle(List items) {
  var random = new Random();

  // Go through all elements.
  for (var i = items.length - 1; i > 0; i--) {

    // Pick a pseudorandom number according to the list length
    var n = random.nextInt(i + 1);

    var temp = items[i];
    items[i] = items[n];
    items[n] = temp;
  }

  return items;
}
导入“包装:颤振/材料.省道”;
导入“dart:math”;
列表高度=新列表();
int n=2;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“排序”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key}):超级(Key:Key);
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState使用SingleTickerProviderStateMixin扩展状态{
int _selectedIndex=0;
最终_widgetOptions=[
文本('Index 0:Sort'),
文本('Index 1:Shuffle'),
];
@凌驾
void initState(){
super.initState();
随机=新随机();
对于(int i=0;i<35;i++){
double ranNum=random.nextDouble()*600;
rectHeights.add(ranNum);
}
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:填充物(
填充:边缘设置。全部(8.0),
儿童:中心(
孩子:排(
子对象:矩形(),
),
),
),
底部导航栏:底部导航栏(
iconSize:50.0,
项目:[
BottomNavigationBarItem(图标:图标(Icons.sort),标题:文本('sort',样式:TextStyle(fontSize:20.0)),
BottomNavigationBarItem(图标:图标(Icons.shuffle),标题:文本('shuffle',样式:TextStyle(fontSize:20.0)),
],
currentIndex:_selectedIndex,
固定颜色:颜色。蓝色,
onTap:\u未映射,
),
);
}
void\u未映射(整数索引){
设置状态(){
_selectedIndex=索引;
});
开关(_selectedIndex){
案例0:
设置状态(){
插入法(1);
});
打破
案例1:
设置状态(){
洗牌;
n=2;
});
}
}
}
列表矩形(){
List rects=新列表();
用于(矩形高度中的双倍高度){
var rect=填充(
填充:边集。对称(水平:1.0),
子:容器(
宽度:8.0,
高度:高度,,
装饰:盒子装饰(
形状:BoxShape.rectangle,
颜色:颜色。蓝色
),
),
);
rects.add(rect);
}
返回矩形;
}
void insertSort(值,选项){
int i,j;
双键,温度;
对于(i=1;i=0&&key=0&&key>值[j]){
温度=数值[j];
值[j]=值[j+1];
数值[j+1]=温度;
j--;
}
打破
}
}
}
void insertSortOnce(值,选项){
int i,j;
双键,温度;
对于(i=1;i=0&&key=0&&key>值[j]){
温度=数值[j];
值[j]=值[j+1];
数值[j+1]=温度;
j--;
}
打破
}
}
n++;
}
列表洗牌(列表项){
var random=新的random();
//检查所有元素。
对于(var i=items.length-1;i>0;i--){
//根据列表长度选择一个伪随机数
var n=随机的nextInt(i+1);
var temp=项目[i];
项目[i]=项目[n];
项目[n]=温度;
}
退货项目;
}

您可以使用
Future
函数进行短延迟,例如(1秒)在每秒之后调用
insertSort()
,直到
排序完成:

  var _notSorted = true ;
  var _shiftNotPressed = true ;

  Future sortRectangles() async {
  while(_notSorted & _shiftPressed) { // You have to provide a condition to know when to stop
  await new Future.delayed(const Duration(seconds: 1), () {
      insertSortOnce(rectHeights, 1);
      }
    );
  }
} 
然后检查洗牌:

    void _onItemTapped(int index) {
setState(() {
  _selectedIndex = index;
});
switch(_selectedIndex) {
  case 0:
      setState(() {
        insertSortOnce(rectHeights, 1);
      });
      break;
  case 1:
    _shiftNotPressed = false ; // This is what you should add
    setState(() {
      shuffle(rectHeights);
      n = 2;
    });

  }
}
然后排序完成:

   void insertSortOnce(values, choice) {
   _notSorted = false ; // if it didn't execute the loop it means sorted 
   int i, j;
   double key, temp;
   for (i = 1; i < n; i++) {
   key = values[i];
   j = i - 1;
   switch (choice) {
     case 1:
      while (j >= 0 && key < values[j]) {
       _notSorted = true ; //You should figure a more efficient way to do this
       temp = values[j];
       values[j] = values[j + 1];
       values[j + 1] = temp;
       j--;
      }
     break;
    case 2:
    while (j >= 0 && key > values[j]) {
      temp = values[j];
      values[j] = values[j + 1];
      values[j + 1] = temp;
      j--;
    }
    break;
   }
 }
 n++;
}
void insertSortOnce(值,选项){
_notSorted=false;//如果没有执行循环,则表示已排序
int i,j;
双键,温度;
对于(i=1;i=0&&key=0&&key>值[j]){
温度=数值[j];
值[j]=值[j+1];
数值[j+1]=温度;
j--;
}
打破
}
}
n++;
}

此外,您还应在您的条件中包括是否按下了shift按钮并相应终止。

您可以使用
Future
函数进行短延迟,例如(1秒)在每秒之后调用
insertSort()
,直到
矩形高度
排序:

  var _notSorted = true ;
  var _shiftNotPressed = true ;

  Future sortRectangles() async {
  while(_notSorted & _shiftPressed) { // You have to provide a condition to know when to stop
  await new Future.delayed(const Duration(seconds: 1), () {
      insertSortOnce(rectHeights, 1);
      }
    );
  }
} 
然后检查洗牌:

    void _onItemTapped(int index) {
setState(() {
  _selectedIndex = index;
});
switch(_selectedIndex) {
  case 0:
      setState(() {
        insertSortOnce(rectHeights, 1);
      });
      break;
  case 1:
    _shiftNotPressed = false ; // This is what you should add
    setState(() {
      shuffle(rectHeights);
      n = 2;
    });

  }
}
然后排序完成:

   void insertSortOnce(values, choice) {
   _notSorted = false ; // if it didn't execute the loop it means sorted 
   int i, j;
   double key, temp;
   for (i = 1; i < n; i++) {
   key = values[i];
   j = i - 1;
   switch (choice) {
     case 1:
      while (j >= 0 && key < values[j]) {
       _notSorted = true ; //You should figure a more efficient way to do this
       temp = values[j];
       values[j] = values[j + 1];
       values[j + 1] = temp;
       j--;
      }
     break;
    case 2:
    while (j >= 0 && key > values[j]) {
      temp = values[j];
      values[j] = values[j + 1];
      values[j + 1] = temp;
      j--;
    }
    break;
   }
 }
 n++;
}
void insertSortOnce(值,选项){
_notSorted=false;//如果没有执行循环,则表示已排序
int i,j;
双键,温度;
对于(i=1;i=0&&key