diff --git a/example/lib/pages/components/breadcrumbs_example.dart b/example/lib/pages/components/breadcrumbs_example.dart index 12566eda..9bc02348 100644 --- a/example/lib/pages/components/breadcrumbs_example.dart +++ b/example/lib/pages/components/breadcrumbs_example.dart @@ -12,8 +12,8 @@ class BreadCrumbsExample extends StatefulWidget { } class _BreadCrumbsExampleState extends State { - List _children = [ - BreadCrumb( + List _children = [ + ZetaBreadCrumb( label: 'Icon before with seperator', onPressed: () { print("Breadcrumb " + 0.toString() + "Clicked"); @@ -39,7 +39,7 @@ class _BreadCrumbsExampleState extends State { onPressed: () { setState(() { _children.add( - BreadCrumb( + ZetaBreadCrumb( label: 'Icon before with seperator', onPressed: () { print("Breadcrumb clicked"); diff --git a/example/widgetbook/pages/components/breadcrumbs_widgetbook.dart b/example/widgetbook/pages/components/breadcrumbs_widgetbook.dart index 2ecdb9e2..e36c7f8b 100644 --- a/example/widgetbook/pages/components/breadcrumbs_widgetbook.dart +++ b/example/widgetbook/pages/components/breadcrumbs_widgetbook.dart @@ -3,6 +3,7 @@ import 'package:widgetbook/widgetbook.dart'; import 'package:zeta_flutter/zeta_flutter.dart'; import '../../test/test_components.dart'; +import '../../utils/utils.dart'; Widget breadCrumbsUseCase(BuildContext context) => WidgetbookTestWidget( widget: Center( @@ -18,8 +19,8 @@ class BreadCrumbExample extends StatefulWidget { } class _BreadCrumbExampleState extends State { - List _children = [ - BreadCrumb( + List _children = [ + ZetaBreadCrumb( label: 'Icon before with seperator', onPressed: () { print("Breadcrumb " + 0.toString() + "Clicked"); @@ -37,26 +38,7 @@ class _BreadCrumbExampleState extends State { ZetaBreadCrumbs( children: _children, rounded: widget.c.knobs.boolean(label: 'Rounded'), - activeIcon: widget.c.knobs.list( - label: 'ActiveIcon', - options: [ - ZetaIcons.star_round, - ZetaIcons.add_alert_round, - ZetaIcons.add_box_round, - ZetaIcons.barcode_round, - ], - labelBuilder: (value) { - if (value == ZetaIcons.star_round) - return 'ZetaIcons.star_round'; - if (value == ZetaIcons.add_alert_round) - return 'ZetaIcons.add_alert_round'; - if (value == ZetaIcons.add_box_round) - return 'ZetaIcons.add_box_round'; - if (value == ZetaIcons.barcode_round) - return 'ZetaIcons.barcode_round'; - return ''; - }, - ), + activeIcon: iconKnob(context), ), SizedBox( height: 50, @@ -65,7 +47,7 @@ class _BreadCrumbExampleState extends State { onPressed: () { setState(() { _children.add( - BreadCrumb( + ZetaBreadCrumb( label: 'Icon before with seperator', onPressed: () { print("Breadcrumb clicked"); diff --git a/lib/src/components/breadcrumbs/breadcrumbs.dart b/lib/src/components/breadcrumbs/breadcrumbs.dart index 2c0dce9f..cb791273 100644 --- a/lib/src/components/breadcrumbs/breadcrumbs.dart +++ b/lib/src/components/breadcrumbs/breadcrumbs.dart @@ -14,7 +14,7 @@ class ZetaBreadCrumbs extends StatefulWidget { }); /// Breadcrumb children - final List children; + final List children; /// {@macro zeta-component-rounded} final bool rounded; @@ -29,7 +29,7 @@ class ZetaBreadCrumbs extends StatefulWidget { void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); properties - ..add(IterableProperty('children', children)) + ..add(IterableProperty('children', children)) ..add(DiagnosticsProperty('rounded', rounded)) ..add(DiagnosticsProperty('activeIcon', activeIcon)); } @@ -37,7 +37,7 @@ class ZetaBreadCrumbs extends StatefulWidget { class _ZetaBreadCrumbsState extends State { late int _selectedIndex; - late List _children; + late List _children; @override void initState() { @@ -46,7 +46,6 @@ class _ZetaBreadCrumbsState extends State { _children = [...widget.children]; } - // TODO: Optimize so we don't call set state each time. OldWidget stays the same as current widget @override void didUpdateWidget(ZetaBreadCrumbs oldWidget) { if (widget.children.length != _children.length) { @@ -86,8 +85,8 @@ class _ZetaBreadCrumbsState extends State { } ///Creates breadcumb widget - BreadCrumb createBreadCrumb(BreadCrumb input, int index) { - return BreadCrumb( + ZetaBreadCrumb createBreadCrumb(ZetaBreadCrumb input, int index) { + return ZetaBreadCrumb( label: input.label, isSelected: _selectedIndex == index, onPressed: () { @@ -100,7 +99,7 @@ class _ZetaBreadCrumbsState extends State { ); } - List renderedChildren(List children) { + List renderedChildren(List children) { final List returnList = []; if (children.length > 3) { returnList.add(createBreadCrumb(children.first, 0)); @@ -128,10 +127,10 @@ class _ZetaBreadCrumbsState extends State { } } -/// Class for untruncated [BreadCrumb]. -class BreadCrumb extends StatelessWidget { - ///Constructor for [BreadCrumb] - const BreadCrumb({ +/// Class for untruncated [ZetaBreadCrumb]. +class ZetaBreadCrumb extends StatefulWidget { + ///Constructor for [ZetaBreadCrumb] + const ZetaBreadCrumb({ super.key, required this.label, this.icon, @@ -140,30 +139,57 @@ class BreadCrumb extends StatelessWidget { this.activeIcon, }); - /// [BreadCrumb] label. + /// [ZetaBreadCrumb] label. final String label; /// Selected icon. final IconData? icon; - /// Is [BreadCrumb] selected. + /// Is [ZetaBreadCrumb] selected. final bool isSelected; - /// Handles press for [BreadCrumb] + /// Handles press for [ZetaBreadCrumb] final VoidCallback onPressed; - /// Active icon for [BreadCrumb] + /// Active icon for [ZetaBreadCrumb] final IconData? activeIcon; + @override + State createState() => _ZetaBreadCrumbState(); + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties + ..add(ObjectFlagProperty.has('onPressed', onPressed)) + ..add(StringProperty('label', label)) + ..add(DiagnosticsProperty('icon', icon)) + ..add(DiagnosticsProperty('isSelected', isSelected)) + ..add(DiagnosticsProperty('activeIcon', activeIcon)); + } +} + +class _ZetaBreadCrumbState extends State { + final controller = MaterialStatesController(); + + @override + void initState() { + super.initState(); + controller.addListener(() { + if (context.mounted && mounted) { + setState(() {}); + } + }); + } + @override Widget build(BuildContext context) { - final controller = MaterialStatesController(); final colors = Zeta.of(context).colors; return Material( color: Colors.transparent, child: InkWell( statesController: controller, - onTap: onPressed, + onTap: widget.onPressed, enableFeedback: false, splashColor: Colors.transparent, overlayColor: MaterialStateProperty.resolveWith((states) { @@ -171,16 +197,16 @@ class BreadCrumb extends StatelessWidget { }), child: Row( children: [ - if (isSelected) + if (widget.isSelected) Icon( - activeIcon ?? ZetaIcons.star_round, + widget.activeIcon ?? ZetaIcons.star_round, color: getColor(controller.value, colors), ), const SizedBox( width: ZetaSpacing.xs, ), Text( - label, + widget.label, style: TextStyle(color: getColor(controller.value, colors)), ), ], @@ -192,21 +218,21 @@ class BreadCrumb extends StatelessWidget { /// Get color of breadcrumb based on state. Color getColor(Set states, ZetaColors colors) { if (states.contains(MaterialState.hovered)) { - return colors.blue.shade100; + return colors.blue; } - if (isSelected) return colors.black; + if (widget.isSelected) return colors.black; return colors.textSubtle; } @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); - properties - ..add(ObjectFlagProperty.has('onPressed', onPressed)) - ..add(StringProperty('label', label)) - ..add(DiagnosticsProperty('icon', icon)) - ..add(DiagnosticsProperty('isSelected', isSelected)) - ..add(DiagnosticsProperty('activeIcon', activeIcon)); + properties.add( + DiagnosticsProperty( + 'controller', + controller, + ), + ); } } @@ -290,8 +316,10 @@ class _BreadCrumbsTruncatedState extends State { minimumSize: MaterialStateProperty.all(Size.zero), elevation: const MaterialStatePropertyAll(0), ), - child: const Icon( - ZetaIcons.more_horizontal_round, + child: Icon( + widget.rounded + ? ZetaIcons.more_horizontal_round + : ZetaIcons.more_horizontal_sharp, size: ZetaSpacing.x4, ) .paddingHorizontal(ZetaSpacing.xs)