import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class TextSwitchContainer extends StatefulWidget { final String leftText; final String? rightText; final bool hasSwitch; final Function onTap; final Function(bool)? onSwitchToggle; final bool switchDefaultValue; // Added parameter for the default switch value TextSwitchContainer({ required this.leftText, this.rightText, this.hasSwitch = false, required this.onTap, this.onSwitchToggle, this.switchDefaultValue = false, }); @override _TextSwitchContainerState createState() => _TextSwitchContainerState(switchValue: this.switchDefaultValue); } class _TextSwitchContainerState extends State { bool switchValue; // No longer explicitly initialized here _TextSwitchContainerState( {required this.switchValue}); // Constructor takes the initial switch value @override Widget build(BuildContext context) { return GestureDetector( onTap: () => widget.onTap(), child: Container( padding: EdgeInsets.symmetric(horizontal: 0, vertical: 20), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( widget.leftText, style: TextStyle(fontSize: 18), ), if (widget.hasSwitch) CupertinoSwitch( trackColor: Colors.white, thumbColor: switchValue ? Colors.lightGreen : Colors.grey, value: switchValue, onChanged: (newValue) { setState(() { switchValue = newValue; widget.onSwitchToggle?.call(newValue); }); }, activeColor: Colors.white, // Active (ON) color // CupertinoSwitch doesn't allow inactive thumb color customization by default // Track color changes with the active color ) else if (widget.rightText != null) Text( widget.rightText!, style: TextStyle(fontSize: 16, color: Colors.grey), ), ], ), ), ); } }