import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/home/activity_list_page.dart';
import 'package:huixiang/home/home_page.dart';
import 'package:huixiang/home/points_mall_page.dart';
import 'package:huixiang/main.dart';
import 'package:huixiang/utils/event_type.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:huixiang/view_widget/my_tab.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MainHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MainHomePage();
  }
}

class _MainHomePage extends State<MainHomePage>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  TabController tabcontroller;
  List<Widget> _widgetOptions;

  @override
  void initState() {
    super.initState();
    if (tabcontroller == null)
      tabcontroller = TabController(length: 3, vsync: this);

    _widgetOptions = <Widget>[
      // HomePage(() {
      //   setState(() {
      //     tabcontroller.index = 2;
      //   });
      // }),
      ActivityListPage(),
      PointsMallPage()
    ];

    eventBus.on<EventType>().listen((event) {
      print("object: MainHomePage");
      if (event.type < 3) {
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        appBar: MyAppBar(
          background: Color(0xFFFAFAFA),
          toolbarHeight: 50.h,
          leading: false,
          brightness: Brightness.light,
          action: Container(
            margin: EdgeInsets.only(right: 8.w),
            child: GestureDetector(
              onTap: () {
                Navigator.of(context).pushNamed('/router/system_msg_page');
              },
              child: Image.asset(
                "assets/image/icon_notices.webp",
                width: 24,
                height: 24,
              ),
            ),
          ),
          titleChild: PreferredSize(
            preferredSize: Size(double.infinity, 38.h),
            child: TabBar(
              controller: tabcontroller,
              automaticIndicatorColorAdjustment: true,
              isScrollable: true,
              indicatorWeight: 2,
              indicatorColor: Color(0xFF39B54A),
              labelPadding: EdgeInsets.only(left: 8.w, right: 8.w),
              indicatorSize: TabBarIndicatorSize.label,
              unselectedLabelStyle: TextStyle(
                fontSize: 15.sp,
                fontWeight: FontWeight.w400,
              ),
              labelStyle: TextStyle(
                color: Colors.black,
                fontSize: 18.sp,
                fontWeight: FontWeight.bold,
              ),
              labelColor: Colors.black,
              tabs: [
                MyTab(text: S.of(context).shouye),
                MyTab(text: S.of(context).huodongliebiao),
                MyTab(text: S.of(context).jifenshangcheng),
              ],
            ),
          ),
        ),
        body: Container(
          padding: EdgeInsets.only(bottom: 76.h),
          child: TabBarView(
            physics: BouncingScrollPhysics(),
            children: _widgetOptions,
            controller: tabcontroller,
          ),
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}