ChartLegend class

A tappable legend for a chart.

Each item renders a color swatch plus its label. Tapping an item calls onToggle with that item's color so the caller can flip visibility. Items whose color is in hidden are rendered with reduced opacity and a strike-through label to signal that the series is currently hidden.

The widget is purely presentational — it does not own visibility state. Combine with ChartLegendScope for an end-to-end toggle experience, or manage the hidden set yourself if you need custom behavior.

Example

ChartLegend(
  items: const [
    ChartLegendItem(color: Colors.indigo, label: 'Sales'),
    ChartLegendItem(color: Colors.green, label: 'Revenue'),
  ],
  hidden: hiddenColors,
  onToggle: (color) => setState(() {
    hiddenColors.toggle(color);
  }),
)
Inheritance

Constructors

ChartLegend({Key? key, required List<ChartLegendItem> items, Set<Color> hidden = const <Color>{}, ValueChanged<Color>? onToggle, ChartTheme? theme, Axis direction = Axis.horizontal, double runSpacing = 8.0, double spacing = 16.0, EdgeInsets padding = const EdgeInsets.symmetric(horizontal: 8, vertical: 6), double swatchSize = 12.0, TextStyle? labelStyle})
const
ChartLegend.fromDataSets(List<ChartDataSet> dataSets, {Key? key, Set<Color> hidden = const <Color>{}, ValueChanged<Color>? onToggle, ChartTheme? theme, Axis direction = Axis.horizontal, String seriesLabelFor(Color color, List<ChartDataSet> group)?})
Construct a legend from a list of ChartDataSets, grouped by color.
factory

Properties

direction Axis
Whether items are laid out in a row (horizontal) or column (vertical). The horizontal variant wraps onto multiple lines when space runs out.
final
hashCode int
The hash code for this object.
no setterinherited
hidden Set<Color>
Colors whose series are currently hidden (rendered dimmed).
final
items List<ChartLegendItem>
The rows/columns to show.
final
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
labelStyle TextStyle?
Optional label text style override.
final
onToggle ValueChanged<Color>?
Called when a legend entry is tapped. Omit to render a non-interactive legend.
final
padding EdgeInsets
Padding around each tappable row.
final
runSpacing double
Vertical gap between wrapped rows (horizontal) or between items (vertical).
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
spacing double
Horizontal gap between items when laid out horizontally.
final
swatchSize double
Diameter of the color swatch circle.
final
theme ChartTheme?
Theme used for label colors. Falls back to the ambient Material theme.
final

Methods

build(BuildContext context) Widget
Describes the part of the user interface represented by this widget.
override
createElement() StatelessElement
Creates a StatelessElement to manage this widget's location in the tree.
inherited
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited