import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';

class SingleChoiceDoctor extends FStatefulWidget {
  SingleChoiceDoctor({
    Key? key,
    required this.source,
    this.onChangeIndex,
    this.value,
  }) : super(key: key);

  /// 数据源
  final List<dynamic> source;

  ///选中值
  final String? value;

  /// 下拉选中的值
  final ValueCallback? onChangeIndex;

  @override
  FState<SingleChoiceDoctor> createState() => _SingleChoiceDoctorState();
}

class _SingleChoiceDoctorState extends FState<SingleChoiceDoctor> {
  int _selectIndex = 0;

  @override
  void initState() {
    super.initState();
    final index = widget.source.indexWhere(
      (x) => x.code == widget.value,
    );
    _selectIndex = index;
    widget.onChangeIndex!(index);
  }

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      height: 500,
      child: FListView.builder(
        shrinkWrap: true,
        itemCount: widget.source.length,
        itemBuilder: (BuildContext context, int index) {
          return FMaterial(
            color: Colors.white,
            child: FInkWell(
              onTap: () {
                setState(() {
                  _selectIndex = index;
                  widget.onChangeIndex!(index);
                });
              },
              child: FContainer(
                padding: EdgeInsets.symmetric(
                  vertical: 15,
                ),
                margin: EdgeInsets.symmetric(
                  horizontal: 15,
                ),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: Color.fromRGBO(231, 231, 231, 1),
                      width: 0.5,
                    ),
                  ),
                ),
                child: _buildChoiceRow(index),
              ),
            ),
          );
        },
      ),
    );
  }

  ///编辑所选的行
  FWidget _buildChoiceRow(int index) {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        FExpanded(
          child: FRow(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              FRow(
                children: [
                  FColumn(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      FText(widget.source[index].name),
                    ],
                  )
                ],
              ),
              FContainer(
                width: 25,
                height: 25,
                decoration: BoxDecoration(
                  border: Border.all(
                    color: Color.fromRGBO(141, 141, 141, 1),
                    width: 0.5,
                  ),
                  borderRadius: BorderRadius.circular(25),
                ),
                child: _selectIndex == index
                    ? FContainer(
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(25),
                          color: Theme.of(context).primaryColor,
                        ),
                        child: FCenter(
                          child: FContainer(
                            child: FIcon(
                              Icons.check,
                              size: 20,
                              color: Colors.white,
                            ),
                          ),
                        ),
                      )
                    : FContainer(),
              ),
            ],
          ),
        ),
        FSizedBox(
          width: 15,
        ),
      ],
    );
  }
}
