import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';

// 控制面板-开关控制器
class ControlItemSwitch extends StatefulWidget {
  const ControlItemSwitch(
      {super.key,
      required this.itemText,
      required this.currentValue,
      this.onChanged});
  final String itemText;
  final bool currentValue;
  final ValueChanged<bool>? onChanged;

  @override
  State<ControlItemSwitch> createState() => _ControlItemSwitchState();
}

class _ControlItemSwitchState extends State<ControlItemSwitch> {
  late bool _currentValue;

  @override
  void initState() {
    super.initState();
    _currentValue = widget.currentValue;
  }

  @override
  void didUpdateWidget(ControlItemSwitch oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.currentValue != _currentValue) {
      _currentValue = widget.currentValue;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
      decoration: BoxDecoration(
        color: LabColors.base200,
        borderRadius: BorderRadius.circular(3),
        border: Border.all(
          color: LabColors.base400,
          width: 1,
        ),
      ),
      child: Row(
        children: [
          Text(
            widget.itemText + '：' + (_currentValue ? "开启" : "关闭"),
            style: TextStyle(
              fontSize: 14,
              color: LabColors.text700,
            ),
          ),
          Expanded(
            child: Container(),
          ),
          InkWell(
            onTap: () {
              setState(() {
                _currentValue = !_currentValue;
              });
              widget.onChanged?.call(_currentValue);
            },
            child: Container(
              width: 50,
              height: 30,
              decoration: BoxDecoration(
                color: _currentValue ? LabColors.base600 : LabColors.base400,
                borderRadius: BorderRadius.circular(3),
              ),
              child: Center(
                child: Text(
                  _currentValue ? "ON" : "OFF",
                  style: TextStyle(
                    fontSize: 14,
                    color: LabColors.text100,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
