// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:general_package/constants/constants.dart';
import 'package:general_package/responsive/responsive.dart';
import 'package:general_package/themes/themes.dart';
import 'package:get/get_rx/src/rx_typedefs/rx_typedefs.dart';

class DynamicFormRadio extends StatefulWidget {
  DynamicFormRadio({
    super.key,
    this.title,
    required this.options,
    required this.onChanged,
    this.padding,
    this.initialValue,
  });
  String? title;
  EdgeInsetsGeometry? padding;
  List<dynamic> options;
  String? initialValue;
  final void Function(String) onChanged;
  @override
  State<DynamicFormRadio> createState() => _DynamicFormRadioState();
}

class _DynamicFormRadioState extends State<DynamicFormRadio> {
  String? value;

  @override
  void initState() {
    super.initState();
    if (widget.initialValue!=null) {
      value=widget.initialValue;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: widget.padding,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          if(widget.title!=null)Text(widget.title!, style: Theme.of(context).textTheme.bodyText1),
          GeneralConstants.instance.sizedBoxHeight10,
          Container(
              padding: GeneralConstants.instance.paddingSymmetric10,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(90),
                color: AppTheme.secondaryColor.withOpacity(0.4),
              ),
              child: Row(
                children: widget.options
                    .map(
                      (option) => Expanded(
                        child: InkWell(
                          onTap: () {
                            widget.onChanged(option);
                            setState(() {
                              value=option;
                            });
                          },
                          child: Container(
                            constraints: const BoxConstraints(
                              minHeight: 30,
                            ),
                            decoration: BoxDecoration(
                              gradient: (value==option)?LinearGradient(colors: [
                                AppTheme.primaryColor,
                                AppTheme.primaryDarkColor,
                              ]):null,
                              borderRadius: BorderRadius.circular(90),
                            ),
                            child: Align(
                              alignment: Alignment.center,
                              child: Text(option,style: Theme.of(context).textTheme.bodyMedium!.copyWith(color: (value==option)?AppTheme.whiteColor:AppTheme.blackColor),),
                            ),
                          ),
                        ),
                      ),
                    )
                    .toList(),
              )),
        ],
      ),
    );
  }
}
