ModernMemoires/lib/views/settings_page/widgets/TextSwitchContainer.dart

71 lines
2.2 KiB
Dart
Raw Normal View History

2024-01-01 18:31:10 +01:00
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;
2024-01-01 19:52:42 +01:00
final bool switchDefaultValue; // Added parameter for the default switch value
2024-01-01 18:31:10 +01:00
TextSwitchContainer({
required this.leftText,
this.rightText,
this.hasSwitch = false,
required this.onTap,
this.onSwitchToggle,
2024-01-01 19:52:42 +01:00
this.switchDefaultValue = false,
2024-01-01 18:31:10 +01:00
});
@override
2024-01-01 19:52:42 +01:00
_TextSwitchContainerState createState() =>
_TextSwitchContainerState(switchValue: this.switchDefaultValue);
2024-01-01 18:31:10 +01:00
}
class _TextSwitchContainerState extends State<TextSwitchContainer> {
2024-01-01 19:52:42 +01:00
bool switchValue; // No longer explicitly initialized here
_TextSwitchContainerState(
{required this.switchValue}); // Constructor takes the initial switch value
2024-01-01 18:31:10 +01:00
@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),
),
],
),
),
);
}
}