Skip to content
Snippets Groups Projects
homepage.dart 8.33 KiB
Newer Older
  • Learn to ignore specific revisions
  • import 'package:ambient/widgets/MusicPlayerState.dart';
    
    import 'package:flutter/material.dart';
    
    
    import 'package:flutter/services.dart';
    import 'package:spotify_sdk/models/connection_status.dart';
    
    import 'package:spotify_sdk/models/track.dart';
    
    import 'package:spotify_sdk/spotify_sdk.dart';
    import 'dart:developer' as developer;
    
    void printErr(String msg) {
      developer.log(msg);
    }
    
      final pageController;
      const HomePage({super.key, this.pageController});
    
    
      @override
      Widget build(BuildContext context) {
    
        return StreamBuilder<ConnectionStatus>(
          stream: SpotifySdk.subscribeConnectionStatus(),
          builder: (context, snapshot) {
            var data = snapshot.data;
            if (data != null) {
    
              MusicPlayerState
                  .of(context)
                  .connected = data.connected;
    
              stream: MusicPlayerState.of(context).rebuildStream.stream,
    
              builder: (context, _) => player(context),
    
      Widget player(BuildContext context) {
        return Stack(children: [
    
    Erik Hinkelmanns's avatar
    Erik Hinkelmanns committed
          Center(child: ListView(
            shrinkWrap: true,
    
            padding: const EdgeInsets.all(8),
            children: [
              MusicPlayerState.of(context).connected
    
                  ? _buildPlayerStateWidget(context)
    
                  : Center(
    
    Erik Hinkelmanns's avatar
    Erik Hinkelmanns committed
                child: MaterialButton(
                  minWidth: 200,
                  height: 200,
                  color: Colors.blue,
                  textColor: Colors.white,
                  shape: const CircleBorder(),
                  child: const Icon(
                    Icons.connected_tv,
                    size: 50,
                  ),
                  onPressed: () async {
    
                    await MusicPlayerState.of(context).connectToSpotifyRemote();
    
    Erik Hinkelmanns's avatar
    Erik Hinkelmanns committed
                    buildSnackbar(context);
                  },
                ),
              ),
    
              IconButton(
                icon: const Icon(Icons.info),
                iconSize: 50,
                color: primaryColor,
                onPressed: () => checkIfAppIsActive(context),
              ),
              MusicPlayerState.of(context).loading
                  ? Container(
    
    Erik Hinkelmanns's avatar
    Erik Hinkelmanns committed
                  color: Colors.black12,
                  child: const Center(child: CircularProgressIndicator()))
    
                  : const SizedBox(),
            ],
    
    Erik Hinkelmanns's avatar
    Erik Hinkelmanns committed
          ),),
    
    Daniel Furaev's avatar
    Daniel Furaev committed
    
    
      Widget _buildPlayerStateWidget(BuildContext context) {
        Track? track;
        if(MusicPlayerState.of(context).playerState == null){
          return const Text("PlayState is Null");
        } else {
          track = MusicPlayerState.of(context).playerState!.track;
        }
        if (MusicPlayerState.of(context).playerState?.track == null) {
          return Center(
            child: MaterialButton(
              minWidth: 200,
              height: 200,
              color: Colors.blue,
              textColor: Colors.white,
              shape: const CircleBorder(),
    
                  pageController.animateToPage(2,
                    duration: const Duration(milliseconds: 500),
                    curve: Curves.ease);
                  //_title = _titleList[2];
    
              child: const Icon(
                Icons.play_arrow_rounded,
                size: 50,
              ),
            ),
          );
        } else {
          MusicPlayerState.of(context).currentTrackImageUri = track!.imageUri;
          return Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text('''
    
                ${track.name}
                ${track.artist.name}
                ${track.album.name}
                ''',
    
                  maxLines: 20,
                  style: const TextStyle(
                      fontSize: 16.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.black)),
              MusicPlayerState
                  .of(context)
                  .connected
                  ? MusicPlayerState.of(context).spotifyImageWidget()
                  : const Text('Connect to see an image...'),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  IconButton(
                    iconSize: 50.0,
                    icon: const Padding(
                        padding: EdgeInsets.zero,
                        child: Icon(Icons.skip_previous_outlined)),
                    onPressed: skipPrevious,
                  ),
    
                  StreamBuilder<void>(
                    stream: MusicPlayerState.of(context).playStatedStream.stream,
                    builder: (context, _) => ControlButtons(),
                  ),
    
                  IconButton(
                    iconSize: 50.0,
                    icon: const Padding(
                        padding: EdgeInsets.zero,
                        child: Icon(Icons.skip_next_outlined)),
                    onPressed: skipNext,
                  ),
                ],
              ),
            ],
          );
        }
    
    Daniel Furaev's avatar
    Daniel Furaev committed
    
    
    Erik Hinkelmanns's avatar
    Erik Hinkelmanns committed
      //Todo Daniel
    
    Erik Hinkelmanns's avatar
    Erik Hinkelmanns committed
      Future<void> queue(String songId) async {
    
    Erik Hinkelmanns's avatar
    Erik Hinkelmanns committed
              spotifyUri: 'spotify:track:$songId');
    
        } on PlatformException catch (e) {
          setStatus(e.code, message: e.message);
        } on MissingPluginException {
          setStatus('not implemented');
        }
      }
    
    
    Erik Hinkelmanns's avatar
    Erik Hinkelmanns committed
      //Todo Daniel
      Future<void> play(String songId) async {
    
    Erik Hinkelmanns's avatar
    Erik Hinkelmanns committed
          await SpotifySdk.play(spotifyUri: 'spotify:track:$songId');
    
        } on PlatformException catch (e) {
          setStatus(e.code, message: e.message);
        } on MissingPluginException {
          setStatus('not implemented');
        }
      }
    
      Future<void> pause() async {
        try {
          await SpotifySdk.pause();
        } on PlatformException catch (e) {
          setStatus(e.code, message: e.message);
        } on MissingPluginException {
          setStatus('not implemented');
        }
      }
    
      Future<void> resume() async {
        try {
          await SpotifySdk.resume();
        } on PlatformException catch (e) {
          setStatus(e.code, message: e.message);
        } on MissingPluginException {
          setStatus('not implemented');
        }
      }
    
    Daniel Furaev's avatar
    Daniel Furaev committed
    
    
      Future<void> skipNext() async {
        try {
          await SpotifySdk.skipNext();
        } on PlatformException catch (e) {
          setStatus(e.code, message: e.message);
        } on MissingPluginException {
          setStatus('not implemented');
        }
      }
    
      Future<void> skipPrevious() async {
        try {
          await SpotifySdk.skipPrevious();
        } on PlatformException catch (e) {
          setStatus(e.code, message: e.message);
        } on MissingPluginException {
          setStatus('not implemented');
        }
      }
    
      Future<void> checkIfAppIsActive(BuildContext context) async {
        try {
          var isActive = await SpotifySdk.isSpotifyAppActive;
          final snackBar = SnackBar(
              content: Text(isActive
                  ? 'Spotify app connection is active (currently playing)'
                  : 'Spotify app connection is not active (currently not playing)'));
          ScaffoldMessenger.of(context).showSnackBar(snackBar);
        } on PlatformException catch (e) {
          setStatus(e.code, message: e.message);
        } on MissingPluginException {
          setStatus('not implemented');
        }
      }
    
    Daniel Furaev's avatar
    Daniel Furaev committed
    
    
      void setStatus(String code, {String? message}) {
        var text = message ?? '';
        printErr(text);
      }
    
    
      void buildSnackbar(BuildContext context) {
        final snackBar = SnackBar(
    
            content: Text(MusicPlayerState
                .of(context)
                .connected
    
                ? 'connect to spotify successful'
                : 'connect to spotify failed'));
    
        ScaffoldMessenger.of(context).showSnackBar(snackBar);
      }
    
    
    }
    
    class ControlButtons extends StatelessWidget {
      const ControlButtons({
        Key? key,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
    
        if (MusicPlayerState
            .of(context)
            .playerState == null) {
    
          return const Text("PlayState is Null");
        } else {
    
          if (MusicPlayerState
              .of(context)
              .playerState!
              .isPaused) {
    
            return IconButton(
                icon: const Icon(Icons.play_arrow_outlined),
                iconSize: 50,
                onPressed: () {
                  var state = MusicPlayerState.of(context);
                  SpotifySdk.resume()
                      .then((value) => state.updatePlayerState());
                });
          } else {
            return IconButton(
              icon: const Icon(Icons.pause_outlined),
              iconSize: 50,
              color: primaryColor,
              onPressed: () {
                var state = MusicPlayerState.of(context);
                SpotifySdk.pause()
                    .then((value) => state.updatePlayerState());
              },
            );
          }
        }
      }