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

class DropdownItems extends StatefulWidget {
  int index;
  String initValue;

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

class _DropdownItemsState extends State<DropdownItems> {
  List<DropdownMenuItem<String>> _dropDownMenuItems;
  String selectedValue;

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

  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: Text(cpysList[i].cpysText,
              style: TextStyle(
                  color: cpysList[i].cpysFont,
                  background: Paint()..color = cpysList[i].cpysBackground))));
    }
    return items;
  }

  Widget getDropdownButton() {
    //DropdownButton默认有一条下划线，DropdownButtonHideUnderline去除下划线
    return DropdownButtonHideUnderline(
      child: DropdownButton(
        value: selectedValue,
        items: _dropDownMenuItems,
        onChanged: (String _selectedFruit) {
          selectedValue = _selectedFruit;
          //黑烟初审数据审核Dropdown选项改变广播
          eventBus.fire(HycsDataAuditDropdownEvent('黑烟初审数据审核Dropdown选项已改变', selectedValue));
          setState(() {});
          print('selectedValue = $selectedValue');
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return getDropdownButton();
  }
}
