1+ // ignore_for_file: must_be_immutable
2+
13import 'package:flutter/material.dart' ;
24import 'package:intl/intl.dart' ;
35
@@ -11,26 +13,10 @@ class SynchronizedTrackball extends StatelessWidget {
1113
1214 @override
1315 Widget build (BuildContext context) {
14- return const MaterialApp (
16+ return MaterialApp (
1517 debugShowCheckedModeBanner: false ,
1618 title: 'Synchronized Trackball' ,
17- home: MyHomePage (),
18- );
19- }
20- }
21-
22- class MyHomePage extends StatefulWidget {
23- const MyHomePage ({super .key});
24-
25- @override
26- _MyHomePageState createState () => _MyHomePageState ();
27- }
28-
29- class _MyHomePageState extends State <MyHomePage > {
30- @override
31- Widget build (BuildContext context) {
32- return const SafeArea (
33- child: Scaffold (
19+ home: Scaffold (
3420 backgroundColor: Colors .white,
3521 body: Row (
3622 children: < Widget > [
@@ -43,26 +29,18 @@ class _MyHomePageState extends State<MyHomePage> {
4329 }
4430}
4531
46- TrackballBehavior trackball1 =
32+ TrackballBehavior _baseTrackball =
4733 TrackballBehavior (enable: true , activationMode: ActivationMode .singleTap);
48- TrackballBehavior trackball2 =
34+ TrackballBehavior _targetTrackball =
4935 TrackballBehavior (enable: true , activationMode: ActivationMode .singleTap);
50-
51- ChartSeriesController ? _controller1;
52- ChartSeriesController ? _controller2;
53- DataModel dataModel = DataModel ();
36+ ChartSeriesController ? _baseSeriesController;
37+ ChartSeriesController ? _targetSeriesController;
38+ DataModel _dataModel = DataModel ();
5439Offset ? _position;
5540
56- class FirstChart extends StatefulWidget {
57- const FirstChart ({super .key});
58-
59- @override
60- State <StatefulWidget > createState () {
61- return FirstChartState ();
62- }
63- }
41+ class FirstChart extends StatelessWidget {
42+ FirstChart ({super .key});
6443
65- class FirstChartState extends State <FirstChart > {
6644 bool _isInteractive = false ;
6745
6846 @override
@@ -73,14 +51,14 @@ class FirstChartState extends State<FirstChart> {
7351 },
7452 onChartTouchInteractionUp: (ChartTouchInteractionArgs tapArgs) {
7553 _isInteractive = false ;
76- trackball2 .hide ();
54+ _targetTrackball .hide ();
7755 },
7856 onTrackballPositionChanging: (TrackballArgs trackballArgs) {
7957 if (_isInteractive) {
80- _position = _controller1 ! .pointToPixel (
58+ _position = _baseSeriesController ! .pointToPixel (
8159 trackballArgs.chartPointInfo.chartPoint! ,
8260 );
83- trackball2 .show (_position! .dx, _position! .dy, 'pixel' );
61+ _targetTrackball .show (_position! .dx, _position! .dy, 'pixel' );
8462 }
8563 },
8664 primaryXAxis: DateTimeAxis (
@@ -96,32 +74,25 @@ class FirstChartState extends State<FirstChart> {
9674 interval: 0.025 ,
9775 ),
9876 title: const ChartTitle (text: 'Chart 1' ),
99- trackballBehavior: trackball1 ,
77+ trackballBehavior: _baseTrackball ,
10078 series: < LineSeries <SalesData , DateTime >> [
10179 LineSeries <SalesData , DateTime >(
10280 color: const Color .fromRGBO (99 , 85 , 199 , 1 ),
103- dataSource: dataModel .data,
81+ dataSource: _dataModel .data,
10482 xValueMapper: (SalesData sales, int index) => sales.dateTime,
10583 yValueMapper: (SalesData sales, int index) => sales.y,
10684 onRendererCreated: (ChartSeriesController controller) {
107- _controller2 = controller;
85+ _targetSeriesController = controller;
10886 },
10987 ),
11088 ],
11189 );
11290 }
11391}
11492
115- class SecondChart extends StatefulWidget {
116- const SecondChart ({super .key});
117-
118- @override
119- State <StatefulWidget > createState () {
120- return SecondChartState ();
121- }
122- }
93+ class SecondChart extends StatelessWidget {
94+ SecondChart ({super .key});
12395
124- class SecondChartState extends State <SecondChart > {
12596 bool _isInteractive = false ;
12697
12798 @override
@@ -132,14 +103,14 @@ class SecondChartState extends State<SecondChart> {
132103 },
133104 onChartTouchInteractionUp: (ChartTouchInteractionArgs tapArgs) {
134105 _isInteractive = false ;
135- trackball1 .hide ();
106+ _baseTrackball .hide ();
136107 },
137108 onTrackballPositionChanging: (TrackballArgs trackballArgs) {
138109 if (_isInteractive) {
139- _position = _controller2 ! .pointToPixel (
110+ _position = _targetSeriesController ! .pointToPixel (
140111 trackballArgs.chartPointInfo.chartPoint! ,
141112 );
142- trackball1 .show (_position! .dx, _position! .dy, 'pixel' );
113+ _baseTrackball .show (_position! .dx, _position! .dy, 'pixel' );
143114 }
144115 },
145116 primaryXAxis: DateTimeAxis (
@@ -155,15 +126,15 @@ class SecondChartState extends State<SecondChart> {
155126 interval: 0.025 ,
156127 ),
157128 title: const ChartTitle (text: 'Chart 2' ),
158- trackballBehavior: trackball2 ,
129+ trackballBehavior: _targetTrackball ,
159130 series: < LineSeries <SalesData , DateTime >> [
160131 LineSeries <SalesData , DateTime >(
161132 color: const Color .fromRGBO (99 , 85 , 199 , 1 ),
162- dataSource: dataModel .data,
133+ dataSource: _dataModel .data,
163134 xValueMapper: (SalesData sales, int index) => sales.dateTime,
164135 yValueMapper: (SalesData sales, int index) => sales.y,
165136 onRendererCreated: (ChartSeriesController controller) {
166- _controller1 = controller;
137+ _baseSeriesController = controller;
167138 },
168139 ),
169140 ],
0 commit comments