Skip to content

Instantly share code, notes, and snippets.

@imaNNeo
Created April 13, 2025 21:22
Show Gist options
  • Save imaNNeo/bfed00864918f921739d60da6cff6a00 to your computer and use it in GitHub Desktop.
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
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],
),
]),
],
),
),
),
),
);
}
}
@imaNNeo
Copy link
Author

imaNNeo commented Apr 13, 2025

Answered to #1873

@imaNNeo
Copy link
Author

imaNNeo commented Apr 13, 2025

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