import 'dart:convert';
import 'dart:ui';
import 'package:provider/provider.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/pages/goods_details_page/notifier/goods_details_appbar_color_notifier.dart';
import 'package:zhiying_base_widget/widgets/goods_details/appbar/model/goods_details_appbar_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class GoodsDetailsAppBarWidget extends StatefulWidget {
  final Map<String, dynamic> data;
  GoodsDetailsAppBarModel model;

  GoodsDetailsAppBarWidget(this.data, {Key key}) : super(key: key){
    try{
      model = GoodsDetailsAppBarModel.fromJson(jsonDecode(data['data']));
    }catch(e, s){
      Logger.error(e, s);
    }
  }

  @override
  _GoodsDetailsAppBarWidgetState createState() => _GoodsDetailsAppBarWidgetState();
}

class _GoodsDetailsAppBarWidgetState extends State<GoodsDetailsAppBarWidget> {



  Color _starColor;
  Color _endBgColor = HexColor.fromHex('#FF4242');
  Color _endTextColor = HexColor.fromHex('#FFFFFF');

  Color _bgColor = Colors.transparent;
  Color _textColor = Colors.transparent;

  @override
  void initState() {
    _starColor = Colors.transparent;
    _endTextColor = HexColor.fromHex(widget?.model?.appBarNameColor ?? '#FFFFFF' );
    _endBgColor = HexColor.fromHex(widget?.model?.appBarBgColor ?? '#FF4242');
    super.initState();
  }

  @override
  void didChangeDependencies() {
    GoodsDetailsAppBarColorNotifier notifier = Provider.of<GoodsDetailsAppBarColorNotifier>(context);
    if (null != notifier) {
      _bgColor = Color.lerp(_starColor, _endBgColor, notifier.percent);
      _textColor = Color.lerp(_starColor, _endTextColor, notifier.percent);
    }
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    // Logger.log('^-^');
    return Container(
      width: double.infinity,
      color: _bgColor,
      height: MediaQueryData.fromWindow(window).padding.top + 44,
      padding: EdgeInsets.only(top: MediaQueryData.fromWindow(window).padding.top),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          GestureDetector(
            onTap: ()=> Navigator.maybePop(context),
            child: Padding(
                padding: const EdgeInsets.only(left: 12.5),
                child: SizedBox(
                  width: 25 + 12.5,
                  height: 25,
                  child: CachedNetworkImage(imageUrl: widget?.model?.appBarBackupIcon ?? '', ),
                )),
          ),
          Text(
            widget?.model?.appBarName ?? '商品详情',
            style: TextStyle(color: _textColor, fontSize: 15, fontWeight: FontWeight.bold),
          ),
          Padding(
              padding: const EdgeInsets.only(right: 12.5),
              child: SizedBox(
                width: 25 + 12.5,
                height: 25,
                child: Container(
                  height: double.infinity,
                  width: double.infinity,
                  color: Colors.transparent,
                ),
              )),
        ],
      ),
    );
  }
}
