build method
Describes the part of the user interface represented by this widget.
The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.
The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedWidgetOfExactType.
If a widget's build method is to depend on anything else, use a StatefulWidget instead.
See also:
- StatelessWidget, which contains the discussion on performance considerations.
Implementation
@override
Widget build(BuildContext context) {
final theme = BankThemeData.of(context);
final scope = BankUiScope.of(context);
final isDark =
ThemeData.estimateBrightnessForColor(theme.surface) == Brightness.dark;
final resolvedGain = gainColor ??
(isDark ? BankTokens.investmentGainDark : BankTokens.investmentGain);
final resolvedLoss = lossColor ??
(isDark ? BankTokens.investmentLossDark : BankTokens.investmentLoss);
if (dataPoints.isEmpty) {
return Center(
child: Text(
emptyLabel,
style: BankTokens.bodyMedium
.copyWith(color: theme.onSurfaceVariant)
.merge(emptyLabelStyle),
),
);
}
final spots = dataPoints
.asMap()
.entries
.map((e) => FlSpot(e.key.toDouble(), e.value.value))
.toList();
final minY = dataPoints.map((d) => d.value).reduce((a, b) => a < b ? a : b);
final maxY = dataPoints.map((d) => d.value).reduce((a, b) => a > b ? a : b);
final padding = (maxY - minY) * 0.1;
final chartMinY = minY - padding;
final chartMaxY = maxY + padding;
final ySpan = chartMaxY - chartMinY;
// Period direction drives the default line colour and the header
// change chip: gains read green, losses red, on every preset.
final first = dataPoints.first.value;
final last = dataPoints.last.value;
final isGain = last >= first;
final color = lineColor ?? (isGain ? resolvedGain : resolvedLoss);
final changePct = first == 0 ? 0.0 : (last - first) / first.abs() * 100;
final changeStr =
'${isGain ? '+' : '-'}${changePct.abs().toStringAsFixed(2)}%';
String formatValue(double v, {bool compact = false}) {
if (valueFormatter != null) return valueFormatter!(v);
final code = currencyCode;
if (code == null) return v.toStringAsFixed(2);
return BankMoneyFormatter.format(
amount: Money.fromDouble(v, code).amount,
currencyCode: code,
numeralStyle: scope.numeralStyle,
compact: compact,
);
}
String formatDate(DateTime d) =>
dateFormatter?.call(d) ?? BankDateFormatter.formatShort(d);
final axisStyle =
BankTokens.labelSmall.copyWith(color: theme.onSurfaceVariant);
final chart = RepaintBoundary(
child: Column(
children: [
if (showChangeHeader) ...[
Row(
children: [
Expanded(
child: Text(
formatValue(last),
style: BankTokens.numeralMedium
.copyWith(color: theme.onSurface),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
const SizedBox(width: BankTokens.space2),
DecoratedBox(
decoration: BoxDecoration(
color: (isGain ? resolvedGain : resolvedLoss)
.withValues(alpha: 0.12),
borderRadius: BorderRadius.circular(BankTokens.radiusFull),
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: BankTokens.space2,
vertical: 2,
),
child: Text(
changeStr,
style: BankTokens.labelSmall.copyWith(
color: isGain ? resolvedGain : resolvedLoss,
),
maxLines: 1,
),
),
),
],
),
const SizedBox(height: BankTokens.space3),
],
SizedBox(
height: height ?? 200,
child: LineChart(
LineChartData(
minY: chartMinY,
maxY: chartMaxY,
lineTouchData: LineTouchData(
touchTooltipData: LineTouchTooltipData(
getTooltipColor: (_) =>
tooltipBackgroundColor ?? theme.surfaceVariant,
getTooltipItems: (spots) => spots
.map(
(s) => LineTooltipItem(
formatValue(s.y),
BankTokens.labelMedium
.copyWith(color: theme.onSurface)
.merge(tooltipStyle),
),
)
.toList(),
),
),
gridData: FlGridData(
show: showGrid,
drawVerticalLine: false,
getDrawingHorizontalLine: (_) => FlLine(
color: gridColor ?? theme.onSurface.withValues(alpha: 0.06),
strokeWidth: 1,
),
),
borderData: FlBorderData(show: false),
titlesData: FlTitlesData(
leftTitles: const AxisTitles(),
topTitles: const AxisTitles(),
// Value scale: min/max labels on the trailing edge.
rightTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: showAxisLabels,
reservedSize: 56,
interval: ySpan > 0 ? ySpan : 1,
getTitlesWidget: (value, meta) {
final tolerance = (ySpan > 0 ? ySpan : 1) * 0.001;
final isMinEdge = (value - meta.min).abs() < tolerance;
final isMaxEdge = (value - meta.max).abs() < tolerance;
if (!isMinEdge && !isMaxEdge) {
return const SizedBox.shrink();
}
return Padding(
padding: const EdgeInsetsDirectional.only(start: 4),
child: Text(
formatValue(isMinEdge ? minY : maxY, compact: true),
style: axisStyle,
maxLines: 1,
),
);
},
),
),
// Time scale: first/last date labels.
bottomTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: showAxisLabels,
reservedSize: 24,
interval:
spots.length > 1 ? (spots.length - 1).toDouble() : 1,
getTitlesWidget: (value, meta) {
final index = value.round();
if (index != 0 && index != dataPoints.length - 1) {
return const SizedBox.shrink();
}
return Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(
formatDate(dataPoints[index].timestamp),
style: axisStyle,
maxLines: 1,
),
);
},
),
),
),
lineBarsData: [
LineChartBarData(
spots: spots,
isCurved: true,
color: color,
barWidth: lineWidth ?? 2.0,
dotData: const FlDotData(show: false),
belowBarData: BarAreaData(
show: true,
gradient: gradient ??
LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
color.withValues(alpha: 0.24),
color.withValues(alpha: 0),
],
),
),
),
],
),
),
),
const SizedBox(height: BankTokens.space3),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: BankChartTimeRange.values.map((range) {
final isSelected = range == selectedRange;
final rangeAccent = accentColor ?? theme.primary;
return Padding(
padding: const EdgeInsets.only(right: BankTokens.space2),
child: TextButton(
onPressed: () => onRangeChanged?.call(range),
style: TextButton.styleFrom(
backgroundColor: isSelected
? rangeAccent.withValues(alpha: 0.12)
: Colors.transparent,
foregroundColor:
isSelected ? rangeAccent : theme.onSurfaceVariant,
minimumSize: const Size(44, 36),
padding: const EdgeInsets.symmetric(
horizontal: BankTokens.space3,
),
),
child: Text(
rangeLabels?[range] ?? _rangeLabels[range]!,
),
),
);
}).toList(),
),
),
],
),
);
if (semanticLabel == null) return chart;
return Semantics(label: semanticLabel, child: chart);
}