ChartLegendScope class

Composes any chart with a tappable legend that toggles series visibility.

Pass your full dataSets, render the chart inside builder using the filtered list that the builder receives. Tapping a legend item hides or shows the corresponding series without you having to touch the underlying chart widget.

Example

ChartLegendScope(
  dataSets: myDataSets,
  builder: (context, visibleDataSets) {
    return LineChartWidget(dataSets: visibleDataSets);
  },
)

With an external controller

final controller = ChartVisibilityController();

ChartLegendScope(
  controller: controller,
  dataSets: myDataSets,
  position: LegendPosition.top,
  builder: (context, visible) => BarChartWidget(dataSets: visible),
)
Inheritance

Constructors

ChartLegendScope({Key? key, required List<ChartDataSet> dataSets, required Widget builder(BuildContext context, List<ChartDataSet> visibleDataSets), ChartVisibilityController? controller, LegendPosition position = LegendPosition.bottom, double spacing = 12.0, ChartTheme? theme, String seriesLabelFor(Color color, List<ChartDataSet> group)?, bool showLegend = true})
const

Properties

builder Widget Function(BuildContext context, List<ChartDataSet> visibleDataSets)
Renders the chart. Receives the filtered (visible) list of datasets.
final
controller ChartVisibilityController?
Optional external controller. If omitted, the scope creates its own.
final
dataSets List<ChartDataSet>
The full list of datasets. Not mutated.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
position LegendPosition
Where to place the legend relative to the chart.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
seriesLabelFor String Function(Color color, List<ChartDataSet> group)?
Optional custom label resolver (see ChartLegend.fromDataSets).
final
showLegend bool
Set to false to hide the legend but still render the chart; useful when you want to manage toggling via your own UI.
final
spacing double
Gap between the chart and the legend.
final
theme ChartTheme?
Theme used for label colors. Falls back to the ambient Material theme.
final

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<ChartLegendScope>
Creates the mutable state for this widget at a given location in the tree.
override
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