import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:flutter/material.dart';
import 'dart:ui';

class HomeSreachDeleagater extends SliverPersistentHeaderDelegate{

  double _height;

  HomeSreachDeleagater() : super() {
    _height = MediaQueryData.fromWindow(window).padding.top + 44;
  }

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    print('${shrinkOffset.toString()}');
    double percent = shrinkOffset / _height;
    print('${percent.toString()}');
    return HomeSreachWidget(null);
  }

  @override
  double get maxExtent => _height;

  @override
  double get minExtent => _height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }

}


///
/// 首页搜索框
///
class HomeSreachWidget extends StatelessWidget {
  final Map<String, dynamic> model;

  const HomeSreachWidget(this.model);

  @override
  Widget build(BuildContext context) {
    return HomeSreachContainer();
  }
}

class HomeSreachContainer extends StatefulWidget {
  @override
  _HomeSreachContainerState createState() => _HomeSreachContainerState();
}

class _HomeSreachContainerState extends State<HomeSreachContainer> {
  @override
  Widget build(BuildContext context) {
    return _getMainWidget();
  }

  /// 主视图
  Widget _getMainWidget() {
    return Container(
      color: Colors.transparent,
      height: 30,
      width: double.infinity,
      margin: EdgeInsets.only(left: 12.5, right: 12.5, bottom: 10, top: MediaQueryData.fromWindow(window).padding.top + 10),
      child: Row(
        children: <Widget>[
          /// 搜索框
          Expanded(
            child: _getSreachWidget(),
          ),
          const SizedBox(width: 10),

          /// 消息
          _getMessageWidget(),
        ],
      ),
    );
  }

  /// 搜索栏
  Widget _getSreachWidget() {
    return Container(
      height: 30,
      width: double.infinity,
      child: TextField(
        autofocus: false,
        style: TextStyle(color: HexColor.fromHex('#FFFFFF'), fontSize: 14),
        readOnly: true,
        decoration: InputDecoration(
          hintText: '输入搜索内容，领券省钱',
          hintStyle: TextStyle(
            color: Colors.white,
            fontSize: 14,
          ),
          contentPadding: EdgeInsets.zero,
          prefixIcon: Icon(
            Icons.search,
            color: Colors.white,
          ),
          filled: true,
          fillColor: Color(0x50cccccc),
          focusedBorder: OutlineInputBorder(borderSide: BorderSide(color: Color(0x00000000)), borderRadius: BorderRadius.all(Radius.circular(30))),
          enabledBorder: OutlineInputBorder(borderSide: BorderSide(color: Color(0x00000000)), borderRadius: BorderRadius.all(Radius.circular(30))),
        ),
      ),
    );
  }

  /// 消息widget
  Widget _getMessageWidget() {
    return Container(
      width: 30,
      height: 30,
      color: Colors.red,
    );
  }
}
