mirrored_repositories/VocabApp
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:my_vocab/Presentation/Screens/favorites_screen/screen.dart';
import 'package:my_vocab/Presentation/Screens/history-screen/history_screen.dart';
import 'package:my_vocab/Presentation/Screens/home-screen/screen.dart';
import 'package:my_vocab/Presentation/Screens/settings/settings_screen.dart';
import 'package:my_vocab/model/enum/api_request_status.dart';
import 'package:my_vocab/providers/home_provider.dart';
import 'package:provider/provider.dart';

// This is main screen of the app. It contains all screens in a PageView and
// uses BottomNavBar to toggle among them

class MainScreen extends StatefulWidget {
  static const id = 'MainScreen';
  @override
  _MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen>
    with SingleTickerProviderStateMixin {
  final double max = 200.0;
  AnimationController animationController;
  Animation finalAnimation;
  bool isSettingsVisible = false;

  PageController _pageController;
  int _page = 0;

  int lastPageIndex = 0;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: 0);
    animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 200),
    );
    finalAnimation = CurvedAnimation(
      parent: animationController,
      curve: Curves.decelerate,
    );
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: finalAnimation,
      builder: (context, child) {
        var slide = finalAnimation.value * max as double;
        var scale = 1 - (finalAnimation.value * 0.1 as double);
        return Stack(
          children: [
            Scaffold(
              backgroundColor: const Color(0xff0C120C),
              body: Transform(
                transform: Matrix4.identity()..translate(-slide),
                child: Transform.translate(
                  offset: const Offset(200, 0),
                  child: Container(
                    alignment: Alignment.centerRight,
                    child: SettingsScreen(
                      callBackFunction: toggleSettings,
                    ),
                  ),
                ),
              ),
            ),
            Transform(
              transform: Matrix4.identity()
                ..translate(-slide)
                ..scale(scale),
              alignment: Alignment.centerLeft,
              child: Consumer(
                builder: (BuildContext context, HomeProvider homeProvider,
                        Widget child) =>
                    Container(
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(20)),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(
                      20 * (finalAnimation.value as double),
                    ),
                    child: Scaffold(
                      backgroundColor: Colors.transparent,
                      body: PageView(
                        physics: const NeverScrollableScrollPhysics(),
                        controller: _pageController,
                        onPageChanged: (currentPageIndex) {
                          setState(() {
                            _page = currentPageIndex;
                          });
                        },
                        children: [
                          HomeScreen(),
                          HistoryScreen(),
                          FavScreen(),
                          const SettingsScreen(),
                        ],
                      ),
                      bottomNavigationBar: (homeProvider.apiRequestStatus ==
                              ApiRequestStatus.loaded)
                          ? BottomNavigationBar(
                              type: BottomNavigationBarType.fixed,
                              selectedItemColor: Theme.of(context).primaryColor,
                              currentIndex: _page,
                              onTap: (pageIndex) {
                                if (pageIndex == 3) {
                                  toggleSettings();
                                } else {
                                  _pageController.animateToPage(
                                    pageIndex,
                                    curve: Curves.bounceIn,
                                    duration: const Duration(milliseconds: 100),
                                  );
                                }
                              },
                              items: [
                                BottomNavigationBarItem(
                                  icon: const Icon(Icons.home),
                                  label: tr('home_bottom_nav'),
                                ),
                                BottomNavigationBarItem(
                                  icon: const Icon(Icons.history),
                                  label: tr('history_bottom_nav'),
                                ),
                                BottomNavigationBarItem(
                                  icon: const Icon(Icons.favorite),
                                  label: tr('fav_bottom_nav'),
                                ),
                                BottomNavigationBarItem(
                                  icon: const Icon(Icons.settings),
                                  label: tr('settings_bottom_nav'),
                                ),
                              ],
                            )
                          : null,
                    ),
                  ),
                ),
              ),
            ),
          ],
        );
      },
    );
  }

  // to keep track of correct tab which was present before moving to settings
  // and show correct bar in bottomNavBar
  void toggleSettings() {
    if (animationController.status == AnimationStatus.completed) {
      animationController.reverse();
    } else {
      animationController.forward();
    }
    isSettingsVisible = !isSettingsVisible;
    if (isSettingsVisible) {
      lastPageIndex = _page;
      _page = 3;
    } else {
      _page = lastPageIndex;
    }
  }
}
