Skip to content

Commit bfa890b

Browse files
committed
Bug Fix
1 parent 6e6f7dd commit bfa890b

1 file changed

Lines changed: 43 additions & 46 deletions

File tree

lib/src/simulations/bubble_sort.dart

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class _BubbleSortBarsState extends State<BubbleSortBars> {
1515
List<int> _elements = [];
1616
int i = 0, counter = 0;
1717
int n = 5;
18-
int delay = 500, delay2 = 500;
18+
int delay = 1000;
1919
bool animating = false;
2020
bool sorted = false;
2121
double barwidth = 0;
@@ -39,6 +39,7 @@ class _BubbleSortBarsState extends State<BubbleSortBars> {
3939

4040
@override
4141
dispose() {
42+
animating = false;
4243
SystemChrome.setPreferredOrientations([
4344
DeviceOrientation.portraitUp,
4445
DeviceOrientation.portraitDown,
@@ -66,45 +67,47 @@ class _BubbleSortBarsState extends State<BubbleSortBars> {
6667
}
6768

6869
nextStep() async {
69-
await Future.delayed(Duration(milliseconds: delay));
70-
setState(() {
71-
barColor.clear();
72-
for (int j = 0; j < _elements.length; j++) {
73-
if (n == 1)
74-
barColor.add(Colors.greenAccent[400]);
75-
else
76-
barColor.add(Theme.of(context).primaryColor);
77-
if (resetIndex) _index[j] = j;
78-
}
79-
if (n == 1) {
80-
animating = false;
81-
return;
82-
}
83-
counter++;
84-
if (i == n - 1) {
85-
i = 0;
86-
n--;
87-
}
88-
barColor[i] = Colors.blue;
89-
if (_elements[i] > _elements[i + 1]) {
90-
if (resetIndex) {
91-
resetIndex = false;
70+
await Future.delayed(Duration(milliseconds: 3 * delay));
71+
if (this.mounted) {
72+
setState(() {
73+
barColor.clear();
74+
for (int j = 0; j < _elements.length; j++) {
75+
if (n == 1)
76+
barColor.add(Colors.greenAccent[400]);
77+
else
78+
barColor.add(Theme.of(context).primaryColor);
79+
if (resetIndex) _index[j] = j;
80+
}
81+
if (n == 1) {
82+
animating = false;
83+
return;
84+
}
85+
counter++;
86+
if (i == n - 1) {
87+
i = 0;
88+
n--;
89+
}
90+
barColor[i] = Colors.blue;
91+
if (_elements[i] > _elements[i + 1]) {
92+
if (resetIndex) {
93+
resetIndex = false;
94+
} else {
95+
barColor[i] = Colors.red;
96+
barColor[i + 1] = Colors.red;
97+
final temp = _index[i];
98+
_index[i] = _index[i + 1];
99+
_index[i + 1] = temp;
100+
final tmp = _elements[i];
101+
_elements[i] = _elements[i + 1];
102+
_elements[i + 1] = tmp;
103+
i++;
104+
resetIndex = true;
105+
}
92106
} else {
93-
barColor[i] = Colors.red;
94-
barColor[i + 1] = Colors.red;
95-
final temp = _index[i];
96-
_index[i] = _index[i + 1];
97-
_index[i + 1] = temp;
98-
final tmp = _elements[i];
99-
_elements[i] = _elements[i + 1];
100-
_elements[i + 1] = tmp;
101107
i++;
102-
resetIndex = true;
103108
}
104-
} else {
105-
i++;
106-
}
107-
});
109+
});
110+
}
108111
}
109112

110113
@override
@@ -224,20 +227,14 @@ class _BubbleSortBarsState extends State<BubbleSortBars> {
224227
inactiveColor: Colors.grey,
225228
onChanged: (value) {
226229
setState(() {
227-
delay2 = value.toInt();
228-
});
229-
},
230-
onChangeEnd: (value) {
231-
setState(() {
232-
refresh = false;
233230
delay = value.toInt();
234231
});
235232
},
236-
value: delay2.roundToDouble(),
233+
value: delay.roundToDouble(),
237234
),
238235
Center(
239236
child: Text(
240-
"Delay: ${delay2 / 1000.toInt()} s",
237+
"Delay: ${delay / 1000.toInt()} s",
241238
style: TextStyle(
242239
fontSize: 18,
243240
fontFamily: 'Ubuntu',
@@ -256,7 +253,7 @@ class _BubbleSortBarsState extends State<BubbleSortBars> {
256253
children: <Widget>[
257254
for (var k = 0; k < _elements.length; k++)
258255
AnimatedPositioned(
259-
duration: Duration(milliseconds: delay),
256+
duration: Duration(milliseconds: 2 * delay),
260257
left: _index[k] * barwidth +
261258
((_index[k] + 1) * barwidth / (_numberOfElements + 1)),
262259
curve: Curves.elasticOut,

0 commit comments

Comments
 (0)