Skip to content

Commit fcc752b

Browse files
handle sorting
1 parent 80a07b6 commit fcc752b

File tree

4 files changed

+96
-12
lines changed

4 files changed

+96
-12
lines changed

lib/features/sorting/view/sorting_page.dart

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ class SortingPage extends StatelessWidget {
3131
},
3232
),
3333
),
34-
body: const Stack(
34+
body: Stack(
3535
alignment: AlignmentDirectional.bottomCenter,
3636
children: [
37-
Align(alignment: AlignmentDirectional.topCenter, child: _BuildList()),
38-
_InteractionButton(),
37+
const Align(alignment: AlignmentDirectional.topCenter, child: _BuildList()),
38+
// _ControlButtons(),
39+
const Align(alignment: AlignmentDirectional.bottomCenter, child: _InteractionButton()),
40+
...List.generate(
41+
SortingNotifier.sortingAlgorithms.length,
42+
(index) => _SelectedOperation(index),
43+
),
3944
],
4045
),
4146
);
@@ -47,11 +52,47 @@ class _InteractionButton extends ConsumerWidget {
4752

4853
@override
4954
Widget build(BuildContext context, ref) {
50-
return const Column(
51-
mainAxisSize: MainAxisSize.min,
52-
children: [
53-
_ControlButtons(),
54-
],
55+
return const _ControlButtons();
56+
}
57+
}
58+
59+
class _SelectedOperation extends ConsumerWidget {
60+
const _SelectedOperation(this.index);
61+
final int index;
62+
@override
63+
Widget build(BuildContext context, ref) {
64+
final algo = SortingNotifier.sortingAlgorithms[index];
65+
final isChanged = ref.watch(_notifierProvider).selectedAlgorithms.contains(algo);
66+
final width = SortingNotifier.calculateButtonWidth(index);
67+
68+
return AnimatedPositionedDirectional(
69+
duration: const Duration(milliseconds: 300),
70+
start: width,
71+
bottom: isChanged ? 100 : 0,
72+
// width: width,
73+
child: CustomRoundedElevatedButton(
74+
roundedRadius: 3,
75+
backgroundColor: ThemeEnum.whiteD7Color,
76+
child: RegularText(algo.name, fontSize: 14),
77+
onPressed: () {
78+
ref.read(_notifierProvider.notifier).selectAlgorithm(index);
79+
},
80+
),
81+
);
82+
}
83+
}
84+
85+
class Item extends StatelessWidget {
86+
final String text;
87+
88+
const Item({super.key, required this.text});
89+
90+
@override
91+
Widget build(BuildContext context) {
92+
return Container(
93+
padding: const EdgeInsets.all(10),
94+
color: context.getColor(ThemeEnum.whiteD7Color),
95+
child: RegularText(text, color: ThemeEnum.primaryColor),
5596
);
5697
}
5798
}
@@ -61,7 +102,7 @@ class _BuildList extends ConsumerWidget {
61102

62103
@override
63104
Widget build(BuildContext context, ref) {
64-
final items = ref.watch(_notifierProvider).list;
105+
final items = ref.watch(_notifierProvider.select((state) => state.list));
65106

66107
return Padding(
67108
padding: const EdgeInsets.only(top: 15),

lib/features/sorting/view_model/sorting_notifier.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import 'package:algorithm_visualizer/core/helpers/screen_size.dart';
2+
import 'package:algorithm_visualizer/core/resources/theme_manager.dart';
3+
import 'package:algorithm_visualizer/core/widgets/adaptive/text/adaptive_text.dart';
24
import 'package:collection/collection.dart';
35
import 'package:flutter/material.dart';
46
import 'package:flutter_riverpod/flutter_riverpod.dart';
@@ -9,6 +11,9 @@ part 'sorting_state.dart';
911

1012
enum SortingEnum { played, stopped, none }
1113

14+
enum SortingAlgorithm { bubble, selection, merge, quick }
15+
16+
//SortingAlgorithm
1217
class SortableItem {
1318
final int id;
1419
final int value;
@@ -28,10 +33,45 @@ class SortingNotifier extends StateNotifier<SortingNotifierState> {
2833
SortingEnum _operation = SortingEnum.none;
2934
CancelableOperation<void>? _cancelableBubbleSort;
3035

36+
static const List<SortingAlgorithm> widthButtons = [
37+
SortingAlgorithm.bubble,
38+
SortingAlgorithm.selection,
39+
SortingAlgorithm.merge,
40+
SortingAlgorithm.quick,
41+
];
42+
43+
static const List<SortingAlgorithm> sortingAlgorithms = [
44+
SortingAlgorithm.bubble,
45+
SortingAlgorithm.selection,
46+
SortingAlgorithm.merge,
47+
SortingAlgorithm.quick,
48+
];
49+
50+
void selectAlgorithm(int index) {
51+
final target = sortingAlgorithms[index];
52+
final selected = [...state.selectedAlgorithms];
53+
final targetIndex = selected.indexOf(target);
54+
55+
if (targetIndex != -1) {
56+
selected.removeAt(targetIndex);
57+
} else {
58+
selected.add(target);
59+
}
60+
state = state.copyWith(selectedAlgorithms: selected);
61+
}
62+
3163
static List<SortableItem> generateList() {
3264
return List.generate(_listSize, (index) => SortableItem(index, index + 1))..shuffle();
3365
}
3466

67+
static double calculateButtonWidth(int index) {
68+
double width = 0;
69+
for (int i = 0; i <= index; i++) {
70+
width += sortingAlgorithms[index].name.length *5;
71+
}
72+
return width;
73+
}
74+
3575
static double calculateItemWidth(BuildContext context) {
3676
final screenWidth = MediaQuery.of(context).size.width;
3777
final availableWidth = screenWidth - (itemsPadding * (_listSize - 1));

lib/features/sorting/view_model/sorting_state.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ part of 'sorting_notifier.dart';
33
class SortingNotifierState {
44
final List<SortableItem> list;
55
final Map<int, Offset> positions;
6+
final List<SortingAlgorithm> selectedAlgorithms;
67

7-
SortingNotifierState({required this.list, this.positions = const {}});
8+
SortingNotifierState({required this.list, this.positions = const {}, this.selectedAlgorithms = const []});
89

910
SortingNotifierState copyWith({
1011
List<SortableItem>? list,
1112
Map<int, Offset>? positions,
13+
List<SortingAlgorithm>? selectedAlgorithms,
1214
}) {
1315
return SortingNotifierState(
1416
list: list ?? this.list,
1517
positions: positions ?? this.positions,
18+
selectedAlgorithms: selectedAlgorithms ?? this.selectedAlgorithms,
1619
);
1720
}
1821
}

pubspec.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,10 @@ packages:
441441
dependency: transitive
442442
description:
443443
name: vm_service
444-
sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc
444+
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
445445
url: "https://pub.dev"
446446
source: hosted
447-
version: "14.2.4"
447+
version: "14.2.5"
448448
web:
449449
dependency: transitive
450450
description:

0 commit comments

Comments
 (0)