import 'dart:async';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:zhiying_base_widget/pages/main_page/main_page_bloc.dart';
import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_bg_notifier.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import '';

class MineHeaderBgWidget extends StatefulWidget {
   ScrollController controller;
   Widget bgWidget;

   MineHeaderBgWidget({Key key, this.controller, this.bgWidget}) : super(key: key);

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

class _MineHeaderBgWidgetState extends State<MineHeaderBgWidget> {
  int zIndex = 0;
  double _offsetY = 0;
  double _height = 300;
  @override
  void initState() {
    super.initState();


    if (widget.controller != null) {
      widget.controller.addListener(() {
        if (widget.controller.offset > _height) {
          // 减少重绘
          return;
        }
        _offsetY = -widget.controller.offset;
        if (_offsetY > 0) {
          _offsetY = 0;
        }
        setState(() {});
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Transform.translate(
      offset: Offset(0, _offsetY),
      child: _MineHeaderBgContainer(bgWidget: widget?.bgWidget),
    );
  }
}

class _MineHeaderBgContainer extends StatefulWidget {
   Widget bgWidget;

   _MineHeaderBgContainer({Key key, this.bgWidget}) : super(key: key);

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

class _MineHeaderBgContainerState extends State<_MineHeaderBgContainer> {

  @override
  Widget build(BuildContext context) {
    print('_MineHeaderBgContainerState build');
    if (widget.bgWidget != null) {
      return widget.bgWidget;
    }
    return Consumer<MainPageBgNotifier>(builder: (context, model, widget) {
      return Stack(
        fit: StackFit.passthrough,
        overflow: Overflow.clip,
        children: <Widget>[
          model.toWidget ?? Container(),
        ],
      );
    });
  }
}
