import 'package:flutter/material.dart';

class FieldLayout extends StatelessWidget {
  const FieldLayout({
    super.key,
    this.layoutType = LayoutType.O_O_O_O,
    required this.fields,
    this.rowHeight = 70,
    this.maxWidth = double.infinity,
  });
  final LayoutType layoutType;
  final List<Widget> fields;
  final double rowHeight;
  final double maxWidth;

  @override
  Widget build(BuildContext context) {
    List<Widget> widgets = fields;
    if (fields.length <= layoutType.index) {
      for (int i = fields.length; i < layoutType.index + 1; i++) {
        widgets.add(Container());
      }
    }
    switch (layoutType) {
      case LayoutType.OOOOOOO:
        return _buildOneWidgetField(widgets[0]);
      case LayoutType.OOO_OOO:
        return _buildTwoWidgetField(widgets[0], widgets[1]);
      case LayoutType.OOO_O_O:
        return _buildThreeWidgetField(widgets[0], widgets[1], widgets[2]);
      case LayoutType.O_O_O_O:
        return _buildFourWidgetField(
            widgets[0], widgets[1], widgets[2], widgets[3]);
    }
  }

  Widget _buildRowContainer(Widget field) {
    return Container(
      height: 70,
      constraints: BoxConstraints(
        maxWidth: maxWidth,
      ),
      alignment: Alignment.centerLeft,
      child: field,
    );
  }

  Widget _buildOneWidgetField(Widget field) {
    return _buildRowContainer(field);
  }

  Widget _buildTwoWidgetField(Widget field1, Widget field2) {
    return _buildRowContainer(Row(
      children: [
        Flexible(child: field1),
        Flexible(child: field2),
      ],
    ));
  }

  Widget _buildThreeWidgetField(Widget field1, Widget field2, Widget field3) {
    return _buildRowContainer(
      Row(
        children: [
          Flexible(flex: 2, child: field1),
          Flexible(flex: 1, child: field2),
          Flexible(flex: 1, child: field3),
        ],
      ),
    );
  }

  Widget _buildFourWidgetField(
      Widget field1, Widget field2, Widget field3, Widget field4) {
    return _buildRowContainer(
      Row(
        children: [
          Flexible(child: field1),
          Flexible(child: field2),
          Flexible(child: field3),
          Flexible(child: field4),
        ],
      ),
    );
  }
}

/// 布局样式
enum LayoutType {
  OOOOOOO,
  OOO_OOO,
  OOO_O_O,
  O_O_O_O,
}
