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

88 lines
2.6 KiB
Dart
Raw Normal View History

2024-01-01 18:31:10 +01:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2024-01-09 12:34:44 +01:00
import '../../../utils/definitions/color_pair.dart';
import '../../../utils/logic/preferences_service.dart';
2024-01-01 18:31:10 +01:00
class TextSwitchContainer extends StatefulWidget {
final String leftText;
final String? rightText;
final bool hasSwitch;
final Function onTap;
final Function(bool)? onSwitchToggle;
2024-01-09 12:34:44 +01:00
final bool switchDefaultValue; // Parameter for the default switch value
2024-01-01 18:31:10 +01:00
2024-01-09 12:34:44 +01:00
const TextSwitchContainer({
super.key,
2024-01-01 18:31:10 +01:00
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-09 12:34:44 +01:00
State<TextSwitchContainer> createState() => _TextSwitchContainer();
2024-01-01 18:31:10 +01:00
}
2024-01-09 12:34:44 +01:00
class _TextSwitchContainer extends State<TextSwitchContainer> {
late bool switchValue;
Color backgroundColor = Colors.white; // Default value
Color textColor = Colors.black; // Default value
2024-01-01 19:52:42 +01:00
2024-01-09 12:34:44 +01:00
@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;
});
}
2024-01-01 18:31:10 +01:00
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => widget.onTap(),
child: Container(
2024-01-09 12:34:44 +01:00
padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 20),
2024-01-01 18:31:10 +01:00
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
widget.leftText,
2024-01-09 12:34:44 +01:00
style: const TextStyle(fontSize: 18),
2024-01-01 18:31:10 +01:00
),
if (widget.hasSwitch)
CupertinoSwitch(
trackColor: Colors.white,
2024-01-09 12:34:44 +01:00
thumbColor: switchValue ? backgroundColor : Colors.grey,
2024-01-01 18:31:10 +01:00
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!,
2024-01-09 12:34:44 +01:00
style: const TextStyle(fontSize: 16, color: Colors.grey),
2024-01-01 18:31:10 +01:00
),
],
),
),
);
}
}