Skip to content

Commit 92f0b4f

Browse files
committed
Add audio bubble appearance animation
1 parent a601602 commit 92f0b4f

File tree

6 files changed

+35
-39
lines changed

6 files changed

+35
-39
lines changed

lib/src/audio_list.dart

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:io';
22

3+
import 'package:audio_chat/src/audio_state.dart';
34
import 'package:audio_chat/src/globals.dart';
45
import 'package:audio_chat/src/widgets/audio_bubble.dart';
56
import 'package:flutter/material.dart';
@@ -14,38 +15,24 @@ class AudioList extends StatefulWidget {
1415
class _AudioListState extends State<AudioList> {
1516
List<FileSystemEntity> data = [];
1617

18+
@override
19+
void initState() {
20+
super.initState();
21+
}
22+
1723
@override
1824
Widget build(BuildContext context) {
19-
return FutureBuilder<List<FileSystemEntity>>(
20-
key: const ValueKey("futureBuilder"),
21-
future: fetchAudioFiles(),
22-
initialData: data,
23-
builder: (context, AsyncSnapshot<List<FileSystemEntity>> snapshot) {
24-
if (snapshot.connectionState == ConnectionState.active ||
25-
snapshot.connectionState == ConnectionState.waiting) {
26-
return const Center(
27-
child: CircularProgressIndicator(),
28-
);
29-
}
30-
if (snapshot.hasData) {
31-
data = snapshot.data!;
32-
return ListView.builder(
33-
key: const ValueKey("ListBuilder"),
34-
reverse: true,
35-
padding: const EdgeInsets.symmetric(vertical: 15),
36-
itemCount: snapshot.data?.length,
37-
itemBuilder: (BuildContext context, int index) {
38-
return AudioBubble(
39-
filepath: snapshot.data![index].path,
40-
key: ValueKey(snapshot.data![index].path),
41-
);
42-
},
43-
);
44-
} else {
45-
return const Center(
46-
child: Text("No Audio files"),
47-
);
48-
}
25+
return AnimatedList(
26+
padding: const EdgeInsets.symmetric(vertical: 15),
27+
key: Globals.audioListKey,
28+
itemBuilder: (context, index, animation) {
29+
return FadeTransition(
30+
opacity: animation,
31+
child: AudioBubble(
32+
filepath: AudioState.files[index],
33+
key: ValueKey(AudioState.files[index]),
34+
),
35+
);
4936
},
5037
);
5138
}

lib/src/audio_state.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// Choosing this method because using proper state management would be an
2+
/// overkill for the scope of this project.
3+
class AudioState {
4+
AudioState._();
5+
static List<String> files = [];
6+
}

lib/src/globals.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ class Globals {
1111
static const double borderRadius = 27;
1212
static const double defaultPadding = 8;
1313
static String documentPath = '';
14-
static GlobalKey audioListKey = GlobalKey();
14+
static GlobalKey<AnimatedListState> audioListKey =
15+
GlobalKey<AnimatedListState>();
1516
}

lib/src/home_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class _HomePageState extends State<HomePage>
4242
padding: const EdgeInsets.all(Globals.defaultPadding),
4343
child: Column(
4444
children: [
45-
Expanded(child: AudioList(key: Globals.audioListKey)),
45+
const Expanded(child: AudioList()),
4646
Row(
4747
mainAxisSize: MainAxisSize.max,
4848
children: [

lib/src/widgets/audio_bubble.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@ class _AudioBubbleState extends State<AudioBubble> {
1818
@override
1919
void initState() {
2020
super.initState();
21-
}
22-
23-
@override
24-
void didChangeDependencies() async {
25-
super.didChangeDependencies();
2621
player.setFilePath(widget.filepath).then((value) {
2722
setState(() {
2823
duration = value;
29-
debugPrint("File Duration: ${duration!.inMilliseconds}");
3024
});
3125
});
3226
}
3327

28+
@override
29+
void didChangeDependencies() async {
30+
super.didChangeDependencies();
31+
}
32+
3433
@override
3534
Widget build(BuildContext context) {
3635
return Padding(

lib/src/widgets/record_button.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:async';
22
import 'dart:io';
33

4+
import 'package:audio_chat/src/audio_state.dart';
45
import 'package:audio_chat/src/globals.dart';
56
import 'package:audio_chat/src/widgets/flow_shader.dart';
67
import 'package:flutter/material.dart';
@@ -187,7 +188,9 @@ class _RecordButtonState extends State<RecordButton> {
187188
debugPrint("Locked recording");
188189
} else {
189190
var filePath = await Record().stop();
190-
Globals.audioListKey.currentState!.setState(() {});
191+
AudioState.files.add(filePath!);
192+
Globals.audioListKey.currentState!
193+
.insertItem(AudioState.files.length - 1);
191194
debugPrint(filePath);
192195
}
193196
},

0 commit comments

Comments
 (0)