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

//错误提示
// This class (or a class which this class inherits from) is marked as '@immutable', but one or more of
// This class (or a class that this class inherits from) is marked as '@immutable', but one or more of
//
// 原本是自定义一个导航栏，需要传递一个参数（标题）过来。
// class NavWidget extends StatelessWidget {
//   String title; //
//   NavWidget(this.title);
// 结果就提示了上面的错误。看了下有道翻译
// 有道翻译：这个类(或该类继承自的一个类)被标记为“@不可变”，但是它的一个或多个实例字段不是final: NavWidget.title
//  意为StatelessWidget是一个不可变的widget，申明的title也应为不可变的。所以我就加了个申明的关键字final。然后警告就没了
//
// 以下是修改后的代码，不再报错
// class NavWidget extends StatelessWidget {
//   final String title; //
//   NavWidget(this.title);

class CheckboxButtonItem extends StatefulWidget {
  final int index;

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

class _CheckboxButtonItemState extends State<CheckboxButtonItem> {
  //try_setState();   //避免如下异常报错
  try_setState() {
    try {
      setState(() {});
    } catch (e) {
      print('setState(() {})异常：${e}');
    }
  }

  @override
  void initState() {
    //黑烟审核推送交警Checkbox改变事件
    eventBus.on<HycsDataAuditSfyc>().listen((event) {
      print(event.str);
      try_setState(); //避免如下异常报错
    });

    super.initState();
  }

  //自定义带说明图标按钮函数。点击说明文字有反应
  Widget _getCheckboxButton() {
    return Container(
      width: ScreenUtil().setWidth(330),
      height: ScreenUtil().setWidth(110),
      alignment: const Alignment(0, 1),
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey[600], width: 1),
        borderRadius: BorderRadius.circular(3),
      ),
      child: FlatButton(
        padding: EdgeInsets.all(0),
        onPressed: !fh_hyc
            ? null
            : () {
                tsjj = !tsjj;
                //黑烟审核推送交警Checkbox改变广播
                eventBus.fire(HycsDataAuditCheckboxButton('黑烟审核推送交警Checkbox已改变', tsjj));
                print('tsjj = $tsjj');
                setState(() {});
              },
        color: Colors.transparent,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Text('同时推送交警',
                style: TextStyle(color: !fh_hyc || 1 == sfyc ? Colors.grey : null, fontSize: 12)),
            SizedBox(width: 1),
            !fh_hyc || 1 == sfyc
                ? Icon(Icons.check_box_outline_blank, color: Colors.grey)
                : tsjj
                    ? Icon(Icons.check_box, color: Colors.blue)
                    : Icon(Icons.check_box_outline_blank, color: Colors.black),
          ],
        ),
      ),
    );
  }

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