Skip to content

Instantly share code, notes, and snippets.

@ghulamostafa
Created April 20, 2021 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghulamostafa/0f93c4a9ac21e5bea6c68391fe25507b to your computer and use it in GitHub Desktop.
Save ghulamostafa/0f93c4a9ac21e5bea6c68391fe25507b to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class ChatConversationScreen extends StatefulWidget {
@override
_ChatConversationScreenState createState() => _ChatConversationScreenState();
}
class _ChatConversationScreenState extends State<ChatConversationScreen> {
var currentAudioIndex = -1; //The index of the currently playing audio. By default it is -1
var myAudioList = <AudioMessageClass>[
AudioMessageClass(isPlaying: false, audioPath: "Path 1"),
AudioMessageClass(isPlaying: false, audioPath: "Path 2"),
AudioMessageClass(isPlaying: false, audioPath: "Path 3"),
AudioMessageClass(isPlaying: false, audioPath: "Path 4"),
AudioMessageClass(isPlaying: false, audioPath: "Path 5"),
];
@override
Widget build(BuildContext context) {
//Make a duplicate of the myAudioList which will be used to reset the list later on the application
var myStoppedAudioList = List
.generate(
myAudioList.length,
(index) => (AudioMessageClass(
isPlaying: false,
audioPath: myAudioList[index].audioPath)
)
);
return Scaffold(
appBar: AppBar(),
body: Column(
children: [
Text('Current Audio is:' + (currentAudioIndex == -1 ? "NOT PLAYING" : myAudioList[currentAudioIndex].audioPath)),
Expanded(
child: ListView.builder(
itemCount: myAudioList.length,
itemBuilder: (context, index){
return Switch(value: myAudioList[index].isPlaying, onChanged: (val){
setState(() {
//This will clear the values of current list
myAudioList.setAll(0, myStoppedAudioList);
myAudioList[index].isPlaying = val;
currentAudioIndex = !val ? -1 : index;
});
});
}
),
)
],
),
);
}
}
class AudioMessageClass{
bool isPlaying;
String audioPath;
AudioMessageClass({this.isPlaying = false, this.audioPath});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment