diff --git a/lib/components/toly_ui/popable/drop_selectable_widget.dart b/lib/components/toly_ui/popable/drop_selectable_widget.dart index 5e30c67..da8e4e8 100644 --- a/lib/components/toly_ui/popable/drop_selectable_widget.dart +++ b/lib/components/toly_ui/popable/drop_selectable_widget.dart @@ -113,6 +113,7 @@ class _DropSelectableWidgetState extends State decoration: BoxDecoration( borderRadius: BorderRadius.circular(20), border: Border.all( + width: _focused ?1:1/View.of(context).devicePixelRatio, color: _focused ? Colors.blue : widget.disableColor, )), child: Row( diff --git a/lib/v5/app/navigation/router/app_router_delegate.dart b/lib/v5/app/navigation/router/app_router_delegate.dart index 10cd84c..6910126 100644 --- a/lib/v5/app/navigation/router/app_router_delegate.dart +++ b/lib/v5/app/navigation/router/app_router_delegate.dart @@ -98,8 +98,7 @@ class AppRouterDelegate extends RouterDelegate with ChangeNotifier { child = const CounterPage(); } if (path == kDestinationsPaths[2]) { - child = LayoutBuilder( - builder: (_,cts)=> SortPage(size: Size(cts.maxWidth,cts.maxHeight),)); + child = SortPage(); } if (path == kDestinationsPaths[3]) { child = const UserPage(); diff --git a/lib/v5/app/navigation/views/app_navigation.dart b/lib/v5/app/navigation/views/app_navigation.dart index 10c84f4..e1d002d 100644 --- a/lib/v5/app/navigation/views/app_navigation.dart +++ b/lib/v5/app/navigation/views/app_navigation.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.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'; @@ -13,7 +12,7 @@ class AppNavigation extends StatelessWidget { double px1 = 1/View.of(context).devicePixelRatio; return Scaffold( endDrawer: Drawer( - child: SortSettings(config: sortConfig.value,), + child: SortSettings(), ), body: Row( children: [ diff --git a/lib/v5/app/navigation/views/app_router_editor.dart b/lib/v5/app/navigation/views/app_router_editor.dart index ddbe2f2..9173948 100644 --- a/lib/v5/app/navigation/views/app_router_editor.dart +++ b/lib/v5/app/navigation/views/app_router_editor.dart @@ -2,8 +2,9 @@ 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/bloc/sort_config.dart'; import '../../../pages/sort/functions.dart'; +import '../../../pages/sort/views/sort_bar.dart'; +import '../../../pages/sort/views/sort_button.dart'; import '../router/app_router_delegate.dart'; class AppRouterEditor extends StatefulWidget { @@ -28,6 +29,9 @@ class _AppRouterEditorState extends State { void _onRouteChange() { _controller.text=router.path; + setState(() { + + }); } @override @@ -39,58 +43,37 @@ class _AppRouterEditorState extends State { @override Widget build(BuildContext context) { + print(router.path); + if(router.path=='/sort'){ + return SortBar(); + } - return Row( - children: [ - DropSelectableWidget( - fontSize: 12, - data: sortNameMap.values.toList(), - iconSize: 20, - height: 30, - width: 200, - disableColor: const Color(0xff1F425F), - onDropSelected: (int index) async { -sortName.value=sortNameMap.keys.toList()[index]; - // curveAnim = CurvedAnimation( - // parent: _ctrl, curve: maps.values.toList()[index]); - // _startAnim(); - }, - ), - const SizedBox(width: 10,), - GestureDetector( - onTap: (){ - Scaffold.of(context).openEndDrawer(); - // showDialog( - // useRootNavigator: false, - // context: context, builder: (ctx)=>AlertDialog()); - }, - child: const Icon(Icons.settings)) - ], - ); - - return Stack( - alignment: Alignment.centerRight, - children: [ - SizedBox( - child: CupertinoTextField( - controller: _controller, - style: TextStyle(fontSize: 14), - padding: EdgeInsets.only(left:12,top: 6,bottom: 6,right: 32), - placeholder: '输入路由地址导航', - onSubmitted: widget.onSubmit, - decoration: BoxDecoration(color: Color(0xffF1F2F3),borderRadius: BorderRadius.circular(6)), + return SizedBox( + width: 250, + child: Stack( + alignment: Alignment.centerRight, + children: [ + SizedBox( + child: CupertinoTextField( + controller: _controller, + style: TextStyle(fontSize: 14), + padding: EdgeInsets.only(left:12,top: 6,bottom: 6,right: 32), + placeholder: '输入路由地址导航', + onSubmitted: widget.onSubmit, + decoration: BoxDecoration(color: Color(0xffF1F2F3),borderRadius: BorderRadius.circular(6)), + ), ), - ), - Padding( - padding: const EdgeInsets.only(right: 8.0), - child: HoverIconButton( - icon: Icons.directions_outlined, - defaultColor: Color(0xff68696B), - onPressed:()=>widget.onSubmit?.call(_controller.text), - size: 20 - ), - ) - ], + Padding( + padding: const EdgeInsets.only(right: 8.0), + child: HoverIconButton( + icon: Icons.directions_outlined, + defaultColor: Color(0xff68696B), + onPressed:()=>widget.onSubmit?.call(_controller.text), + size: 20 + ), + ) + ], + ), ); } } diff --git a/lib/v5/app/unit_app.dart b/lib/v5/app/unit_app.dart index e8d89a7..36e67ab 100644 --- a/lib/v5/app/unit_app.dart +++ b/lib/v5/app/unit_app.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import '../pages/sort/bloc/state.dart'; +import '../pages/sort/provider/state.dart'; import 'navigation/router/app_router_delegate.dart'; import 'navigation/views/app_navigation.dart'; import 'navigation/views/app_navigation_rail.dart'; diff --git a/lib/v5/pages/sort/bloc/sort_config.dart b/lib/v5/pages/sort/bloc/sort_config.dart deleted file mode 100644 index 2983815..0000000 --- a/lib/v5/pages/sort/bloc/sort_config.dart +++ /dev/null @@ -1,19 +0,0 @@ -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 = ValueNotifier( - SortConfig(-1, const Duration(microseconds: 1500),-1,'quick'), -); - - -final ValueNotifier sortName = ValueNotifier('quick'); diff --git a/lib/v5/pages/sort/provider/sort_config.dart b/lib/v5/pages/sort/provider/sort_config.dart new file mode 100644 index 0000000..2ad50ed --- /dev/null +++ b/lib/v5/pages/sort/provider/sort_config.dart @@ -0,0 +1,31 @@ +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 = 100, + this.duration = const Duration(microseconds: 1500), + this.seed = -1, + this.name = 'insertion', + }); + + SortConfig copyWith({ + int? count, + int? seed, + Duration? duration, + String? name, + }) => + SortConfig( + count:count??this.count, + seed:seed??this.seed, + duration:duration??this.duration, + name:name??this.name, + ); +} + diff --git a/lib/v5/pages/sort/bloc/state.dart b/lib/v5/pages/sort/provider/state.dart similarity index 80% rename from lib/v5/pages/sort/bloc/state.dart rename to lib/v5/pages/sort/provider/state.dart index 8f9664a..e60e208 100644 --- a/lib/v5/pages/sort/bloc/state.dart +++ b/lib/v5/pages/sort/provider/state.dart @@ -13,35 +13,37 @@ enum SortStatus{ class SortState with ChangeNotifier{ + SortState(){ + reset(); + } + SortStatus status = SortStatus.none; List data = []; + List stepData = []; - SortConfig _config = SortConfig(-1, const Duration(microseconds: 1500),-1,'quick'); + SortConfig _config = SortConfig(); SortConfig get config => _config; Random random = Random(); set config(SortConfig config){ _config = config; - reset(_zoneSize); + reset(); notifyListeners(); } - Size _zoneSize = Size.zero; - void reset(Size zoneSize){ - _zoneSize = zoneSize; - status = SortStatus.sorting; + void reset(){ + data.clear(); + status = SortStatus.none; + notifyListeners(); 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())); + data.add(random.nextInt(1000)); } } @@ -53,7 +55,6 @@ class SortState with ChangeNotifier{ if(sortFunction!=null){ await sortFunction(data,(arr) async { await Future.delayed(config.duration); - data = arr; notifyListeners(); }); } diff --git a/lib/v5/pages/sort/sort_setting.dart b/lib/v5/pages/sort/sort_setting.dart index df06c1a..3b61da8 100644 --- a/lib/v5/pages/sort/sort_setting.dart +++ b/lib/v5/pages/sort/sort_setting.dart @@ -1,34 +1,38 @@ import 'package:flutter/material.dart'; -import 'bloc/sort_config.dart'; +import 'provider/sort_config.dart'; +import 'provider/state.dart'; class SortSettings extends StatefulWidget { - final SortConfig config; - - const SortSettings({super.key,required this.config}); + const SortSettings({super.key,}); @override State createState() => _SortSettingsState(); } class _SortSettingsState extends State { + late TextEditingController _count = + TextEditingController(); + late TextEditingController _duration = TextEditingController(); + late TextEditingController _seed = + TextEditingController(); - late TextEditingController _count = TextEditingController( - text:sortConfig.value.count.toString() - ); - late TextEditingController _duration = TextEditingController( - text:sortConfig.value.duration.inMicroseconds.toString() - ); - late TextEditingController _seed = TextEditingController( - text:sortConfig.value.seed.toString() - ); @override void initState() { - // TODO: implement initState super.initState(); } + @override + void didChangeDependencies() { + print('========_SortSettingsState#didChangeDependencies============='); + super.didChangeDependencies(); + SortState state = SortStateScope.of(context); + _count.text = state.config.count.toString(); + _duration.text = state.config.duration.inMicroseconds.toString(); + _seed.text = state.config.seed.toString(); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -50,8 +54,11 @@ class _SortSettingsState extends State { Row( children: [ Text('数据数量(个数):'), - const SizedBox(width: 20,), - Expanded(child: TextField( + const SizedBox( + width: 20, + ), + Expanded( + child: TextField( controller: _count, )), ], @@ -59,8 +66,11 @@ class _SortSettingsState extends State { Row( children: [ Text('时间间隔(微秒):'), - const SizedBox(width: 20,), - Expanded(child: TextField( + const SizedBox( + width: 20, + ), + Expanded( + child: TextField( controller: _duration, )), ], @@ -68,21 +78,29 @@ class _SortSettingsState extends State { Row( children: [ Text('随机种子:'), - const SizedBox(width: 20,), - Expanded(child: TextField( + const SizedBox( + width: 20, + ), + Expanded( + child: TextField( controller: _seed, )), ], ), Spacer(), - ElevatedButton(onPressed: (){ - - sortConfig.value = SortConfig(int.parse(_count.text), Duration( - microseconds: int.parse(_duration.text), - ),int.parse(_seed.text),sortConfig.value.name); - - Navigator.of(context).pop(); - }, child: Text('确定设置')) + ElevatedButton( + onPressed: () { + SortState state = SortStateScope.of(context); + state.config =state.config.copyWith( + count: int.parse(_count.text), + duration: Duration( + microseconds: int.parse(_duration.text), + ), + seed: int.parse(_seed.text) + ); + Navigator.of(context).pop(); + }, + child: Text('确定设置')) ], ), ), diff --git a/lib/v5/pages/sort/views/data_painter.dart b/lib/v5/pages/sort/views/data_painter.dart index 881d967..d23e2f5 100644 --- a/lib/v5/pages/sort/views/data_painter.dart +++ b/lib/v5/pages/sort/views/data_painter.dart @@ -19,23 +19,23 @@ class DataPainter extends CustomPainter{ for(int i=0;iAlertDialog()); + }, + child: const Icon(Icons.settings)) + ], + ); + } +} diff --git a/lib/v5/pages/sort/views/sort_button.dart b/lib/v5/pages/sort/views/sort_button.dart new file mode 100644 index 0000000..ec2b30a --- /dev/null +++ b/lib/v5/pages/sort/views/sort_button.dart @@ -0,0 +1,40 @@ +import 'package:flutter/material.dart'; + +import '../provider/state.dart'; + +class SortButton extends StatelessWidget { + const SortButton({super.key}); + + @override + Widget build(BuildContext context) { + SortState state = SortStateScope.of(context); + VoidCallback? action; + IconData icon; + Color color; + switch (state.status) { + case SortStatus.none: + icon = Icons.not_started_outlined; + color = Colors.green; + action = state.sort; + break; + case SortStatus.sorting: + icon = Icons.stop_circle_outlined; + color = Colors.grey; + action = null; + break; + case SortStatus.sorted: + icon = Icons.refresh; + color = Colors.black; + action = state.reset; + break; + } + + return GestureDetector( + onTap: action, + child: Icon( + icon, + color: color, + ), + ); + } +} diff --git a/lib/v5/pages/sort/views/sort_page.dart b/lib/v5/pages/sort/views/sort_page.dart index ab554b3..01224c3 100644 --- a/lib/v5/pages/sort/views/sort_page.dart +++ b/lib/v5/pages/sort/views/sort_page.dart @@ -1,228 +1,22 @@ -import 'dart:async'; -import 'dart:math'; import 'package:flutter/material.dart'; -import '../bloc/state.dart'; -import '../functions.dart'; -import '../bloc/sort_config.dart'; +import '../provider/state.dart'; import 'data_painter.dart'; -class SortPage extends StatefulWidget { - final Size size; - const SortPage({Key? key, required this.size}) : super(key: key); - - @override - State createState() => _SortPageState(); -} - -class _SortPageState extends State { - //存放随机数组 - List numbers = []; - - //订阅流 - StreamController> streamController = StreamController(); - - String get currentSort => sortName.value; - - //柱子的数量 -> 生成排序数组的长度 - double sampleSize = 0; - - //是否排序 - bool isSorted = false; - - //是否在排序中 - bool isSorting = false; - - - reset() { - isSorted = false; - setSize(sortConfig.value.count); - streamController.add(numbers); - } - - Duration getDuration() { - return sortConfig.value.duration; - } - - checkAndResetIfSorted() async { - if (isSorted) { - reset(); - await Future.delayed(const Duration(milliseconds: 200)); - } - } - - sort() async { - setState(() { - isSorting = true; - }); - - await checkAndResetIfSorted(); - - Stopwatch stopwatch = Stopwatch()..start(); - - SortFunction? sortFunction = sortFunctionMap[currentSort]; - if(sortFunction!=null){ - await sortFunction(numbers,(arr) async { - await Future.delayed(getDuration()); - streamController.add(arr); - }); - } - - stopwatch.stop(); - print("Sorting completed in ${stopwatch.elapsed.inMilliseconds} ms."); - setState(() { - isSorting = false; - isSorted = true; - }); - } - - @override - void initState() { - super.initState(); - // reset(); - print(widget.size); - setSize(sortConfig.value.count); - sortConfig.addListener(_onSortConfigChange); - sortName.addListener(reset); - } - - Random random = Random(); - void setSize(int count){ - int s = count; - numbers.clear(); - 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++) { - //随机往数组中填值 - numbers.add(random.nextInt(widget.size.height.toInt())); - } - setState(() {}); - } - - - @override - void dispose() { - streamController.close(); - super.dispose(); - } +class SortPage extends StatelessWidget{ @override Widget build(BuildContext context) { - - List numbers = SortStateScope.of(context).data; + SortState state = SortStateScope.of(context); + List numbers = state.data; return Scaffold( - floatingActionButton: FloatingActionButton( - child: Icon(Icons.run_circle_outlined), - onPressed: (){ - sort(); - }, + body: CustomPaint( + painter: DataPainter(data: numbers), + child: ConstrainedBox(constraints: BoxConstraints.expand()), ), - body: StreamBuilder( - initialData: numbers, - stream: streamController.stream, - builder: (context, snapshot) { - List numbers = snapshot.data as List; - return CustomPaint( - size: widget.size, - painter: DataPainter(data: numbers), - ); - }, - ), - // bottomNavigationBar: BottomAppBar( - // child: Row( - // mainAxisAlignment: MainAxisAlignment.spaceAround, - // children: [ - // ElevatedButton( - // onPressed: isSorting - // ? null - // : () { - // reset(); - // setSort(currentSort); - // }, - // child: const Text("重置")), - // ElevatedButton( - // onPressed: isSorting ? null : sort, child: const Text("开始排序")), - // ElevatedButton( - // onPressed: isSorting ? null : changeSpeed, - // child: Text( - // "${speed + 1}x", - // style: const TextStyle(fontSize: 20), - // ), - // ), - // ], - // ), - // ), ); } - - void _onSortConfigChange() { - setSize(sortConfig.value.count); - } } -class BarPainter extends CustomPainter { - //宽度 - final double width; - final double height; - - //高度(数组中对应的值) - final int value; - - //位置索引 - final int index; - - BarPainter({required this.width, required this.height,required this.value, required this.index}); - - @override - void paint(Canvas canvas, Size size) { - Paint paint = Paint(); - // double rate = value/height; - // print(rate); - // paint.color = Colors.blue.withOpacity(rate); - if (value < 500 * .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); - } - - paint.strokeWidth = width; - paint.strokeCap = StrokeCap.round; - - canvas.drawLine( - Offset(index * width+width/2, 0), - Offset( - index * width+width/2, - value.ceilToDouble(), - ), - paint); - } - - @override - bool shouldRepaint(covariant CustomPainter oldDelegate) { - return true; - } -} diff --git a/windows/runner/main.cpp b/windows/runner/main.cpp index c92ca90..ae006fd 100644 --- a/windows/runner/main.cpp +++ b/windows/runner/main.cpp @@ -26,7 +26,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev, FlutterWindow window(project); Win32Window::Point origin(10, 10); - Win32Window::Size size(640, 360); + Win32Window::Size size(1600, 1200); if (!window.Create(L"iroute", origin, size)) { return EXIT_FAILURE; }