import 'package:flutter/cupertino.dart';

/// 由于Getx的Controller用了单例，导致无法监听到组件的生命周期，所以用一个StatefulWidget来获取Widget的生命周期
class DelegateLifecycleWidget extends StatefulWidget {
  final Widget child;

  final Function? onCreateCallback;
  final Function? onDestroyCallback;

  const DelegateLifecycleWidget({
    super.key,
    required this.child,
    this.onCreateCallback,
    this.onDestroyCallback,
  });

  @override
  State<StatefulWidget> createState() {
    return _DelegateLifecycleWidgetState();
  }
}

class _DelegateLifecycleWidgetState extends State<DelegateLifecycleWidget> {
  @override
  void initState() {
    super.initState();
    if (widget.onCreateCallback != null) {
      widget.onCreateCallback!();
    }
  }

  @override
  void dispose() {
    if (widget.onDestroyCallback != null) {
      widget.onDestroyCallback!();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}
