Skip to content

Commit

Permalink
feat(example): add and remove player actions (#1394)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 authored Jan 21, 2023
1 parent 002e2fc commit f06cab9
Showing 1 changed file with 104 additions and 52 deletions.
156 changes: 104 additions & 52 deletions packages/audioplayers/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:audioplayers_example/tabs/streams.dart';
import 'package:audioplayers_example/utils.dart';
import 'package:flutter/material.dart';

const playerCount = 4;
const defaultPlayerCount = 4;

typedef OnError = void Function(Exception exception);

Expand All @@ -29,7 +29,7 @@ class ExampleApp extends StatefulWidget {

class _ExampleAppState extends State<ExampleApp> {
List<AudioPlayer> audioPlayers = List.generate(
playerCount,
defaultPlayerCount,
(_) => AudioPlayer()..setReleaseMode(ReleaseMode.stop),
);
int selectedPlayerIdx = 0;
Expand Down Expand Up @@ -80,75 +80,127 @@ class _ExampleAppState extends State<ExampleApp> {
super.dispose();
}

void _handleAction(PopupAction value) {
switch (value) {
case PopupAction.add:
setState(() {
audioPlayers.add(AudioPlayer()..setReleaseMode(ReleaseMode.stop));
});
break;
case PopupAction.remove:
setState(() {
if (audioPlayers.isNotEmpty) {
selectedAudioPlayer.stop();
selectedAudioPlayer.release();
audioPlayers.removeAt(selectedPlayerIdx);
}
// Adjust index to be in valid range
if (audioPlayers.isEmpty) {
selectedPlayerIdx = 0;
} else if (selectedPlayerIdx >= audioPlayers.length) {
selectedPlayerIdx = audioPlayers.length - 1;
}
});
break;
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('audioplayers example'),
actions: [
PopupMenuButton<PopupAction>(
onSelected: _handleAction,
itemBuilder: (BuildContext context) {
return PopupAction.values.map((PopupAction choice) {
return PopupMenuItem<PopupAction>(
value: choice,
child: Text(
choice == PopupAction.add
? 'Add player'
: 'Remove selected player',
),
);
}).toList();
},
),
],
),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Tgl(
key: const Key('playerTgl'),
options: [for (var i = 1; i <= audioPlayers.length; i++) i]
.asMap()
.map((key, val) => MapEntry('player-$key', 'P$val')),
selected: selectedPlayerIdx,
onChange: (v) => setState(() => selectedPlayerIdx = v),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Tgl(
key: const Key('playerTgl'),
options: [for (var i = 1; i <= audioPlayers.length; i++) i]
.asMap()
.map((key, val) => MapEntry('player-$key', 'P$val')),
selected: selectedPlayerIdx,
onChange: (v) => setState(() => selectedPlayerIdx = v),
),
),
),
),
Expanded(
child: IndexedStack2(
index: selectedPlayerIdx,
children: audioPlayers
.map(
(player) => Tabs(
tabs: [
TabData(
key: 'sourcesTab',
label: 'Src',
content: SourcesTab(
player: player,
),
),
TabData(
key: 'controlsTab',
label: 'Ctrl',
content: ControlsTab(
player: player,
child: audioPlayers.isEmpty
? const Text('No AudioPlayer available!')
: IndexedStack2(
index: selectedPlayerIdx,
children: audioPlayers
.map(
(player) => Tabs(
tabs: [
TabData(
key: 'sourcesTab',
label: 'Src',
content: SourcesTab(
player: player,
),
),
TabData(
key: 'controlsTab',
label: 'Ctrl',
content: ControlsTab(
player: player,
),
),
TabData(
key: 'streamsTab',
label: 'Stream',
content: StreamsTab(
player: player,
),
),
TabData(
key: 'audioContextTab',
label: 'Ctx',
content: AudioContextTab(
player: player,
),
),
TabData(
key: 'loggerTab',
label: 'Log',
content: const LoggerTab(),
),
],
),
),
TabData(
key: 'streamsTab',
label: 'Stream',
content: StreamsTab(
player: player,
),
),
TabData(
key: 'audioContextTab',
label: 'Ctx',
content: AudioContextTab(
player: player,
),
),
TabData(
key: 'loggerTab',
label: 'Log',
content: const LoggerTab(),
),
],
),
)
.toList(),
),
)
.toList(),
),
),
],
),
);
}
}

enum PopupAction {
add,
remove,
}

0 comments on commit f06cab9

Please sign in to comment.