import 'package:flutter/material.dart';

class TitledContainer extends StatelessWidget {
  final String title;
  final Widget child;
  final Widget? action;
  final double? height;
  final double? width;

  const TitledContainer({super.key, required this.title, required this.child, this.action, this.height, this.width});

  Widget _computeTitleRow() {
    List<Widget> children = [];
    children.add(Text(
      title,
      style: const TextStyle(
        fontWeight: FontWeight.w300,
        fontSize: 20,
      ),
    ));
    Widget? actionWidget = action;
    if (actionWidget != null) {
      children.add(actionWidget);
    }
    return Row(
      children: children
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: height,
      width: width,
      decoration: BoxDecoration(
        color: Theme.of(context).colorScheme.primaryContainer,
        borderRadius: BorderRadius.circular(15),
      ),
      margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 0),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Container(
                decoration: BoxDecoration(
                  color: Theme.of(context).colorScheme.secondaryContainer,
                  borderRadius: const BorderRadius.only(topLeft: Radius.circular(14), topRight: Radius.circular(15)),
                  border: Border(
                    bottom: BorderSide(
                      color: Theme.of(context).colorScheme.onSecondaryContainer,
                    )
                  )
                ),
                padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 15),
                child: _computeTitleRow()
              ),
            ],
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
            child: child,
          ),
        ],
      ) 
    );
  }
}