import 'package:didvan/views/widgets/menu_item.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class DidvanSwitch extends StatefulWidget {
  final bool value;
  final String title;
  final IconData? icon;
  final void Function(bool value) onChanged;
  const DidvanSwitch({
    Key? key,
    required this.value,
    required this.title,
    this.icon,
    required this.onChanged,
  }) : super(key: key);

  @override
  State<DidvanSwitch> createState() => _DidvanSwitchState();
}

class _DidvanSwitchState extends State<DidvanSwitch> {
  bool _value = false;

  @override
  void initState() {
    _value = widget.value;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MenuOption(
      title: widget.title,
      onTap: () {
        setState(
          () => _value = !_value,
        );
        widget.onChanged(_value);
      },
      icon: widget.icon,
      trailing: CupertinoSwitch(
        activeTrackColor: Theme.of(context).colorScheme.primary,
        value: _value,
        onChanged: (value) {
          setState(() {
            _value = value;
          });
          widget.onChanged(value);
        },
      ),
    );
  }
}
