import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screen_util.dart';
import '../components/hyxx_data_handle.dart';
import '../services/EventBus.dart';

class CarNumberAndCpysItems extends StatefulWidget {
  int index;
  String initValue;

  CarNumberAndCpysItems(
      this.index, this.initValue); //I don't know what is this index for but I will put it in anyway
  @override
  _CarNumberAndCpysItemsState createState() => _CarNumberAndCpysItemsState();
}

class _CarNumberAndCpysItemsState extends State<CarNumberAndCpysItems> {
  List<DropdownMenuItem<String>> _dropDownMenuItems;
  String selectedValue;

  @override
  void initState() {
    super.initState();
    selectedValue = widget.initValue;
    _dropDownMenuItems = getDropDownMenuItems();
  }

  List<DropdownMenuItem<String>> getDropDownMenuItems() {
    List<DropdownMenuItem<String>> items = [];
    int len = cpysList.length;
    for (int i = 0; i < len; i++) {
      items.add(
        DropdownMenuItem(
          value: cpysList[i].cpysText,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                cpysList[i].cpysText,
                style: TextStyle(
                    color: cpysList[i].cpysFont,
                    background: Paint()..color = cpysList[i].cpysBackground),
              ),
              (selectedValue == cpysList[i].cpysText)
                  ? Icon(
                      Icons.check,
                      //color: cpysList[i].cpysBackground,
                      size: 16,
                    )
                  : SizedBox(
                      width: 0,
                    ),
            ],
          ),
        ),
      );
    }
    return items;
  }

  Widget getDropdownButton() {
    //DropdownButton默认有一条下划线，DropdownButtonHideUnderline去除下划线
    //更改图标亮度
    return Theme(
      //data: Theme.of(context).copyWith(primaryColor: cpysList[getIndexOfCpysList(colorText: selectedValue)].cpysFont),
      data: Theme.of(context).copyWith(brightness: Brightness.dark),
      child: DropdownButtonHideUnderline(
        child: DropdownButton(
          iconEnabledColor: cpysList[getIndexOfCpysList(colorText: selectedValue)].cpysFont,
          value: selectedValue,
          items: _dropDownMenuItems,
          onChanged: (String _selectedFruit) {
            selectedValue = _selectedFruit;
            _dropDownMenuItems = getDropDownMenuItems();
            //黑烟初审数据审核Dropdown选项改变广播
            eventBus.fire(HycsDataAuditDropdownEvent('黑烟初审数据审核Dropdown选项已改变', selectedValue));
            setState(() {});
            print('selectedValue = $selectedValue');
          },
        ),
      ),
    );
  }

  //Tab页面中的车牌号码、车牌颜色组件
  Widget getCarNumberAndCpys(int i) {
    return Container(
      width: ScreenUtil().setWidth(1022),
      height: ScreenUtil().setHeight(my_listTileHeight2 * 2 + 15),
      child: Column(children: [
        //1、车牌号码
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            SizedBox(width: ScreenUtil().setWidth(my_marginLeft2)),
            Text('${mapGetZpjlGetDataSpecial['car_number'].fieldText}: ',
                style: TextStyle(fontSize: my_fontSize)),
            Container(
              alignment: Alignment(-1, 0),
              //widthTrail = 400报错，360刚能显示，300换行，260
              height: ScreenUtil().setHeight(my_listTileHeight2), //最大高度
              width: ScreenUtil().setWidth(400),
              child: TextField(
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: my_fontSize,
                  //color: cpysList[getIndexOfCpysList(colorText: listGetZpjl[i]['cpys'])].cpysFont,
                  //color: cpysList[getIndexOfCpysList(colorText: topTabs_map['cpysText_List'][i])].cpysFont,
                  color: cpysList[getIndexOfCpysList(colorText: selectedValue)].cpysFont,
                ),
                // background: Paint()
                //   ..color = cpysList[getIndexOfCpysList(colorText: listGetZpjl[i]['cpys'])]
                //       .cpysBackground),
                decoration: InputDecoration(
                  focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(width: 2.0), borderRadius: BorderRadius.circular(3.0)),
                  enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(width: 2.0), borderRadius: BorderRadius.circular(3.0)),
                  //prefixText: "pre",
                  filled: true,
                  //fillColor: cpysList[getIndexOfCpysList(colorText: listGetZpjl[i]['cpys'])].cpysBackground,
                  //fillColor: cpysList[getIndexOfCpysList(colorText: topTabs_map['cpysText_List'][i])].cpysBackground,
                  fillColor: cpysList[getIndexOfCpysList(colorText: selectedValue)].cpysBackground,
                  hintText: '车牌号码',
                  //border: InputBorder.none, //TextField去掉下划线
                  //contentPadding: EdgeInsets.only(right: 0),
                  //contentPadding: const EdgeInsets.symmetric(vertical: _textFieldHeight),
                  //contentPadding: EdgeInsets.symmetric(vertical: _textFieldHeight),
                  contentPadding: EdgeInsets.all(0),
                  //contentPadding: EdgeInsets.only(top: 0),

                  // border: OutlineInputBorder(
                  //     //borderRadius: BorderRadius.circular(1.0),
                  //     borderSide: BorderSide(
                  //         //color: cpysList[getIndexOfCpysList(colorText: listGetZpjl[i]['cpys'])].cpysBorder,
                  //         color:
                  //             cpysList[getIndexOfCpysList(colorText: topTabs_map['cpysText_List'][i])]
                  //                 .cpysBorder,
                  //         width: 2.0)),
                ),
                //controller: listZpljController[i][indexField],
                controller: TextEditingController.fromValue(TextEditingValue(
                    text: listGetZpjl[i]['car_number'].toString(),
                    // 保持光标在最后
                    selection: TextSelection.fromPosition(TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: '${listGetZpjl[i]['car_number'].toString()}'.length)))),
                enabled: true,
                //利用控制器初始化文本
                onChanged: (value) {
                  listGetZpjl[i]['car_number'] = value;
                },
              ),
            ),
            SizedBox(width: ScreenUtil().setWidth(my_marginLeft2)),
          ],
        ),
        SizedBox(height: ScreenUtil().setHeight(15)),
        //2、车牌颜色
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            SizedBox(width: ScreenUtil().setWidth(my_marginLeft2)),
            Text('车牌颜色: ',
                style: TextStyle(fontSize: my_fontSize)),
            Container(
              alignment: Alignment(1, 0),
              //widthTrail = 400报错，360刚能显示，300换行，260
              height: ScreenUtil().setHeight(my_listTileHeight2), //最大高度
              width: ScreenUtil().setWidth(400),
              margin: EdgeInsets.only(bottom: 0),
              padding: EdgeInsets.only(left: 0, bottom: 2),
              decoration: BoxDecoration(
                //color: cpysList[getIndexOfCpysList(colorText: listGetZpjl[i]['cpys'])].cpysBackground,
                //color: cpysList[getIndexOfCpysList(colorText: topTabs_map['cpysText_List'][i])].cpysBackground,
                color: cpysList[getIndexOfCpysList(colorText: selectedValue)].cpysBackground,
                border: Border.all(color: Colors.black87, width: 2),
                //边框圆角设置
                borderRadius: BorderRadius.vertical(
                    top: Radius.elliptical(3, 3), bottom: Radius.elliptical(3, 3)),
              ),
              child: getDropdownButton(),
            )
          ],
        ),
      ],),
    );
  }

  @override
  Widget build(BuildContext context) {
    return getCarNumberAndCpys(widget.index);
  }
}
