Skip to content

Instantly share code, notes, and snippets.

@imaNNeo
Created January 22, 2024 21:14
Show Gist options
  • Save imaNNeo/0b373eff097faf7a9a33e37ad1707fef to your computer and use it in GitHub Desktop.
Save imaNNeo/0b373eff097faf7a9a33e37ad1707fef to your computer and use it in GitHub Desktop.
Line Chart Sample 10 with DropDown
import 'dart:async';
import 'dart:math' as math;
import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
import 'package:flutter/material.dart';
class LineChartSample10 extends StatelessWidget {
LineChartSample10({super.key});
final vals = [1, 2, 3, 4];
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DropdownMenu<int>(
dropdownMenuEntries: vals
.map((e) => DropdownMenuEntry(label: e.toString(), value: e))
.toList(),
),
const SizedBox(height: 12),
const SizedBox(
height: 12,
),
const _LiveChart(),
],
);
}
}
class _LiveChart extends StatefulWidget {
const _LiveChart({super.key});
final Color sinColor = AppColors.contentColorBlue;
final Color cosColor = AppColors.contentColorPink;
@override
State<_LiveChart> createState() => _LiveChartState();
}
class _LiveChartState extends State<_LiveChart> {
final limitCount = 400;
final sinPoints = <FlSpot>[];
final cosPoints = <FlSpot>[];
double xValue = 0;
double step = 0.05;
late Timer timer;
@override
void initState() {
super.initState();
timer = Timer.periodic(const Duration(milliseconds: 40), (timer) {
while (sinPoints.length > limitCount) {
sinPoints.removeAt(0);
cosPoints.removeAt(0);
}
setState(() {
sinPoints.add(FlSpot(xValue, math.sin(xValue)));
cosPoints.add(FlSpot(xValue, math.cos(xValue)));
});
xValue += step;
});
}
final vals = [1, 2, 3, 4];
@override
Widget build(BuildContext context) {
return cosPoints.isNotEmpty
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'x: ${xValue.toStringAsFixed(1)}',
style: const TextStyle(
color: AppColors.mainTextColor2,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
Text(
'sin: ${sinPoints.last.y.toStringAsFixed(1)}',
style: TextStyle(
color: widget.sinColor,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
Text(
'cos: ${cosPoints.last.y.toStringAsFixed(1)}',
style: TextStyle(
color: widget.cosColor,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(
height: 12,
),
AspectRatio(
aspectRatio: 1.5,
child: Padding(
padding: const EdgeInsets.only(bottom: 24.0),
child: RepaintBoundary(
child: LineChart(
LineChartData(
minY: -1,
maxY: 1,
minX: sinPoints.first.x,
maxX: sinPoints.last.x,
lineTouchData: const LineTouchData(enabled: false),
clipData: const FlClipData.all(),
gridData: const FlGridData(
show: true,
drawVerticalLine: false,
),
borderData: FlBorderData(show: false),
lineBarsData: [
sinLine(sinPoints),
cosLine(cosPoints),
],
titlesData: const FlTitlesData(
show: false,
),
),
),
),
),
)
],
)
: Container();
}
LineChartBarData sinLine(List<FlSpot> points) {
return LineChartBarData(
spots: points,
dotData: const FlDotData(
show: false,
),
gradient: LinearGradient(
colors: [widget.sinColor.withOpacity(0), widget.sinColor],
stops: const [0.1, 1.0],
),
barWidth: 4,
isCurved: false,
);
}
LineChartBarData cosLine(List<FlSpot> points) {
return LineChartBarData(
spots: points,
dotData: const FlDotData(
show: false,
),
gradient: LinearGradient(
colors: [widget.cosColor.withOpacity(0), widget.cosColor],
stops: const [0.1, 1.0],
),
barWidth: 4,
isCurved: false,
);
}
@override
void dispose() {
timer.cancel();
super.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment