[SYNC] Working on the Selectors Elemetns

This commit is contained in:
2024-05-14 15:36:09 +03:00
parent 1777641eaf
commit 4b1bcc7b5c
7 changed files with 301 additions and 8 deletions

View File

@@ -0,0 +1,31 @@
//s2 Core Packages Imports
import 'package:flutter/widgets.dart';
class AstromicSelectors {
//S1 -- Radio
static Widget radio<T>({
T? initialSelectedValue,
Function(T selectedItem)? onChanged,
//
AstromicSelectorConfiguration? configurations,
//
double? itemSpacing = 4,
//
required Widget Function(AstromicRadioSelectorItem<T> item, VoidCallback? onTap, bool isSelected) itemBuilder,
Widget Function(AstromicRadioSelectorItem<T> item)? disabledItemBuilder,
required List<AstromicRadioSelectorItem<T>> items,
}) =>
astromicRadioSelector<T>(
initialSelectedValue: initialSelectedValue,
onChanged: onChanged,
//
configurations: configurations,
//
itemSpacing: itemSpacing,
//
itemBuilder: itemBuilder,
disabledItemBuilder: disabledItemBuilder,
items: items,
);
}

View File

@@ -0,0 +1,45 @@
import 'package:flutter/widgets.dart';
class AstromicSelectorConfiguration {
//s1 Shared
final Axis axis;
final bool isNullable;
//s1 Chip Specific
final bool isConsequent;
final bool withClearButton;
final int maxSelectedItems;
final int crossAxisCount;
//s1 Radio Specific
final bool withExpandedSpace;
const AstromicSelectorConfiguration({
this.axis = Axis.horizontal,
this.isNullable = true,
//
this.isConsequent = false,
this.withClearButton = false,
this.maxSelectedItems = 10000,
this.crossAxisCount = 3,
//
this.withExpandedSpace = false,
});
AstromicSelectorConfiguration copyWith({
Axis? axis,
bool? isNullable,
bool? isConsequent,
bool? withClearButton,
int? maxSelectedItems,
int? crossAxisCount,
bool? withExpandedSpace,
}) {
return AstromicSelectorConfiguration(
axis: axis ?? this.axis,
isNullable: isNullable ?? this.isNullable,
isConsequent: isConsequent ?? this.isConsequent,
withClearButton: withClearButton ?? this.withClearButton,
maxSelectedItems: maxSelectedItems ?? this.maxSelectedItems,
crossAxisCount: crossAxisCount ?? this.crossAxisCount,
withExpandedSpace: withExpandedSpace ?? this.withExpandedSpace,
);
}
}

View File

@@ -0,0 +1,207 @@
//SECTION - Imports
//
//s1 PACKAGES
//---------------
//s2 CORE
import 'package:flutter/widgets.dart';
//s2 3RD-PARTY
//
//s1 DEPENDENCIES
//---------------
//s2 SERVICES
//s2 MODELS
import 'configuration.dart';
//s2 MISC
//!SECTION - Imports
//
//SECTION - Exports
//!SECTION - Exports
//
//
class AstromicRadioSelector<T> extends StatefulWidget {
//SECTION - Widget Arguments
//s1 -- Functionality
final T? initialSelectedValue;
final Function(T selectedItem)? onChanged;
//s1 -- Configuration
final AstromicSelectorConfiguration? configurations;
//s1 -- Style
final double? itemSpacing;
//s1 -- Content
final List<AstromicRadioSelectorItem<T>> items;
//
final Widget Function(AstromicRadioSelectorItem<T> item, VoidCallback? onTap, bool isSelected) itemBuilder;
final Widget Function(AstromicRadioSelectorItem<T> item)? disabledItemBuilder;
//!SECTION
//
AstromicRadioSelector({
Key? key,
//s1 -- Functionality
this.initialSelectedValue,
required this.items,
this.onChanged,
//s1 -- Configuration
this.configurations = const AstromicSelectorConfiguration(),
//s1 -- Style
this.itemSpacing = 8,
//s1 -- Content
required this.itemBuilder,
this.disabledItemBuilder,
}) : assert(configurations!.isNullable || initialSelectedValue != null, 'You need to supply an initial value if not nullable!'),
assert(
configurations!.isNullable ||
(items
.map(
(e) => e.value,
)
.toList()
.contains(initialSelectedValue)),
"Initial value is not present in the items!",
),
super(
key: key,
);
@override
State<AstromicRadioSelector<T>> createState() => _AstromicRadioSelectorState<T>();
}
class _AstromicRadioSelectorState<T> extends State<AstromicRadioSelector<T>> {
//
//SECTION - State Variables
//s1 --Controllers
//s1 --Controllers
//
//s1 --State
late T? selectedItem;
//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
selectedItem = widget.initialSelectedValue;
//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
}
//SECTION - Stateless functions
//!SECTION
//SECTION - Action Callbacks
_onTap(T value) {
setState(() {
selectedItem = value;
});
if (widget.onChanged != null) widget.onChanged!(value);
}
//!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
List<Widget> baseChildren = widget.items.map((currentItem) {
bool isSelected = currentItem.value == selectedItem;
//
return !currentItem.enabled! && widget.disabledItemBuilder != null
? widget.configurations!.withExpandedSpace
? Expanded(child: widget.disabledItemBuilder!(currentItem))
: widget.disabledItemBuilder!(currentItem)
: widget.configurations!.withExpandedSpace
? Expanded(child: widget.itemBuilder(currentItem, currentItem.enabled! ? () => _onTap(currentItem.value) : null, isSelected))
: widget.itemBuilder(currentItem, currentItem.enabled! ? () => _onTap(currentItem.value) : null, isSelected);
}).toList();
//s1 -Widgets
//!SECTION
//SECTION - Build Return
return widget.configurations!.withExpandedSpace
? widget.configurations!.axis == Axis.horizontal
? separatedRow(
baseChildren,
AstromicSpacing.hsb(widget.itemSpacing!),
)
: separatedColumn(
baseChildren,
AstromicSpacing.vsb(widget.itemSpacing!),
)
: Wrap(
alignment: WrapAlignment.center,
runAlignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
direction: widget.configurations!.axis,
spacing: widget.itemSpacing!,
children: baseChildren,
);
//!SECTION
}
@override
void dispose() {
//SECTION - Disposable variables
//!SECTION
super.dispose();
}
}
Widget separatedRow(List<Widget> children, Widget separator) {
List<Widget> finalChildren = [];
for (var e in children) {
if (children.indexOf(e) != children.length - 1) {
finalChildren.addAll([e, separator]);
} else {
finalChildren.add(e);
}
}
return Row(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: finalChildren,
);
}
Widget separatedColumn(List<Widget> children, Widget separator) {
List<Widget> finalChildren = [];
for (var e in children) {
if (children.indexOf(e) != children.length - 1) {
finalChildren.addAll([e, separator]);
} else {
finalChildren.add(e);
}
}
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: finalChildren,
);
}