import 'dart:convert' as convert;
import 'dart:ui';

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

class NormalNavDelegate extends SliverPersistentHeaderDelegate {
  final Map<String, dynamic> data;
  double _height;
  Color _bgColor;
  String _title;
  Color _titleColor;

  NormalNavDelegate(this.data) : super() {
    _height = MediaQueryData.fromWindow(window).padding.top + 44;

    String d = data['data'];
    Map<String, dynamic> json =
        Map<String, dynamic>.from(convert.jsonDecode(d));

    if (json.containsKey('app_bar_bg_color')) {
      _bgColor = HexColor.fromHex(json['app_bar_bg_color'] ?? '');
    }
    if (json.containsKey('app_bar_name')) {
      _title = json['app_bar_name'] ?? '';
    }
    if (json.containsKey('app_bar_name')) {
      _titleColor = HexColor.fromHex(json['app_bar_name_color']);
    }
  }

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    print('${shrinkOffset.toString()}');
    double percent = shrinkOffset / _height;
    print('${percent.toString()}');
    return NormalNav(_title, _titleColor,
        _bgColor?.withOpacity(percent) ?? Colors.transparent);
  }

  @override
  double get maxExtent => _height;

  @override
  double get minExtent => _height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) =>
      false; // 如果内容需要更新，设置为true
}

class NormalNav extends StatelessWidget {
  final String title;
  final Color titleColor;
  final Color color;

  NormalNav(this.title, this.titleColor, this.color);

  @override
  Widget build(BuildContext context) {
    return Container(
        color: color,
        child: Column(
          children: <Widget>[
            Expanded(child: Container()),
            Container(
              width: double.infinity,
              height: 44,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Navigator.of(context).canPop()
                      ? GestureDetector(
                          child: Container(
                            width: 44,
                            height: 44,
                            child: Icon(
                              Icons.arrow_back_ios,
                              color: Colors.white,
                            ),
                          ),
                          onTap: () {
                            if (Navigator.canPop(context)) {
                              Navigator.pop(context);
                            }
                          },
                        )
                      : Container(
                          width: 44,
                          height: 44,
                        ),
                  Expanded(
                    child: Container(
                      child: Center(
                        child: Text(
                          title ?? '',
                          style: TextStyle(
                              fontSize: 18, color: titleColor ?? Colors.white),
                        ),
                      ),
                    ),
                  ),
                  GestureDetector(
                    child: Container(
                      width: 44,
                      height: 44,
                      // child: Icon(
                      //   Icons.settings,
                      //   color: Colors.white,
                      // ),
                    ),
                    onTap: () {
                      // Navigator.push(context,
                      //     CupertinoPageRoute(builder: (context) {
                      //   return MinePage();
                      // }));
                    },
                  ),
                ],
              ),
            )
          ],
        ));
  }
}
