diff --git a/lib/src/Toggles/Checkbox/checkbox.toggle.dart b/lib/src/Toggles/Checkbox/checkbox.toggle.dart index 0737ce3..6e38839 100644 --- a/lib/src/Toggles/Checkbox/checkbox.toggle.dart +++ b/lib/src/Toggles/Checkbox/checkbox.toggle.dart @@ -4,7 +4,6 @@ //--------------- //s2 CORE import 'package:flutter/material.dart'; -import 'package:flutter/scheduler.dart'; //s2 3RD-PARTY // @@ -21,6 +20,7 @@ import 'package:flutter/scheduler.dart'; 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; @@ -38,7 +38,7 @@ class AstromicCheckboxToggle extends StatefulWidget { //!SECTION // const AstromicCheckboxToggle( - {Key? key, + {super.key, //s1 -- Functionality this.stateVariable, this.initialState, @@ -53,9 +53,7 @@ class AstromicCheckboxToggle extends StatefulWidget { //s1 -- Content required this.itemBuilder, this.labelBuilder}) - : super( - key: key, - ); + : assert(stateVariable == null || initialState == null, "Can't define both the state variable and the initial state"); @override State createState() => _AstromicCheckboxToggleState(); @@ -84,7 +82,6 @@ class _AstromicCheckboxToggleState extends State { //s1 --Controllers & Listeners // //s1 --State - isChecked = widget.initialState ?? widget.stateVariable ?? false; //s1 --State // @@ -102,6 +99,15 @@ class _AstromicCheckboxToggleState extends 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 @@ -118,11 +124,6 @@ class _AstromicCheckboxToggleState extends State { Widget build(BuildContext context) { //SECTION - Build Setup //s1 -Values - if (widget.stateVariable != null) { - SchedulerBinding.instance.addPostFrameCallback((timeStamp) { - isChecked = widget.stateVariable!; - }); - } //double w = MediaQuery.of(context).size.width; //double h = MediaQuery.of(context).size.height; //s1 -Values @@ -141,12 +142,12 @@ class _AstromicCheckboxToggleState extends State { SizedBox( width: widget.itemSize, height: widget.itemSize, - child: widget.itemBuilder(isChecked, widget.isEnabled!, () => _onTap()), + 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!, () => _onTap()), + if (widget.labelBuilder != null) widget.labelBuilder!(isChecked, widget.isEnabled!, widget.isEnabled! ? () => _onTap() : null), ], ), );