Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(example): add and remove player actions #1394

Merged
merged 3 commits into from
Jan 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}