import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:netrain_flutter_app/common/AppColors.dart';
import 'package:netrain_flutter_app/common/Images.dart';

class DoctorHomePage extends StatefulWidget {
  @override
  _DoctorHomePageState createState() => _DoctorHomePageState();
}

class _DoctorHomePageState extends State<DoctorHomePage> {
  final _scrollController = ScrollController();
  var isShowBlackTitle = false;

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      print("华东 ${_scrollController.offset}");
      if (_scrollController.offset > 270 && !isShowBlackTitle) {
        setState(() {
          isShowBlackTitle = true;
        });
      } else if (_scrollController.offset < 270 && isShowBlackTitle) {
        setState(() {
          isShowBlackTitle = false;
        });
      }
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        physics: BouncingScrollPhysics(),
        controller: _scrollController,
        slivers: [
          SliverAppBar(
            titleSpacing: 16,
            toolbarHeight: 50,
            automaticallyImplyLeading: false,
            pinned: true,
            floating: false,
            flexibleSpace: FlexibleSpaceBar(
              collapseMode: CollapseMode.pin,
              background: Container(
                color: Color(AppColors.mainColor),
                child: Stack(
                  children: [
                    Positioned(
                      child: ClipOval(
                        //圆形裁剪
                        child: Container(
                          height: 56,
                          width: 56,
                          constraints:
                              BoxConstraints(maxHeight: 100, maxWidth: 100),
                          foregroundDecoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(50),
                              border: Border.all(
                                  color: Colors.white,
                                  width: 2,
                                  style: BorderStyle.solid)),
                          child: Image.network(
                            'http://pix2.tvzhe.com/thumb/star/60/87/260x346.jpg',
                            height: 56,
                            width: 56,
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                      top: 106,
                      left: 18,
                    ),
                    Positioned(
                      child: Text(
                        "愚鱼余",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 18,
                            fontWeight: FontWeight.bold),
                      ),
                      top: 106,
                      left: 82,
                    ),
                    Positioned(
                      child: Text(
                        "主任医师",
                        style: TextStyle(color: Colors.white, fontSize: 12),
                      ),
                      top: 111,
                      left: 144,
                    ),
                    Positioned(
                      child: Container(
                        width: 68,
                        height: 20,
                        decoration: BoxDecoration(
                            color: Colors.white30,
                            borderRadius: BorderRadius.circular(10)),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text(
                              "浏览主页",
                              style: TextStyle(
                                height: 1.1,
                                  color: Colors.white,
                                  fontSize: 11
                              ),
                            ),
                            Image.asset(Images.ic_more_white_little)
                          ],
                        ),
                      ),
                      top: 138,
                      left: 82,
                    ),
                    Positioned(
                      child: Image.asset(
                        Images.ic_code,
                        height: 30,
                        width: 30,
                      ),
                      top: 117,
                      right: 16,
                    ),
                    Positioned(
                      child: Container(
                        child: _getStatisticsView(),
                        width: ScreenUtil.getScreenW(context),
                      ),
                      top: 185,
                    ),
                    Positioned(
                      child: Container(
                        child: Flex(
                          direction: Axis.horizontal,
                          children: [
                            _getTab("药房续方", "12"),
                            _getTab("等待接诊", "10"),
                          ],
                        ),
                        width: ScreenUtil.getScreenW(context),
                      ),
                      top: 253,
                      left: 8,
                      right: 8,
                    ),
                  ],
                ),
              ),
            ),
            expandedHeight: 322,
            title: Container(
              child: Text(
                "我的工作站",
                style: TextStyle(
                    fontSize: 21,
                    fontWeight: FontWeight.bold,
                    color: isShowBlackTitle ? Colors.black : Colors.white),
              ),
            ),
          ),
          SliverToBoxAdapter(
            child: Container(
              margin: EdgeInsets.only(top: 16, bottom: 16),
              child: Wrap(
                spacing: 8,
                children: [
                  Image.asset(Images.ic_home_list),
                  Text(
                    "咨询列表",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        color: Color(AppColors.EditTextColor),
                        fontSize: 18,
                        fontWeight: FontWeight.bold),
                  ),
                ],
              ),
            ),
          ),
          _getChatList(),
        ],
      ),
    );
  }

  List<String> list = List.generate(10, (index) => "马云 $index");
  var showEmpty = false;

  /// 咨询列表
  Widget _getChatList() {
    return list.isEmpty
        ? _getEmptyView()
        : SliverList(
            delegate:
                SliverChildBuilderDelegate((BuildContext context, int index) {
            return Dismissible(
              background: Container(
                alignment: Alignment.centerRight,
                padding: EdgeInsets.only(right: 16),
                color: Colors.red,
                child: Text(
                  "删除",
                  style: TextStyle(color: Colors.white),
                  textAlign: TextAlign.center,
                ),
              ),
              key: Key("${list[index]}"),
              onDismissed: (direction) {
                setState(() {
                  print(
                      "删除成功？  ${list.remove(list[index])},  ${list.toString()}");
                });
                ScaffoldMessenger.of(context)
                    .showSnackBar(SnackBar(content: Text("删除了 $index")));
              },
              child: Container(
                width: ScreenUtil.getScreenW(context),
                height: 82,
                child: Stack(
                  children: [
                    Positioned(
                      child: Image.asset(
                        Images.ic_doctor_avatar,
                        width: 50,
                        height: 50,
                      ),
                      top: 16,
                      left: 5,
                    ),
                    Positioned(
                      child: Text(
                        "${list[index]}",
                        style: TextStyle(
                            color: Color(AppColors.EditTextColor),
                            fontWeight: FontWeight.bold,
                            fontSize: 16),
                      ),
                      top: 16,
                      left: 66,
                    ),
                    Positioned(
                      width: ScreenUtil.getScreenW(context) * 0.7,
                      child: Text(
                        "感冒发烧流鼻涕，建议您多喝热水建议您多喝热水建议您多喝热水建议您多喝热水建议您多喝热",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                            color: Color(AppColors.color_999), fontSize: 14),
                      ),
                      bottom: 18,
                      left: 66,
                    ),
                    Positioned(
                      child: Text(
                        "刚刚",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                            color: Color(AppColors.hintText), fontSize: 12),
                      ),
                      top: 16,
                      right: 16,
                    ),
                    Positioned(
                      child: Container(
                        margin: EdgeInsets.only(left: 16, right: 16),
                        width: ScreenUtil.getScreenW(context) - 32,
                        height: 1,
                        color: Color(AppColors.dividerColor),
                      ),
                      bottom: 0,
                    )
                  ],
                ),
              ),
            );
          }, childCount: list.length));
  }

  /// 咨询列表的空数据视图
  Widget _getEmptyView() {
    return SliverList(
        delegate: SliverChildListDelegate([
      Image.asset(
        Images.img_blank_nomessage,
        fit: BoxFit.none,
      ),
      Text(
        "您暂时没有新消息",
        style: TextStyle(
          fontSize: 14,
          color: Color(AppColors.text_default_color),
        ),
        textAlign: TextAlign.center,
      )
    ]));
  }

  /// 中间的医生统计数据
  Widget _getStatisticsView() {
    return Flex(
      direction: Axis.horizontal,
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Expanded(
          child: getStatisticsChild("123", "患者人数"),
          flex: 1,
        ),
        Expanded(
          child: getStatisticsChild("537", "累计处方次数"),
          flex: 1,
        ),
        Expanded(
          child: getStatisticsChild("189", "累计问诊次数"),
          flex: 1,
        ),
        // getStatisticsChild("537", "累计处方次数"),
        // getStatisticsChild("189", "累计问诊次数"),
      ],
    );
  }

  Column getStatisticsChild(String num, String name) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          num,
          style: TextStyle(
              color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold),
        ),
        Text(
          name,
          style: TextStyle(color: Color(0xccffffff), fontSize: 11),
        ),
      ],
    );
  }

  /// 药房续方和等待接诊
  Widget _getTab(String name, String num) {
    return Expanded(
      flex: 1,
      child: Container(
        margin: EdgeInsets.only(left: 8, right: 8),
        child: Stack(
          children: [
            Image.asset(Images.img_home_pharmacy),
            Positioned(
              child: Text(
                name,
                style: TextStyle(
                    color: Color(AppColors.mainColor),
                    fontSize: 14,
                    fontWeight: FontWeight.bold),
              ),
              top: 10,
              left: 10,
            ),
            Positioned(
              child: Text(
                num,
                style: TextStyle(
                    color: Color(AppColors.mainColor),
                    fontSize: 36,
                    fontWeight: FontWeight.bold),
              ),
              bottom: 0,
              left: 10,
            ),
          ],
        ),
      ),
    );
  }
}
