Skip to content

Commit

Permalink
Add showLegend Attribute to Toggle Legend Visibility (#24)
Browse files Browse the repository at this point in the history
* added attribute showLegend

* return just segmentedBar when show legend is false

* Bump to 0.5.0

---------

Co-authored-by: fujidaiti <fujidaiti@gmail.com>
  • Loading branch information
ilicina1 and fujidaiti authored Aug 16, 2024
1 parent 904b409 commit 5d85416
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 0.5.0 August 16, 2024

- Add: `showLegend` Attribute to Toggle Legend Visibility (#24)

## 0.4.2 May 18, 2024

Dependency updates.
Expand Down
63 changes: 37 additions & 26 deletions lib/src/primer_progress_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PrimerProgressBar extends StatelessWidget {
this.barStyle = const SegmentedBarStyle(),
this.legendStyle = const SegmentedBarLegendStyle(),
this.legendItemBuilder = _defaultLegendItemBuilder,
this.showLegend = true,
}) : assert(maxTotalValue == null || maxTotalValue > 0);

/// A list of [Segment] to be displayed in the progress bar.
Expand All @@ -46,11 +47,16 @@ class PrimerProgressBar extends StatelessWidget {
/// A builder that creates a [LegendItems] from a [Segment] for the legend.
final LegendItemBuilder legendItemBuilder;

/// Whether to display the legend.
///
/// If `true`, the legend will be shown. If `false`, the legend will be hidden.
final bool showLegend;

@override
Widget build(BuildContext context) {
final legendItems = segments.map(legendItemBuilder).toList();
if (legendStyle.maxLines == null) {
return _build(context, segments, legendItems);
return _build(context, segments, legendItems, showLegend);
}
return LayoutBuilder(
builder: (context, constraints) {
Expand All @@ -69,36 +75,41 @@ class PrimerProgressBar extends StatelessWidget {
final segments = [
for (final item in ellipsizedLegendItems) item.segment
];
return _build(context, segments, ellipsizedLegendItems);
return _build(context, segments, ellipsizedLegendItems, showLegend);
},
);
}

Widget _build(
BuildContext context,
List<Segment> segments,
List<LegendItem> legendItems,
) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SegmentedBar(
segments: segments,
style: barStyle,
maxTotalValue: maxTotalValue,
),
SegmentedBarLegend(
style: SegmentedBarLegendStyle(
maxLines: null,
spacing: legendStyle.spacing,
runSpacing: legendStyle.runSpacing,
padding: legendStyle.padding,
Widget _build(BuildContext context, List<Segment> segments,
List<LegendItem> legendItems, bool showLegend) {
if (showLegend) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SegmentedBar(
segments: segments,
style: barStyle,
maxTotalValue: maxTotalValue,
),
ellipsisBuilder: legendEllipsisBuilder,
children: legendItems,
),
],
SegmentedBarLegend(
style: SegmentedBarLegendStyle(
maxLines: null,
spacing: legendStyle.spacing,
runSpacing: legendStyle.runSpacing,
padding: legendStyle.padding,
),
ellipsisBuilder: legendEllipsisBuilder,
children: legendItems,
),
],
);
}

return SegmentedBar(
segments: segments,
style: barStyle,
maxTotalValue: maxTotalValue,
);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: primer_progress_bar
description: Unofficial Flutter implementation of the progress bar defined in GitHub Primer Design System.
version: 0.4.2
version: 0.5.0
repository: https://github.com/fujidaiti/primer_progress_bar

environment:
Expand Down

0 comments on commit 5d85416

Please sign in to comment.