MyPlayList

 avatar
unknown
dart
3 years ago
21 kB
3
Indexable
import 'package:base_architecture/core/shared_widgets/bookmark_dialog.dart';
import 'package:base_architecture/core/shared_widgets/login_dialog.dart';
import 'package:base_architecture/presentation/notifiers/auth_notifier/auth_notifier.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/audio_player/widgets/nad_audio_player.dart';
import 'package:base_architecture/presentation/pages/audio_player/widgets/player_menu.dart';
import 'package:base_architecture/presentation/pages/audio_player/widgets/player_seasons.dart';
import 'package:base_architecture/presentation/pages/audio_player/widgets/player_speed_dialog.dart';
import 'package:base_architecture/presentation/pages/audio_player/widgets/player_timer_dialog.dart';
import 'package:base_architecture/presentation/pages/audio_player/widgets/playlist_bootom_item.dart';
import 'package:base_architecture/presentation/pages/main/main_page.dart';
import 'package:base_architecture/presentation/props/book_info_props.dart';
import 'package:base_architecture/presentation/props/bottom_item_props.dart';
import 'package:card_swiper/card_swiper.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';
import 'dart:ui' as ui;

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

  final BookInfoProps? props;

  @override
  State<MyPlayList> createState() => _MyPlayListState();
}

class _MyPlayListState extends State<MyPlayList> {
  BookViewNotifier bookViewNotifier = Provider.of<BookViewNotifier>(
    navigatorKey.currentContext!,
    listen: true,
  );
  ThemeNotifier themeNotifier = Provider.of<ThemeNotifier>(
    navigatorKey.currentContext!,
    listen: true,
  );
  PlayListNotifier playListNotifier = Provider.of<PlayListNotifier>(
    navigatorKey.currentContext!,
    listen: true,
  );
  AuthNotifier authNotifier = Provider.of<AuthNotifier>(
    navigatorKey.currentContext!,
    listen: false,
  );

