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

class MineHeaderBgWidget extends StatefulWidget {
  final ScrollController controller;

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

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

class _MineHeaderBgWidgetState extends State<MineHeaderBgWidget> {
  int zIndex = 0;
  double _offsetY = 0;
  double _height = 300;
  _MineHeaderBgContainer _content;

  @override
  void initState() {
    super.initState();
    _content = _MineHeaderBgContainer();

    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: _content,
    );
  }
}

class _MineHeaderBgContainer extends StatefulWidget {
  @override
  _MineHeaderBgContainerState createState() => _MineHeaderBgContainerState();
}

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