Created
April 13, 2025 21:22
-
-
Save imaNNeo/bfed00864918f921739d60da6cff6a00 to your computer and use it in GitHub Desktop.
Showcase some points and enable default interactions after the first user interaction, #1873
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:fl_chart/fl_chart.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage({ | |
super.key, | |
this.defaultShowingPointIndex = 7, | |
this.data = const [1, 2, 2, 4, 3, 6, 5, 8, 5, 6, 4], | |
}); | |
final int defaultShowingPointIndex; | |
final List<double> data; | |
@override | |
State<HomePage> createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
bool firstInteractionHappened = false; | |
@override | |
Widget build(BuildContext context) { | |
final mainLineBar = LineChartBarData( | |
spots: widget.data.asMap().entries.map((entry) { | |
final index = entry.key; | |
final value = entry.value; | |
return FlSpot(index.toDouble(), value); | |
}).toList(), | |
showingIndicators: firstInteractionHappened | |
? [] | |
: [ | |
widget.defaultShowingPointIndex, | |
], | |
); | |
return Scaffold( | |
body: Center( | |
child: AspectRatio( | |
aspectRatio: 2.0, | |
child: LineChart( | |
LineChartData( | |
minX: 0, | |
maxX: 10, | |
minY: 0, | |
maxY: 10, | |
lineBarsData: [mainLineBar], | |
lineTouchData: LineTouchData( | |
handleBuiltInTouches: firstInteractionHappened, | |
touchCallback: firstInteractionHappened | |
? null | |
: ( | |
FlTouchEvent event, | |
LineTouchResponse? touchResponse, | |
) { | |
if (event is FlTapUpEvent) { | |
setState(() { | |
firstInteractionHappened = true; | |
}); | |
} | |
}, | |
), | |
showingTooltipIndicators: firstInteractionHappened | |
? [] | |
: [ | |
ShowingTooltipIndicators([ | |
LineBarSpot( | |
mainLineBar, | |
0, | |
mainLineBar.spots[widget.defaultShowingPointIndex], | |
), | |
]), | |
], | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
CleanShot.2025-04-13.at.23.24.26.mp4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answered to #1873