[DEV] Done with the switcher toggle and the custom toggle.
This commit is contained in:
128
lib/src/Toggles/Custom/custom.toggle.dart
Normal file
128
lib/src/Toggles/Custom/custom.toggle.dart
Normal file
@@ -0,0 +1,128 @@
|
||||
//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 AstromicCustomToggle 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;
|
||||
//
|
||||
//s1 -- Content
|
||||
final Widget Function(bool isChecked, bool isEnabled, VoidCallback? onTap) itemBuilder;
|
||||
//!SECTION
|
||||
//
|
||||
const AstromicCustomToggle({
|
||||
super.key,
|
||||
//s1 -- Functionality
|
||||
this.stateVariable,
|
||||
this.initialState,
|
||||
this.onStateChanged,
|
||||
//s1 -- Configuration
|
||||
this.isEnabled = true,
|
||||
//s1 -- Content
|
||||
required this.itemBuilder,
|
||||
}) : assert(stateVariable == null || initialState == null, "Can't define both the state variable and the initial state");
|
||||
|
||||
@override
|
||||
State<AstromicCustomToggle> createState() => _AstromicCustomToggleState();
|
||||
}
|
||||
|
||||
class _AstromicCustomToggleState extends State<AstromicCustomToggle> {
|
||||
//
|
||||
//SECTION - State Variables
|
||||
//s1 --Controllers
|
||||
//s1 --Controllers
|
||||
//
|
||||
//s1 --State
|
||||
late bool currentState;
|
||||
//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
|
||||
currentState = 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(AstromicCustomToggle oldWidget) {
|
||||
if (widget.stateVariable != null && widget.stateVariable != oldWidget.stateVariable) {
|
||||
currentState = widget.stateVariable!;
|
||||
}
|
||||
super.didUpdateWidget(oldWidget);
|
||||
}
|
||||
|
||||
//SECTION - Stateless functions
|
||||
//!SECTION
|
||||
|
||||
//SECTION - Action Callbacks
|
||||
_onTap() {
|
||||
setState(() {
|
||||
currentState = !currentState;
|
||||
if (widget.onStateChanged != null) widget.onStateChanged!(currentState);
|
||||
});
|
||||
}
|
||||
//!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 widget.itemBuilder(currentState, widget.isEnabled!, widget.isEnabled! ? () => _onTap() : null);
|
||||
|
||||
//!SECTION
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user