v5-old
This commit is contained in:
19
lib/v5/pages/sort/bloc/sort_config.dart
Normal file
19
lib/v5/pages/sort/bloc/sort_config.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class SortConfig {
|
||||
final int count;
|
||||
final int seed;
|
||||
final Duration duration;
|
||||
final String name;
|
||||
|
||||
SortConfig(this.count, this.duration,this.seed,this.name);
|
||||
}
|
||||
|
||||
final ValueNotifier<SortConfig> sortConfig = ValueNotifier(
|
||||
SortConfig(-1, const Duration(microseconds: 1500),-1,'quick'),
|
||||
);
|
||||
|
||||
|
||||
final ValueNotifier<String> sortName = ValueNotifier('quick');
|
||||
77
lib/v5/pages/sort/bloc/state.dart
Normal file
77
lib/v5/pages/sort/bloc/state.dart
Normal file
@@ -0,0 +1,77 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
import '../functions.dart';
|
||||
import 'sort_config.dart';
|
||||
|
||||
enum SortStatus{
|
||||
none, // 未操作
|
||||
sorting, // 排序中
|
||||
sorted, // 排序完成
|
||||
}
|
||||
|
||||
class SortState with ChangeNotifier{
|
||||
|
||||
SortStatus status = SortStatus.none;
|
||||
|
||||
List<int> data = [];
|
||||
|
||||
SortConfig _config = SortConfig(-1, const Duration(microseconds: 1500),-1,'quick');
|
||||
SortConfig get config => _config;
|
||||
Random random = Random();
|
||||
|
||||
set config(SortConfig config){
|
||||
_config = config;
|
||||
reset(_zoneSize);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Size _zoneSize = Size.zero;
|
||||
|
||||
void reset(Size zoneSize){
|
||||
_zoneSize = zoneSize;
|
||||
status = SortStatus.sorting;
|
||||
int count = config.count;
|
||||
if(count==-1){
|
||||
count = zoneSize.width~/2;
|
||||
}
|
||||
if(config.seed!=-1){
|
||||
random = Random(config.seed);
|
||||
}
|
||||
for (int i = 0; i < count; i++) {
|
||||
//随机往数组中填值
|
||||
data.add(random.nextInt(zoneSize.height.toInt()));
|
||||
}
|
||||
}
|
||||
|
||||
void sort() async{
|
||||
status = SortStatus.sorting;
|
||||
notifyListeners();
|
||||
Stopwatch stopwatch = Stopwatch()..start();
|
||||
SortFunction? sortFunction = sortFunctionMap[config.name];
|
||||
if(sortFunction!=null){
|
||||
await sortFunction(data,(arr) async {
|
||||
await Future.delayed(config.duration);
|
||||
data = arr;
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
status = SortStatus.sorted;
|
||||
notifyListeners();
|
||||
stopwatch.stop();
|
||||
print("Sorting completed in ${stopwatch.elapsed.inMilliseconds} ms.");
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides the current [SortState] to descendant widgets in the tree.
|
||||
class SortStateScope extends InheritedNotifier<SortState> {
|
||||
const SortStateScope({
|
||||
required super.notifier,
|
||||
required super.child,
|
||||
super.key,
|
||||
});
|
||||
|
||||
static SortState of(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<SortStateScope>()!.notifier!;
|
||||
}
|
||||
Reference in New Issue
Block a user