ModernMemoires/lib/views/settings_page/widgets/text_switch_container_widge...

88 lines
2.6 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../utils/definitions/color_pair.dart';
import '../../../utils/logic/preferences_service.dart';
class TextSwitchContainer extends StatefulWidget {
final String leftText;
final String? rightText;
final bool hasSwitch;
final Function onTap;
final Function(bool)? onSwitchToggle;
final bool switchDefaultValue; // Parameter for the default switch value
const TextSwitchContainer({
super.key,
required this.leftText,
this.rightText,
this.hasSwitch = false,
required this.onTap,
this.onSwitchToggle,
this.switchDefaultValue = false,
});
@override
State<TextSwitchContainer> createState() => _TextSwitchContainer();
}
class _TextSwitchContainer extends State<TextSwitchContainer> {
late bool switchValue;
Color backgroundColor = Colors.white; // Default value
Color textColor = Colors.black; // Default value
@override
void initState() {
super.initState();
switchValue = widget.switchDefaultValue; // Initialize switchValue here
_loadColor();
}
void _loadColor() async {
ColorPair colorPair = await PreferencesService().loadColorPair();
setState(() {
backgroundColor = colorPair.backgroundColor;
textColor = colorPair.textColor;
});
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => widget.onTap(),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
widget.leftText,
style: const TextStyle(fontSize: 18),
),
if (widget.hasSwitch)
CupertinoSwitch(
trackColor: Colors.white,
thumbColor: switchValue ? backgroundColor : 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: const TextStyle(fontSize: 16, color: Colors.grey),
),
],
),
),
);
}
}