This commit is contained in:
toly
2023-10-24 13:53:28 +08:00
parent 9313882748
commit 9c91d7d7f3
9 changed files with 175 additions and 37 deletions

View File

@@ -9,7 +9,7 @@ import '../../../pages/empty/empty_page.dart';
import '../../../pages/settings/settings_page.dart';
import '../../../pages/user/user_page.dart';
import '../../../pages/counter/counter_page.dart';
import '../../../pages/sort/sort_page.dart';
import '../../../pages/sort/views/sort_page.dart';
import '../transition/fade_transition_page.dart';
import '../../../pages/color/color_add_page.dart';

View File

@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:iroute/v5/pages/sort/provider/sort_config.dart';
import 'package:iroute/v5/pages/sort/bloc/sort_config.dart';
import '../../../pages/sort/sort_setting.dart';
import '../router/app_router_delegate.dart';
import 'app_navigation_rail.dart';

View File

@@ -2,7 +2,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:iroute/components/toly_ui/button/hover_icon_button.dart';
import 'package:iroute/components/toly_ui/popable/drop_selectable_widget.dart';
import 'package:iroute/v5/pages/sort/provider/sort_config.dart';
import 'package:iroute/v5/pages/sort/bloc/sort_config.dart';
import '../../../pages/sort/functions.dart';
import '../router/app_router_delegate.dart';

View File

@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import '../pages/sort/bloc/state.dart';
import 'navigation/router/app_router_delegate.dart';
import 'navigation/views/app_navigation.dart';
import 'navigation/views/app_navigation_rail.dart';
@@ -8,20 +9,23 @@ class UnitApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
fontFamily: "宋体",
scaffoldBackgroundColor: Colors.white,
appBarTheme: const AppBarTheme(
elevation: 0,
iconTheme: IconThemeData(color: Colors.black),
titleTextStyle: TextStyle(
color: Colors.black,
fontSize: 18,
fontWeight: FontWeight.bold,
))),
debugShowCheckedModeBanner: false,
home: AppNavigation()
return SortStateScope(
notifier: SortState(),
child: MaterialApp(
theme: ThemeData(
fontFamily: "宋体",
scaffoldBackgroundColor: Colors.white,
appBarTheme: const AppBarTheme(
elevation: 0,
iconTheme: IconThemeData(color: Colors.black),
titleTextStyle: TextStyle(
color: Colors.black,
fontSize: 18,
fontWeight: FontWeight.bold,
))),
debugShowCheckedModeBanner: false,
home: AppNavigation()
),
);
}
}

View File

@@ -6,12 +6,13 @@ class SortConfig {
final int count;
final int seed;
final Duration duration;
final String name;
SortConfig(this.count, this.duration,this.seed);
SortConfig(this.count, this.duration,this.seed,this.name);
}
final ValueNotifier<SortConfig> sortConfig = ValueNotifier(
SortConfig(-1, const Duration(microseconds: 1500),-1),
SortConfig(-1, const Duration(microseconds: 1500),-1,'quick'),
);

View 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!;
}

View File

@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'provider/sort_config.dart';
import 'bloc/sort_config.dart';
class SortSettings extends StatefulWidget {
@@ -79,7 +79,7 @@ class _SortSettingsState extends State<SortSettings> {
sortConfig.value = SortConfig(int.parse(_count.text), Duration(
microseconds: int.parse(_duration.text),
),int.parse(_seed.text));
),int.parse(_seed.text),sortConfig.value.name);
Navigator.of(context).pop();
}, child: Text('确定设置'))

View File

@@ -0,0 +1,60 @@
import 'package:flutter/material.dart';
class DataPainter extends CustomPainter{
final List<int> data;
DataPainter({required this.data});
@override
void paint(Canvas canvas, Size size) {
double itemWidth = size.width/data.length;
double height = size.height;
Paint paint = Paint();
paint.strokeWidth = itemWidth;
paint.strokeCap = StrokeCap.round;
for(int i=0;i<data.length;i++){
int value = data[i];
if (value < height * .10) {
paint.color = Colors.blue.shade100;
} else if (value < height * .20) {
paint.color = Colors.blue.shade200;
} else if (value < height * .30) {
paint.color = Colors.blue.shade300;
} else if (value < height * .40) {
paint.color = Colors.blue.shade400;
} else if (value < height * .50) {
paint.color = Colors.blue.shade500;
} else if (value < height * .60) {
paint.color = Colors.blue.shade600;
} else if (value < height * .70) {
paint.color = Colors.blue.shade700;
} else if (value < height * .80) {
paint.color = Colors.blue.shade800;
} else if (value < height * .90) {
paint.color = Colors.blue.shade900;
} else {
paint.color = const Color(0xFF011E51);
}
canvas.drawLine(
Offset(i * itemWidth+itemWidth/2, 0),
Offset(
i * itemWidth+itemWidth/2,
value.ceilToDouble(),
),
paint);
}
}
@override
bool shouldRepaint(covariant DataPainter oldDelegate) {
return true;
}
}

View File

@@ -3,8 +3,10 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'functions.dart';
import 'provider/sort_config.dart';
import '../bloc/state.dart';
import '../functions.dart';
import '../bloc/sort_config.dart';
import 'data_painter.dart';
class SortPage extends StatefulWidget {
final Size size;
@@ -92,15 +94,14 @@ class _SortPageState extends State<SortPage> {
if(count==-1){
s = widget.size.width~/2;
}
if(sortConfig.value.seed!=-1){
random = Random(sortConfig.value.seed);
}
for (int i = 0; i < s; i++) {
for (int i = 0; i < s; i++) {
//
numbers.add(random.nextInt(widget.size.height.toInt()));
//numbers.add(Random().nextInt();
}
setState(() {});
}
@@ -114,6 +115,9 @@ class _SortPageState extends State<SortPage> {
@override
Widget build(BuildContext context) {
List<int> numbers = SortStateScope.of(context).data;
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.run_circle_outlined),
@@ -126,17 +130,9 @@ class _SortPageState extends State<SortPage> {
stream: streamController.stream,
builder: (context, snapshot) {
List<int> numbers = snapshot.data as List<int>;
return Row(
children: numbers.asMap().keys.map((int index) {
return CustomPaint(
painter: BarPainter(
height: widget.size.height,
width: widget.size.width/numbers.length,
value: numbers[index],
index: index,
),
);
}).toList(),
return CustomPaint(
size: widget.size,
painter: DataPainter(data: numbers),
);
},
),