|
| 1 | +import 'package:fl_chart/fl_chart.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + final data = [ |
| 7 | + const FlSpot(0, 0.5), |
| 8 | + const FlSpot(1, 1.3), |
| 9 | + const FlSpot(2, 1.9), |
| 10 | + ]; |
| 11 | + |
| 12 | + const viewSize = Size(400, 400); |
| 13 | + |
| 14 | + testWidgets( |
| 15 | + 'Test the effect of minIncluded and maxIncluded in sideTitles', |
| 16 | + (WidgetTester tester) async { |
| 17 | + |
| 18 | + // Minimum/maximum included |
| 19 | + final mima = [[true, true], [true, false], [false, true], [false, false]]; |
| 20 | + |
| 21 | + for(final e in mima) { |
| 22 | + |
| 23 | + final titlesData = FlTitlesData( |
| 24 | + leftTitles: AxisTitles( |
| 25 | + sideTitles: SideTitles( |
| 26 | + showTitles: true, |
| 27 | + minIncluded: e[0], |
| 28 | + maxIncluded: e[1], |
| 29 | + reservedSize: 50, |
| 30 | + interval: 1, |
| 31 | + ),), |
| 32 | + rightTitles: const AxisTitles(), |
| 33 | + topTitles: const AxisTitles(), |
| 34 | + bottomTitles: const AxisTitles(), |
| 35 | + ); |
| 36 | + |
| 37 | + await tester.pumpWidget( |
| 38 | + MaterialApp( |
| 39 | + home: Scaffold( |
| 40 | + body: Center( |
| 41 | + child: SizedBox( |
| 42 | + width: viewSize.width, |
| 43 | + height: viewSize.height, |
| 44 | + child: LineChart( |
| 45 | + LineChartData( |
| 46 | + titlesData: titlesData, |
| 47 | + lineBarsData: [ |
| 48 | + LineChartBarData( |
| 49 | + spots: data, |
| 50 | + ), |
| 51 | + ], |
| 52 | + ), |
| 53 | + ), |
| 54 | + ), |
| 55 | + ), |
| 56 | + ), |
| 57 | + ), |
| 58 | + ); |
| 59 | + // Number of expected text widgets (titles) on the y-axis |
| 60 | + expect(find.byType(Text), findsNWidgets((e[0] ? 1 : 0) + (e[1] ? 1 : 0) + 1)); |
| 61 | + // Always there |
| 62 | + expect(find.text('1'), findsOneWidget); |
| 63 | + if(e[0]) { |
| 64 | + // Minimum included |
| 65 | + expect(find.text('0.5'), findsOneWidget); |
| 66 | + } |
| 67 | + if(e[1]) { |
| 68 | + // Maximum included |
| 69 | + expect(find.text('1.9'), findsOneWidget); |
| 70 | + } |
| 71 | + } |
| 72 | + }, |
| 73 | + ); |
| 74 | +} |
0 commit comments