Files
astromic_elements/lib/src/Toggles/Checkbox/checkbox.toggle.dart

158 lines
3.9 KiB
Dart

//SECTION - Imports
//
//s1 PACKAGES
//---------------
//s2 CORE
import 'package:flutter/material.dart';
//s2 3RD-PARTY
//
//s1 DEPENDENCIES
//---------------
//s2 SERVICES
//s2 MODELS
//s2 MISC
//!SECTION - Imports
//
//SECTION - Exports
//!SECTION - Exports
//
class AstromicCheckboxToggle extends StatefulWidget {
//SECTION - Widget Arguments
//s1 -- Functionality
/// If provided, you have to change the variable yourself in the onStateChanged!
final bool? stateVariable;
final bool? initialState;
final void Function(bool)? onStateChanged;
//s1 -- Configuration
final bool? isEnabled;
final bool? isLabelTapable;
final TextDirection textDirection;
//s1 -- Style
final double? itemSize;
final double? labelSpacing;
//
//s1 -- Content
final Widget Function(bool isChecked, bool isEnabled, VoidCallback? onTap) itemBuilder;
final Widget Function(bool isChecked, bool isEnabled, VoidCallback? onTap)? labelBuilder;
//!SECTION
//
const AstromicCheckboxToggle(
{super.key,
//s1 -- Functionality
this.stateVariable,
this.initialState,
this.onStateChanged,
//s1 -- Configuration
this.isEnabled = true,
this.isLabelTapable = true,
this.textDirection = TextDirection.ltr,
//s1 -- Style
this.itemSize = 24,
this.labelSpacing = 6,
//s1 -- Content
required this.itemBuilder,
this.labelBuilder})
: assert(stateVariable == null || initialState == null, "Can't define both the state variable and the initial state");
@override
State<AstromicCheckboxToggle> createState() => _AstromicCheckboxToggleState();
}
class _AstromicCheckboxToggleState extends State<AstromicCheckboxToggle> {
//
//SECTION - State Variables
//s1 --Controllers
//s1 --Controllers
//
//s1 --State
late bool isChecked;
//s1 --State
//
//s1 --Constants
//s1 --Constants
//!SECTION
@override
void initState() {
super.initState();
//
//SECTION - State Variables initializations & Listeners
//s1 --Controllers & Listeners
//s1 --Controllers & Listeners
//
//s1 --State
isChecked = widget.initialState ?? widget.stateVariable ?? false;
//s1 --State
//
//s1 --Late & Async Initializers
//s1 --Late & Async Initializers
//!SECTION
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
//
//SECTION - State Variables initializations & Listeners
//s1 --State
//s1 --State
//!SECTION
}
@override
void didUpdateWidget(AstromicCheckboxToggle oldWidget) {
if (widget.stateVariable != null && widget.stateVariable != oldWidget.stateVariable) {
isChecked = widget.stateVariable!;
}
super.didUpdateWidget(oldWidget);
}
//SECTION - Stateless functions
//!SECTION
//SECTION - Action Callbacks
_onTap() {
setState(() {
isChecked = !isChecked;
if (widget.onStateChanged != null) widget.onStateChanged!(isChecked);
});
}
//!SECTION
@override
Widget build(BuildContext context) {
//SECTION - Build Setup
//s1 -Values
//double w = MediaQuery.of(context).size.width;
//double h = MediaQuery.of(context).size.height;
//s1 -Values
//
//s1 -Widgets
//s1 -Widgets
//!SECTION
//SECTION - Build Return
return Directionality(
textDirection: widget.textDirection,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
//S1 -- Item
SizedBox(
width: widget.itemSize,
height: widget.itemSize,
child: widget.itemBuilder(isChecked, widget.isEnabled!, widget.isEnabled! ? () => _onTap() : null),
),
//S1 -- Label Spacing
if (widget.labelBuilder != null) SizedBox(width: widget.labelSpacing),
//S1 -- Label
if (widget.labelBuilder != null) widget.labelBuilder!(isChecked, widget.isEnabled!, widget.isEnabled! ? () => _onTap() : null),
],
),
);
//!SECTION
}
}