import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:news_getx/modules/category/category_page.dart';
import 'package:news_getx/modules/main/main_page.dart';
import 'package:news_getx/modules/widgets/app_bar.dart';
import 'package:news_getx/theme/app_colors.dart';

import 'application_controller.dart';

class ApplicationPage extends GetView<ApplicationController> {
  const ApplicationPage({Key? key}) : super(key: key);

  AppBar _buildAppBar() {
    return transparentAppBar(
      title: Obx(
        () => Text(
          controller.tabTitles[controller.page],
          style: TextStyle(
            color: AppColors.primaryText,
            fontFamily: "Montserrat",
            fontSize: 18.sp,
            fontWeight: FontWeight.w600,
          ),
        ),
      ),
      actions: <Widget>[
        IconButton(
          onPressed: () {},
          icon: Icon(
            Icons.search,
            color: AppColors.primaryText,
          ),
        )
      ],
    );
  }

  Widget _buildPageView() {
    return PageView(
      // 禁止滚动
      physics: NeverScrollableScrollPhysics(),
      controller: controller.pageController,
      onPageChanged: controller.handlePageChange,
      children: <Widget>[
        MainPage(),
        CategoryPage(),
        Text('BookmarksPage'),
        Text('AccountPage'),
      ],
    );
  }

  Widget _buildBottomNavigationBar() {
    return Obx(
      () => BottomNavigationBar(
        items: controller.bottomTabs,
        currentIndex: controller.page,
        // 固定类型 导航栏中的所有项都会显示在屏幕上，无论有多少项，它们的宽度都会平均分配
        type: BottomNavigationBarType.fixed,
        onTap: controller.handleNavBarTap,
        showSelectedLabels: false,
        showUnselectedLabels: false,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xfff6f6f6),
      appBar: _buildAppBar(),
      body: _buildPageView(),
      bottomNavigationBar: _buildBottomNavigationBar(),
    );
  }
}
