NadAudioPlayer

mail@pastecode.io avatar
unknown
dart
2 years ago
9.6 kB
1
Indexable
Never
import 'package:base_architecture/core/extensions/widget_extensions.dart';
import 'package:base_architecture/presentation/notifiers/book_view_notifier.dart';
import 'package:base_architecture/presentation/notifiers/play_list_notifier.dart/playlist_notifier.dart';
import 'package:base_architecture/presentation/notifiers/theme_notifier.dart';
import 'package:base_architecture/presentation/pages/main/main_page.dart';
import 'package:base_architecture/presentation/props/book_info_props.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';
import 'dart:ui' as ui;

class NadAudioPlayer extends StatefulWidget {
  const NadAudioPlayer({
    Key? key,
    this.props,
  }) : super(key: key);

  final BookInfoProps? props;

  @override
  State<NadAudioPlayer> createState() => _NadAudioPlayerState();
}

class _NadAudioPlayerState extends State<NadAudioPlayer> {
  BookViewNotifier bookViewNotifier = Provider.of<BookViewNotifier>(
    navigatorKey.currentContext!,
    listen: true,
  );
  PlayListNotifier playListNotifier = Provider.of<PlayListNotifier>(
    navigatorKey.currentContext!,
    listen: true,
  );
  @override
  void dispose() {
    playListNotifier.disposePlayer();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    ThemeNotifier themeNotifier = Provider.of<ThemeNotifier>(
      context,
      listen: true,
    );
    PlayListNotifier playListNotifier = Provider.of<PlayListNotifier>(
      context,
      listen: true,
    );
    int toLibrary;
    ui.TextDirection direction = ui.TextDirection.ltr;
    String currentPosition =
        (playListNotifier.position).toString().split('.').first;
    String duration = (playListNotifier.duration)
        // .toStringAsFixed(2)
        .toString()
        .split('.')
        .first;
    return Container(
      margin: EdgeInsets.symmetric(
        horizontal: 40,
        vertical: playListNotifier.isDrive
            ? MediaQuery.of(context).size.height * 0.03
            : 0,
      ),
      child: Column(
        children: [
          SizedBox(
            height: 10,
            child: SliderTheme(
              data: SliderThemeData(
                activeTrackColor: themeNotifier.getTheme().canvasColor,
                inactiveTrackColor: themeNotifier.getTheme().hintColor,
                thumbColor: themeNotifier.getTheme().canvasColor,
                activeTickMarkColor: Colors.transparent,
                disabledActiveTickMarkColor: Colors.transparent,
                trackHeight: 2,
                overlayColor: Colors.transparent,
                thumbShape: const RoundSliderThumbShape(enabledThumbRadius: 4),
              ),
              child: Directionality(
                textDirection: direction,
                child: Slider(
                  value: playListNotifier.position.inSeconds.toDouble(),
                  min: 0,
                  max: playListNotifier.duration.inSeconds.toDouble(),
                  onChanged: (value) {
                    // print(duration.toString());

                    playListNotifier.handleSeek(value);
                  },
                ),
              ),
            ),
          ),
          Padding(
            padding: EdgeInsetsDirectional.only(
              start: 20,
              end: 20,
              bottom: MediaQuery.of(context).size.height * 0.04,
            ),
            child: Directionality(
              textDirection: direction,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    currentPosition,
                    style: themeNotifier
                        .getTheme()
                        .textTheme
                        .headline1!
                        .copyWith(color: themeNotifier.getTheme().focusColor),
                  ),
                  Text(
                    duration,
                    style: themeNotifier
                        .getTheme()
                        .textTheme
                        .headline1!
                        .copyWith(color: themeNotifier.getTheme().focusColor),
                  ),
                ],
              ),
            ),
          ),
          Visibility(
            visible: !playListNotifier.isDrive,
            child: Directionality(
              textDirection: direction,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  InkWell(
                    onTap: () {
                      playListNotifier.seek(
                        const Duration(
                          seconds: -15,
                        ),
                      );
                    },
                    child: Semantics(
                      label: 'backward player 15 seconds'.tr(),
                      child: SvgPicture.asset(
                        'assets/svg/decrease.svg',
                        width: MediaQuery.of(context).size.width * 0.1,
                        color: themeNotifier.getTheme().focusColor,
                        excludeFromSemantics: true,
                      ),
                    ),
                  ),
                  _playButton(context),
                  InkWell(
                    onTap: () {
                      if (playListNotifier.duration !=
                              playListNotifier.position &&
                          playListNotifier.duration - Duration(seconds: 15) >
                              playListNotifier.position)
                        playListNotifier.seek(
                          const Duration(
                            seconds: 15,
                          ),
                        );
                    },
                    child: Semantics(
                      label: 'forward player 15 seconds'.tr(),
                      child: SvgPicture.asset(
                        'assets/svg/increase.svg',
                        width: MediaQuery.of(context).size.width * 0.1,
                        color: themeNotifier.getTheme().focusColor,
                        excludeFromSemantics: true,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          Visibility(
            visible: playListNotifier.isDrive,
            child: Directionality(
              textDirection: direction,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Semantics(
                    container: true,
                    button: true,
                    label: 'backward player 15 seconds'.tr(),
                    child: InkWell(
                      onTap: () {
                        playListNotifier.seek(
                          const Duration(
                            seconds: -15,
                          ),
                        );
                      },
                      child: SvgPicture.asset(
                        'assets/svg/decrease.svg',
                        width: 67,
                        color: themeNotifier.getTheme().focusColor,
                        excludeFromSemantics: true,
                      ),
                    ),
                  ),
                  Semantics(
                    container: true,
                    button: true,
                    label: 'forward player 15 seconds'.tr(),
                    child: InkWell(
                      onTap: () {
                        if (playListNotifier.duration !=
                                playListNotifier.position &&
                            playListNotifier.duration - Duration(seconds: 15) >
                                playListNotifier.position)
                          playListNotifier.seek(
                            const Duration(
                              seconds: 15,
                            ),
                          );
                      },
                      child: SvgPicture.asset(
                        'assets/svg/increase.svg',
                        width: 67,
                        color: themeNotifier.getTheme().focusColor,
                        excludeFromSemantics: true,
                      ),
                    ),
                  ),
                ],
              ).padding(
                padding: const EdgeInsets.symmetric(
                  horizontal: 40,
                  vertical: 30,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  InkWell _playButton(BuildContext context) {
    return InkWell(
      onTap: () {
        String url = bookViewNotifier.bookDetails.data?.first
                .soundFiles?[playListNotifier.listIndex!].fullPathFile ??
            'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3';
        playListNotifier.play(
          url,
        );
      },
      child: Semantics(
        label: playListNotifier.isPlaying ? 'pause'.tr() : 'play'.tr(),
        excludeSemantics: true,
        child: SvgPicture.asset(
          playListNotifier.isPlaying
              ? 'assets/svg/pause_audio.svg'
              : 'assets/svg/play_audio.svg',
          width: 75,
          excludeFromSemantics: true,
        ),
      ),
    );
  }
}