import 'package:flutter/material.dart';

/// Helps to create a scrollable layout with two widgets in a scrollable column
/// and space between them
class ScrollLayoutHelper extends StatelessWidget {
  final Widget upperWidget;
  final Widget lowerWidget;
  final EdgeInsets padding;

  final ScrollController? controller;

  final Future Function()? onRefresh;

  const ScrollLayoutHelper(
      {super.key,
        required this.upperWidget,
        required this.lowerWidget,
        this.padding = const EdgeInsets.symmetric(horizontal: 16),
        this.controller,
        this.onRefresh});

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        Widget content = SingleChildScrollView(
          physics:
          onRefresh != null ? const AlwaysScrollableScrollPhysics() : null,
          controller: controller,
          child: ConstrainedBox(
            constraints: BoxConstraints(
              minHeight: constraints.maxHeight,
            ),
            child: SafeArea(
              child: Padding(
                padding: padding,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    upperWidget,
                    lowerWidget,
                  ],
                ),
              ),
            ),
          ),
        );

        if (onRefresh != null) {
          content = RefreshIndicator(
            onRefresh: () => onRefresh!.call(),
            child: content,
          );
        }

        return content;
      },
    );
  }
}
