|
1 | | -import 'package:algorithm_visualizer/core/helpers/random_int.dart'; |
2 | 1 | import 'package:algorithm_visualizer/core/resources/strings_manager.dart'; |
3 | 2 | import 'package:algorithm_visualizer/core/resources/theme_manager.dart'; |
4 | 3 | import 'package:algorithm_visualizer/core/widgets/custom_widgets/custom_dialog.dart'; |
5 | 4 | import 'package:algorithm_visualizer/core/widgets/custom_widgets/custom_icon.dart'; |
| 5 | +import 'package:algorithm_visualizer/features/sorting/view_model/sorting_notifier.dart'; |
6 | 6 | import 'package:flutter/material.dart'; |
7 | 7 | import 'package:flutter_riverpod/flutter_riverpod.dart'; |
8 | | -import 'package:flutter_screenutil/flutter_screenutil.dart'; |
| 8 | +part '../widgets/sorting_app_bar.dart'; |
| 9 | + |
| 10 | +final _notifierProvider = StateNotifierProvider<SortingNotifier, SortingNotifierState>( |
| 11 | + (ref) => SortingNotifier(), |
| 12 | +); |
9 | 13 |
|
10 | 14 | class SortingPage extends StatelessWidget { |
11 | 15 | const SortingPage({super.key}); |
12 | 16 |
|
13 | 17 | @override |
14 | 18 | Widget build(BuildContext context) { |
15 | | - final list = CustomRandom.generateRandomList(150, 70); |
16 | | - |
17 | 19 | return Scaffold( |
18 | 20 | appBar: AppBar( |
19 | 21 | elevation: 1, |
20 | | - actions: [ |
21 | | - Consumer( |
22 | | - builder: (context, ref, _) { |
23 | | - return TextButton( |
24 | | - onPressed: () { |
25 | | - CustomAlertDialog(context).solidDialog( |
26 | | - parameters: [ |
27 | | - ListDialogParameters( |
28 | | - text: StringsManager.generateMaze, |
29 | | - onTap: () {}, |
30 | | - ), |
31 | | - ListDialogParameters( |
32 | | - text: "Dijkstra", |
33 | | - onTap: () {}, |
34 | | - ), |
35 | | - ListDialogParameters( |
36 | | - text: "BFS", |
37 | | - onTap: () {}, |
38 | | - ), |
39 | | - ListDialogParameters( |
40 | | - text: StringsManager.clearPath, |
41 | | - color: ThemeEnum.redColor, |
42 | | - onTap: () {}, |
43 | | - ), |
44 | | - ListDialogParameters( |
45 | | - text: StringsManager.clearAll, |
46 | | - color: ThemeEnum.redColor, |
47 | | - onTap: () {}, |
48 | | - ), |
49 | | - ], |
50 | | - ); |
51 | | - }, |
52 | | - child: const CustomIcon(Icons.menu_rounded), |
53 | | - ); |
| 22 | + title: Consumer(builder: (context, ref, _) { |
| 23 | + return InkWell( |
| 24 | + onTap: () { |
| 25 | + ref.read(_notifierProvider.notifier).bubbleSort(); |
54 | 26 | }, |
55 | | - ), |
56 | | - ], |
| 27 | + child: const Text("Sort"), |
| 28 | + ); |
| 29 | + }), |
57 | 30 | ), |
58 | | - body: Padding( |
59 | | - padding: REdgeInsets.only(top: 15), |
60 | | - child: Row( |
61 | | - crossAxisAlignment: CrossAxisAlignment.end, |
62 | | - children: List.generate( |
63 | | - list.length, |
64 | | - (index) => Flexible( |
65 | | - child: Container( |
66 | | - margin: REdgeInsets.symmetric(horizontal: 1), |
67 | | - height: (1 + (list[index])).toDouble(), |
68 | | - decoration: BoxDecoration( |
69 | | - color: context.getColor(ThemeEnum.darkBlueColor), |
70 | | - borderRadius: const BorderRadius.vertical( |
71 | | - top: Radius.circular(1), |
72 | | - ), |
73 | | - ), |
| 31 | + body: Consumer(builder: (context, ref, _) { |
| 32 | + final items = ref.watch(_notifierProvider).list; |
| 33 | + |
| 34 | + return Padding( |
| 35 | + padding: const EdgeInsets.only(top: 15), |
| 36 | + child: SizedBox( |
| 37 | + height: SortingNotifier.maxListItemHeight*1.2, |
| 38 | + width: double.infinity, |
| 39 | + child: Stack( |
| 40 | + alignment: AlignmentDirectional.bottomCenter, |
| 41 | + children: List.generate( |
| 42 | + items.length, |
| 43 | + (index) { |
| 44 | + final item = items[index]; |
| 45 | + final position = |
| 46 | + ref.watch(_notifierProvider.select((state) => state.positions[item.id]!)); |
| 47 | + return AnimatedPositioned( |
| 48 | + key: ValueKey(item.id), |
| 49 | + left: position.dx, |
| 50 | + bottom: position.dy, |
| 51 | + duration: SortingNotifier.swipeDuration, |
| 52 | + child: _BuildItem(item: item), |
| 53 | + ); |
| 54 | + }, |
74 | 55 | ), |
75 | 56 | ), |
76 | 57 | ), |
| 58 | + ); |
| 59 | + }), |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class _BuildItem extends ConsumerWidget { |
| 65 | + const _BuildItem({required this.item}); |
| 66 | + |
| 67 | + final SortableItem item; |
| 68 | + |
| 69 | + @override |
| 70 | + Widget build(BuildContext context, ref) { |
| 71 | + final itemWidth = SortingNotifier.calculateItemWidth(context); |
| 72 | + return Padding( |
| 73 | + padding: EdgeInsets.symmetric(horizontal: SortingNotifier.itemsPadding / 2), |
| 74 | + child: Container( |
| 75 | + height: SortingNotifier.calculateItemHeight(item.value), |
| 76 | + width: itemWidth, |
| 77 | + decoration: BoxDecoration( |
| 78 | + color: SortingNotifier.getColor(item.value), |
| 79 | + borderRadius: const BorderRadius.vertical( |
| 80 | + top: Radius.circular(1), |
| 81 | + ), |
77 | 82 | ), |
78 | 83 | ), |
79 | 84 | ); |
|
0 commit comments