  ui.TextDirection direction = ui.TextDirection.ltr;
  @override
  Widget build(BuildContext context) {
    Size mediaQuery = MediaQuery.of(context).size;

    bool? isFav = widget.props?.bookInfo.data?.first.isFav ?? true;

    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            _buildFirstRow(playListNotifier, themeNotifier, context),
            _buildList(context, isFav, mediaQuery),
            _buildBookDetails(themeNotifier, playListNotifier),
            NadAudioPlayer(
              props: widget.props,
            ),
            _buildBottomNav(widget.props, authNotifier, playListNotifier),
          ],
        ),
      ),
    );
  }

  Widget _buildList(
    BuildContext context,
    bool? isFav,
    mediaQuery,
  ) =>
      Padding(
        padding: const EdgeInsets.symmetric(vertical: 15),
        child: SizedBox(
          height: playListNotifier.isDrive
              ? mediaQuery.height * 0.51
              : mediaQuery.height * 0.43,
          child: Directionality(
            textDirection: direction,
            child: Swiper(
              controller: playListNotifier.swiperController,
              itemCount:
                  widget.props?.bookInfo.data?.first.soundFiles?.length ?? 5,
              onIndexChanged: (int value) {
                if (playListNotifier.isPlaying) {
                  playListNotifier.changeIndex(value);
                  playListNotifier.pause();
                  playListNotifier.setSource(
                    bookViewNotifier.bookDetails.data?.first.soundFiles?[value]
                            .fullPathFile ??
                        'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3',
                  );
                  playListNotifier.didLog50 = false;
                  playListNotifier.didLog85 = false;
                } else {
                  playListNotifier.changeIndex(value);
                  playListNotifier.setSource(
                    bookViewNotifier.bookDetails.data?.first.soundFiles?[value]
                            .fullPathFile ??
                        'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3',
                  );
                  playListNotifier.didLog50 = false;
                  playListNotifier.didLog85 = false;
                }
              },
              viewportFraction: 0.77,
              scale: 0.8,
              loop: false,
              index: playListNotifier.listIndex,
              autoplay: false,
              itemBuilder: (BuildContext context, int index) => Stack(
                children: <Widget>[
                  Container(
                    padding: const EdgeInsets.symmetric(
                      vertical: 20,
                      horizontal: 20,
                    ),
                    height: mediaQuery.height * 0.43,
                    decoration: BoxDecoration(
                      color: themeNotifier.getTheme().focusColor,
                      borderRadius: const BorderRadius.all(
                        Radius.circular(10),
                      ),
                    ),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            _likeButton(
                              themeNotifier,
                              isFav,
                            ),
                          ],
                        ),
                        const SizedBox(height: 16),
                        _cardImage(),
                      ],
                    ),
                  ),
                  Visibility(
                    visible: playListNotifier.isDrive,
                    child: Align(
                      alignment: Alignment.bottomCenter,
                      child: InkWell(
                        child: SvgPicture.asset(
                          playListNotifier.isPlaying
                              ? 'assets/svg/pause_audio.svg'
                              : 'assets/svg/play_audio.svg',
                          width: 128,
                        ),
                        onTap: () {
                          String url = bookViewNotifier
                                  .bookDetails
                                  .data
                                  ?.first
                                  .soundFiles?[playListNotifier.listIndex!]
                                  .fullPathFile ??
                              'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3';
                          playListNotifier.play(
                            url,
                          );
                        },
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      );

  Widget _buildBottomNav(
    BookInfoProps? props,
    AuthNotifier authNotifier,
    PlayListNotifier playListNotifier,
  ) =>
      Expanded(
        child: Visibility(
          visible: !playListNotifier.isDrive,
          child: Align(
            alignment: Alignment.bottomCenter,
            child: Consumer<PlayListNotifier>(
              builder: (
                BuildContext context,
                PlayListNotifier notifier,
                Widget? child,
              ) =>
                  Container(
                padding: const EdgeInsets.symmetric(horizontal: 10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    BottomItemsList(
                      props: BottomItemProps(
                        name: 'Put a mark',
                        svgPath: 'assets/svg/save.svg',
                        isSpeed: false,
                        ontap: () {
                          if (authNotifier.appUser != null) {
                            showDialog(
                              context: context,
                              builder: ((BuildContext context) =>
                                  const BookMarkDialog(
                                    isAudio: true,
                                  )),
                            );
                          } else {
                            showDialog(
                              context: context,
                              builder: (BuildContext context) =>
                                  const LoginDialog(),
                            );
                          }
                        },
                      ),
                    ),
                    BottomItemsList(
                      props: BottomItemProps(
                        name: 'Seasons',
                        svgPath: 'assets/svg/b2.svg',
                        isSpeed: false,
                        ontap: () {
                          Navigator.of(
                            context,
                          ).push(
                            MaterialPageRoute<dynamic>(
                              builder: (BuildContext context) => PlayerSeasons(
                                soundFiles: widget
                                    .props!.bookInfo.data!.first.soundFiles!,
                              ),
                            ),
                          );
                        },
                      ),
                    ),
                    BottomItemsList(
                      props: BottomItemProps(
                        name: 'Speed',
                        svgPath: 'assets/svg/b3.svg',
                        isSpeed: true,
                        ontap: () {
                          showDialog(
                            context: context,
                            builder: ((BuildContext context) =>
                                const PlayerSpeedDialog()),
                          );
                        },
                      ),
                    ),
                    BottomItemsList(
                      props: BottomItemProps(
                        name: 'Timer',
                        svgPath: 'assets/svg/b4.svg',
                        isSpeed: false,
                        ontap: () {
                          showDialog(
                            context: context,
                            builder: ((BuildContext context) =>
                                const PlayerTimerDialog()),
                          );
                        },
                      ),
                    ),
                    BottomItemsList(
                      props: BottomItemProps(
                        name: 'More',
                        svgPath: 'assets/svg/b5.svg',
                        isSpeed: false,
                        ontap: () {
                          if (authNotifier.appUser != null) {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (BuildContext context) =>
                                    PlayerMenuPage(
                                  props: widget.props!,
                                ),
                              ),
                            );
                          } else {
                            showDialog(
                              context: context,
                              builder: (BuildContext context) =>
                                  const LoginDialog(),
                            );
                          }
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      );

  Widget _buildBookDetails(
    ThemeNotifier themeNotifier,
    PlayListNotifier playListNotifier,
  ) =>
      Consumer<PlayListNotifier>(
        builder:
            (BuildContext context, PlayListNotifier notifier, Widget? child) =>
                Visibility(
          visible: !notifier.isDrive,
          child: Padding(
            padding: notifier.isDrive
                ? const EdgeInsets.symmetric(horizontal: 50, vertical: 10)
                : EdgeInsets.only(
                    left: 50,
                    right: 50,
                    top: MediaQuery.of(context).size.height * 0.025,
                    bottom: MediaQuery.of(context).size.height * 0.035),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Semantics(
                  label: 'previous chapter'.tr(),
                  child: IconButton(
                    iconSize: 40,
                    highlightColor: Colors.transparent,
                    splashColor: Colors.transparent,
                    onPressed: () {
                      playListNotifier.swiperController.next();
                    },
                    icon: SvgPicture.asset(
                      context.locale == const Locale('ar')
                          ? 'assets/svg/next.svg'
                          : 'assets/svg/back.svg',
                      excludeFromSemantics: true,
                    ),
                  ),
                ),
                Expanded(
                  child: Column(
                    children: [
                      Semantics(
                        label: widget.props?.bookInfo.data?.first.name ??
                            'اسم الكتاب',
                        container: true,
                        excludeSemantics: true,
                        child: Text(
                          widget.props?.bookInfo.data?.first.name ??
                              'اسم الكتاب',
                          style: themeNotifier
                              .getTheme()
                              .textTheme
                              .headline5!
                              .copyWith(
                                fontSize: 23,
                              ),
                          textAlign: TextAlign.center,
                        ),
                      ),
                      Semantics(
                        label: widget
                                .props
                                ?.bookInfo
                                .data
                                ?.first
                                .soundFiles?[playListNotifier.listIndex!]
                                .name ??
                            'اسم الفصل',
                        container: true,
                        excludeSemantics: true,
                        child: Text(
                          widget
                                  .props
                                  ?.bookInfo
                                  .data
                                  ?.first
                                  .soundFiles?[playListNotifier.listIndex!]
                                  .name ??
                              'اسم الفصل',
                          style: themeNotifier
                              .getTheme()
                              .textTheme
                              .headline5!
                              .copyWith(fontSize: 16),
                          overflow: TextOverflow.ellipsis,
                          textAlign: TextAlign.center,
                        ),
                      ),
                    ],
                  ),
                ),
                Semantics(
                  label: 'next chapter'.tr(),
                  child: IconButton(
                    iconSize: 40,
                    highlightColor: Colors.transparent,
                    splashColor: Colors.transparent,
                    onPressed: () {
                      playListNotifier.swiperController.previous();
                    },
                    icon: SvgPicture.asset(
                      context.locale == const Locale('ar')
                          ? 'assets/svg/back.svg'
                          : 'assets/svg/next.svg',
                      excludeFromSemantics: true,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      );

  Widget _likeButton(
    ThemeNotifier themeNotifier,
    bool? isFav,
  ) =>
      Semantics(
        container: true,
        button: true,
        label: playListNotifier.isLiked ? 'like'.tr() : 'dislike'.tr(),
        excludeSemantics: true,
        child: Container(
          decoration: BoxDecoration(
            color: themeNotifier.getTheme().canvasColor,
            border: Border.all(
              color: themeNotifier.getTheme().canvasColor,
            ),
            borderRadius: BorderRadius.circular(10),
          ),
          child: IconButton(
            highlightColor: Colors.transparent,
            splashColor: Colors.transparent,
            onPressed: () {
              setState(() {
                bookViewNotifier.favouriteValue(isFav);
                bookViewNotifier.addToWhishListORFav(
                  widget.props!.bookInfo.data!.first.id,
                  'fav',
                );
              });
            },
            icon: !isFav!
                ? SvgPicture.asset('assets/svg/heart.svg')
                : SvgPicture.asset(
                    'assets/svg/filled_heart.svg',
                    excludeFromSemantics: true,
                  ),
          ),
        ),
      );

  Expanded _cardImage() => Expanded(
        child: Padding(
          padding: const EdgeInsets.only(bottom: 50),
          child: ClipRRect(
            borderRadius: const BorderRadius.all(
              Radius.circular(10),
            ),
            child: Image.network(
              widget.props?.bookInfo.data?.first.imageFullPath ??
                  'https://arabcast.nqa.nadsoft.co/storage/uploads/book/images/b0ad5193-6509-4271-8307-3fabaebaf529-39b90772-d999-4711-a989-cb99e47f9e2c-غلاف عوالم تحت الارض.png',
            ),
          ),
        ),
      );

  Widget _buildFirstRow(
    PlayListNotifier playListNotifier,
    ThemeNotifier themeNotifier,
    BuildContext context,
  ) =>
      Padding(
        padding: const EdgeInsets.only(left: 45, right: 45),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _buildBackButton(themeNotifier, context),
            _buildDrivingMode(playListNotifier, themeNotifier),
          ],
        ),
      );

  Widget _buildDrivingMode(
    PlayListNotifier playListNotifier,
    ThemeNotifier themeNotifier,
  ) =>
      InkWell(
        onTap: () {
          playListNotifier.setIsDrive();
        },
        child: Semantics(
          label: playListNotifier.isDrive
              ? 'Disable driving mode'.tr()
              : 'Enable driving mode'.tr(),
          excludeSemantics: true,
          child: Container(
            padding: const EdgeInsets.all(8),
            height: 50,
            decoration: BoxDecoration(
              color: themeNotifier.getTheme().shadowColor,
              border: Border.all(
                color: themeNotifier.getTheme().shadowColor,
              ),
              borderRadius: BorderRadius.circular(10),
            ),
            child: Row(
              children: [
                SvgPicture.asset(
                  'assets/svg/car.svg',
                  excludeFromSemantics: true,
                ),
                const SizedBox(width: 8),
                Text(
                  playListNotifier.isDrive
                      ? 'Disable driving mode'.tr()
                      : 'Enable driving mode'.tr(),
                  style: themeNotifier.getTheme().textTheme.headline2!.copyWith(
                        color: themeNotifier.getTheme().focusColor,
                      ),
                ),
              ],
            ),
          ),
        ),
      );

  Widget _buildBackButton(ThemeNotifier themeNotifier, BuildContext context) =>
      Semantics(
        label: 'back'.tr(),
        child: Container(
          decoration: BoxDecoration(
            color: themeNotifier.getTheme().primaryColor,
            border: Border.all(
              color: themeNotifier.getTheme().shadowColor,
            ),
            borderRadius: BorderRadius.circular(10),
          ),
          child: IconButton(
            highlightColor: Colors.transparent,
            splashColor: Colors.transparent,
            onPressed: () {
              Navigator.pop(context);
            },
            icon: SvgPicture.asset(
              'assets/svg/down_arrow.svg',
              excludeFromSemantics: true,
            ),
          ),
        ),
      );
}
Editor is loading...