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';
import 'package:netrain_flutter_app/common/Ext.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(() {
      if (_scrollController.offset > 302.dp() && !isShowBlackTitle) {
        setState(() {
          isShowBlackTitle = true;
        });
      } else if (_scrollController.offset < 302.dp() && 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.dp(),
            toolbarHeight: 50.dp(),
            automaticallyImplyLeading: false,
            pinned: true,
            floating: false,
            // snap: true,
            stretch: true,
            title: Container(
              child: Text(
                "我的工作站",
                style: TextStyle(
                    fontSize: 21,
                    fontWeight: FontWeight.bold,
                    color: isShowBlackTitle ? Colors.black : Colors.white),
              ),
            ),
            expandedHeight: 352.dp() - ScreenUtil.getStatusBarH(context),
            flexibleSpace: FlexibleSpaceBar(
              collapseMode: CollapseMode.pin,
              // 拉伸的样式
              stretchModes: [StretchMode.blurBackground,StretchMode.zoomBackground],
              background: Container(
                decoration: BoxDecoration(
                    gradient: LinearGradient(
                        colors: [Color(0xff379BFF), Color(0xff0B7CEE)])),
                child: Stack(
                  children: [
                    Positioned(
                      child: ClipOval(
                        //圆形裁剪
                        child: Container(
                          height: 56.dp(),
                          width: 56.dp(),
                          constraints:
                              BoxConstraints(maxHeight: 100.dp(), maxWidth: 100.dp()),
                          foregroundDecoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(50.dp()),
                              border: Border.all(
                                  color: Colors.white,
                                  width: 2.dp(),
                                  style: BorderStyle.solid)),
                          child: Image.network(
                            'http://pix2.tvzhe.com/thumb/star/60/87/260x346.jpg',
                            height: 56.dp(),
                            width: 56.dp(),
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                      top: 106.dp(),
                      left: 18.dp(),
                    ),
                    Positioned(
                      child: Text(
                        "愚鱼余",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 18,
                            fontWeight: FontWeight.bold),
                      ),
                      top: 106.dp(),
                      left: 82.dp(),
                    ),
                    Positioned(
                      child: Text(
                        "主任医师",
                        style: TextStyle(color: Colors.white, fontSize: 12),
                      ),
                      top: 111.dp(),
                      left: 144.dp(),
                    ),
                    Positioned(
                      child: Container(
                        width: 68.dp(),
                        height: 20.dp(),
                        decoration: BoxDecoration(
                            color: Colors.white30,
                            borderRadius: BorderRadius.circular(10.dp())),
                        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.dp(),
                      left: 82.dp(),
                    ),
                    Positioned(
                      child: Image.asset(
                        Images.ic_code,
                        height: 30.dp(),
                        width: 30.dp(),
                      ),
                      top: 117.dp(),
                      right: 16.dp(),
                    ),
                    Positioned(
                      child: Container(
                        child: _getStatisticsView(),
                        width: ScreenUtil.getScreenW(context),
                      ),
                      top: 185.dp(),
                    ),
                    Positioned(
                      child: Container(
                        padding: EdgeInsets.only(left: 8.dp(),right: 8.dp()),
                        child: Flex(
                          direction: Axis.horizontal,
                          children: [
                            _getTab("药房续方", "12"),
                            _getTab("等待接诊", "10"),
                          ],
                        ),
                        width: ScreenUtil.getScreenW(context),
                      ),
                      top: 253.dp(),
                    ),
                  ],
                ),
              ),
            ),
          ),
          SliverToBoxAdapter(
            child: Container(
              margin: EdgeInsets.only(top: 16.dp(), bottom: 16.dp()),
              child: Wrap(
                spacing: 8.dp(),
                children: [
                  Image.asset(Images.ic_home_list),
                  Text(
                    "咨询列表",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        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.dp()),
                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.dp(),
                child: Stack(
                  children: [
                    Positioned(
                      child: Image.asset(
                        Images.ic_doctor_avatar,
                        width: 50.dp(),
                        height: 50.dp(),
                      ),
                      top: 16.dp(),
                      left: 5.dp(),
                    ),
                    Positioned(
                      child: Text(
                        "${list[index]}",
                        style: TextStyle(
                            color: AppColors.EditTextColor,
                            fontWeight: FontWeight.bold,
                            fontSize: 16),
                      ),
                      top: 16.dp(),
                      left: 66.dp(),
                    ),
                    Positioned(
                      width: ScreenUtil.getScreenW(context) * 0.7,
                      child: Text(
                        "感冒发烧流鼻涕，建议您多喝热水建议您多喝热水建议您多喝热水建议您多喝热水建议您多喝热",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                            color: AppColors.color_999, fontSize: 14),
                      ),
                      bottom: 18.dp(),
                      left: 66.dp(),
                    ),
                    Positioned(
                      child: Text(
                        "刚刚",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                            color: AppColors.hintText, fontSize: 12),
                      ),
                      top: 16.dp(),
                      right: 16.dp(),
                    ),
                    Positioned(
                      child: Container(
                        margin: EdgeInsets.only(left: 16.dp(), right: 16.dp()),
                        width: ScreenUtil.getScreenW(context) - 32,
                        height: 1,
                        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: 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(
        height: 83.dp(),
        margin: 8.spacingHorizontal(),
        child: Stack(
          children: [
            Image.asset(Images.img_home_pharmacy),
            Positioned(
              child: Text(
                name,
                style: TextStyle(
                    color: AppColors.mainColor,
                    fontSize: 14,
                    fontWeight: FontWeight.bold),
              ),
              top: 10.dp(),
              left: 10.dp(),
            ),
            Positioned(
              child: Text(
                num,
                style: TextStyle(
                    color: AppColors.mainColor,
                    fontSize: 36,
                    fontWeight: FontWeight.bold),
              ),
              bottom: 0,
              left: 10.dp(),
            ),
          ],
        ),
      ),
    );
  }
}
