Skip to content

Instantly share code, notes, and snippets.

@JasperEssien2
Created May 9, 2022 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JasperEssien2/aa475a19b91010f46c095d797bbf0a82 to your computer and use it in GitHub Desktop.
Save JasperEssien2/aa475a19b91010f46c095d797bbf0a82 to your computer and use it in GitHub Desktop.
class MyApp extends StatefulWidget {
const MyApp({Key? key, required this.repository}) : super(key: key);
final Repository repository;
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _data = {
'1HR': [
const FlSpot(0, 120),
const FlSpot(30, 100),
const FlSpot(40, 150),
const FlSpot(50, 200),
const FlSpot(60, 140),
const FlSpot(70, 240),
const FlSpot(80, 145),
],
'1DAY': [
const FlSpot(0, 620),
const FlSpot(30, 500),
const FlSpot(40, 350),
const FlSpot(50, 700),
const FlSpot(60, 140),
const FlSpot(70, 40),
const FlSpot(80, 145),
],
'1WK': [
const FlSpot(0, 1720),
const FlSpot(30, 4100),
const FlSpot(40, 3150),
const FlSpot(50, 1200),
const FlSpot(60, 1140),
const FlSpot(70, 240),
const FlSpot(80, 145),
],
};
String _selected = '1HR';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 24),
child: LineChart(
LineChartData(
minX: 0,
maxX: getMaxX(_data[_selected]!),
minY: 0,
maxY: getMaxY(_data[_selected]!),
borderData: FlBorderData(show: false),
titlesData: FlTitlesData(
show: true,
bottomTitles: AxisTitles(
axisNameWidget: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: ['1HR', '1DAY', '1WK', '1MTH', '1YR']
.map((e) => bottomTitleWidget(e, _selected == e))
.toList(),
),
),
leftTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: false,
),
),
topTitles: AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
rightTitles: AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
),
gridData: FlGridData(show: false),
lineBarsData: [
LineChartBarData(
barWidth: 2,
isStrokeCapRound: true,
belowBarData: BarAreaData(
show: true,
cutOffY: 1500,
applyCutOffY: false,
gradient: LinearGradient(
colors: [Colors.deepPurple, Colors.white]
.map((color) => color.withOpacity(0.3))
.toList(),
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
color: Colors.deepPurple,
isCurved: true,
dotData: FlDotData(show: false),
spots: _data[_selected],
)
],
),
),
),
),
),
);
}
Widget bottomTitleWidget(String text, bool isSelected) {
final style = TextStyle(
color: isSelected ? Colors.deepPurple : const Color(0xff68737d),
fontWeight: FontWeight.bold,
fontSize: 16,
);
return GestureDetector(
onTap: () {
setState(() {
_selected = text;
});
},
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(text, style: style),
),
);
}
double getMaxX(List<FlSpot> data) {
final xData = data.map((e) => e.x).toList();
return xData.reduce(max);
}
double getMaxY(List<FlSpot> data) {
final yData = data.map((e) => e.y).toList();
return yData.reduce(max);
}
}
@mariamadebolahamzat
Copy link

God bless you Boss

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment