-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
613 additions
and
512 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,106 @@ | ||
import 'package:example/radar_chart/radar_chart_page.dart'; | ||
import 'package:example/scatter_chart/scatter_chart_page.dart'; | ||
import 'package:fl_chart/fl_chart.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'bar_chart/bar_chart_page.dart'; | ||
import 'bar_chart/bar_chart_page2.dart'; | ||
import 'bar_chart/bar_chart_page3.dart'; | ||
import 'line_chart/line_chart_page.dart'; | ||
import 'line_chart/line_chart_page2.dart'; | ||
import 'line_chart/line_chart_page3.dart'; | ||
import 'line_chart/line_chart_page4.dart'; | ||
import 'pie_chart/pie_chart_page.dart'; | ||
import 'utils/platform_info.dart'; | ||
import 'scatter_chart/scatter_chart_page.dart'; | ||
|
||
void main() => runApp(const MyApp()); | ||
|
||
class MyApp extends StatelessWidget { | ||
const MyApp({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
return const MaterialApp( | ||
title: 'FlChart Demo', | ||
showPerformanceOverlay: false, | ||
theme: ThemeData( | ||
primaryColor: const Color(0xff262545), | ||
primaryColorDark: const Color(0xff201f39), | ||
brightness: Brightness.dark, | ||
), | ||
home: const MyHomePage(title: 'fl_chart'), | ||
home: MyHomePage(), | ||
); | ||
} | ||
} | ||
|
||
class MyHomePage extends StatefulWidget { | ||
const MyHomePage({Key? key, required this.title}) : super(key: key); | ||
final String title; | ||
const MyHomePage({Key? key}) : super(key: key); | ||
|
||
@override | ||
_MyHomePageState createState() => _MyHomePageState(); | ||
} | ||
|
||
class _MyHomePageState extends State<MyHomePage> { | ||
int _currentPage = 0; | ||
final List<ShowingTooltipIndicators> _showingTouchedTooltips = []; | ||
|
||
final _controller = PageController(initialPage: 0); | ||
final _duration = const Duration(milliseconds: 300); | ||
final _curve = Curves.easeInOutCubic; | ||
final _pages = const [ | ||
LineChartPage(), | ||
BarChartPage(), | ||
BarChartPage2(), | ||
PieChartPage(), | ||
LineChartPage2(), | ||
LineChartPage3(), | ||
LineChartPage4(), | ||
BarChartPage3(), | ||
ScatterChartPage(), | ||
RadarChartPage(), | ||
]; | ||
|
||
bool get isDesktopOrWeb => PlatformInfo().isDesktopOrWeb(); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_controller.addListener(() { | ||
setState(() { | ||
_currentPage = _controller.page!.round(); | ||
}); | ||
}); | ||
} | ||
final Map<int, List<int>> _showingTouchedIndicators = {}; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
body: SafeArea( | ||
child: PageView( | ||
physics: isDesktopOrWeb | ||
? const NeverScrollableScrollPhysics() | ||
: const AlwaysScrollableScrollPhysics(), | ||
controller: _controller, | ||
children: _pages, | ||
), | ||
), | ||
bottomNavigationBar: isDesktopOrWeb | ||
? Container( | ||
padding: const EdgeInsets.all(16), | ||
color: Colors.transparent, | ||
child: Row( | ||
mainAxisSize: MainAxisSize.max, | ||
children: [ | ||
Visibility( | ||
visible: _currentPage != 0, | ||
child: FloatingActionButton( | ||
onPressed: () => _controller.previousPage( | ||
duration: _duration, curve: _curve), | ||
child: const Icon(Icons.chevron_left_rounded), | ||
), | ||
child: Center( | ||
child: SizedBox( | ||
width: 200, | ||
height: 100, | ||
child: LineChart( | ||
LineChartData( | ||
lineBarsData: [ | ||
LineChartBarData( | ||
colors: [Colors.green], | ||
spots: const [ | ||
FlSpot(0, 0), | ||
FlSpot(1, 1), | ||
FlSpot(2, 1), | ||
], | ||
showingIndicators: _showingTouchedIndicators[0] ?? [], | ||
), | ||
const Spacer(), | ||
Visibility( | ||
visible: _currentPage != _pages.length - 1, | ||
child: FloatingActionButton( | ||
onPressed: () => _controller.nextPage( | ||
duration: _duration, curve: _curve), | ||
child: const Icon(Icons.chevron_right_rounded), | ||
), | ||
LineChartBarData( | ||
colors: [Colors.red], | ||
spots: const [ | ||
FlSpot(0, 3), | ||
FlSpot(1, 2), | ||
FlSpot(2, 0), | ||
], | ||
showingIndicators: _showingTouchedIndicators[1] ?? [], | ||
), | ||
], | ||
showingTooltipIndicators: _showingTouchedTooltips, | ||
lineTouchData: LineTouchData( | ||
enabled: true, | ||
handleBuiltInTouches: false, | ||
touchCallback: (event, touchResponse) { | ||
if (!event.isInterestedForInteractions || | ||
touchResponse?.lineBarSpots == null || | ||
touchResponse!.lineBarSpots!.isEmpty) { | ||
setState(() { | ||
_showingTouchedTooltips.clear(); | ||
_showingTouchedIndicators.clear(); | ||
}); | ||
return; | ||
} | ||
|
||
const showOnlyOnLineIndex = 0; | ||
setState(() { | ||
final sortedLineSpots = List.of( | ||
touchResponse.lineBarSpots!.where((element) => | ||
element.barIndex == showOnlyOnLineIndex)); | ||
sortedLineSpots | ||
.sort((spot1, spot2) => spot2.y.compareTo(spot1.y)); | ||
|
||
_showingTouchedIndicators.clear(); | ||
|
||
final touchedBarSpot = | ||
touchResponse.lineBarSpots![showOnlyOnLineIndex]; | ||
final barPos = touchedBarSpot.barIndex; | ||
_showingTouchedIndicators[barPos] = [ | ||
touchedBarSpot.spotIndex | ||
]; | ||
|
||
_showingTouchedTooltips.clear(); | ||
_showingTouchedTooltips | ||
.add(ShowingTooltipIndicators(sortedLineSpots)); | ||
}); | ||
}, | ||
), | ||
), | ||
) | ||
: null, | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.