/*
 * @author lsy
 * @date   2019-11-05
 **/
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:gmalpha_flutter/res/value/ALColors.dart';

import 'HomeModel.dart';

class HomePage extends StatefulWidget {
  HomeModel _model;

  HomePage() {
    _model = new HomeModel();
  }

  @override
  State<StatefulWidget> createState() => HomeState(_model);
}

class HomeState extends State<HomePage> {
  HomeModel _model;

  HomeState(this._model);

  @override
  void initState() {
    super.initState();
    _model.init();
  }

  @override
  Widget build(BuildContext context) {
    var width = MediaQuery.of(context).size.width;
    return Scaffold(
//        bottomNavigationBar: StreamBuilder<int>(
//            stream: _model.indexLive.stream,
//            initialData: _model.indexLive.data,
//            builder: (con, data) {
//              int index = data.data ?? 0;
//              return BottomNavigationBar(
//                  items: _model.items,
//                  currentIndex: index,
//                  onTap: (index) {
//                    _model.onTap(index);
//                  });
//            }),
        body: StreamBuilder<int>(
            stream: _model.widgetLive.stream,
            initialData: _model.widgetLive.data,
            builder: (con, data) {
              int index = data.data ?? 0;
              List<Widget> tabList = new List();
              _model.items.forEach((value) {
                tabList.add(baseItem(value.index, value.svgIcon));
              });
              return Column(
                children: <Widget>[
                  Expanded(
                      child: IndexedStack(
                    index: index,
                    children: _model.pages,
                  )),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: tabList,
                  )
                ],
              );
            }));
  }

  Widget baseItem(int index, String pic) {
    return Expanded(
        child: GestureDetector(
            onTap: () {
              _model.currentIndex = index;
              _model.onTap(index);
            },
            child: Container(
                height: 56,
                alignment: Alignment.center,
                child: _model.currentIndex == index
                    ? SvgPicture.asset(pic, color: ALColors.Color323232)
                    : SvgPicture.asset(pic, color: ALColors.Color999999))));
  }

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