Skip to content

Commit 3ca18bc

Browse files
FLUT-892072-[others][flutter]: Updated the sample
1 parent 19819d2 commit 3ca18bc

File tree

1 file changed

+134
-92
lines changed

1 file changed

+134
-92
lines changed

lib/main.dart

Lines changed: 134 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -9,93 +9,119 @@ class MyApp extends StatelessWidget {
99
@override
1010
Widget build(BuildContext context) {
1111
return MaterialApp(
12-
theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: false),
13-
home: _MyHomePage(),
12+
debugShowCheckedModeBanner: false,
13+
theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
14+
home: PieChart(),
1415
);
1516
}
1617
}
1718

18-
class _MyHomePage extends StatefulWidget {
19+
class PieChart extends StatefulWidget {
1920
@override
20-
_MyHomePageState createState() => _MyHomePageState();
21+
_PieChartState createState() => _PieChartState();
2122
}
2223

23-
class _MyHomePageState extends State<_MyHomePage> {
24+
class _PieChartState extends State<PieChart> {
25+
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
26+
27+
late List<_ElectronicData> _electronicProducts;
2428
late LabelIntersectAction _labelIntersectAction;
25-
late List<_ChartSampleData> _electronicProducts;
29+
late String _selectedItem;
30+
31+
List<String>? _labelIntersectActionMode;
2632

2733
@override
2834
void initState() {
35+
_selectedItem = 'shift';
2936
_electronicProducts = _buildChartData();
3037
_labelIntersectAction = LabelIntersectAction.shift;
38+
_labelIntersectActionMode = <String>['shift', 'none', 'hide'].toList();
3139
super.initState();
3240
}
3341

34-
List<_ChartSampleData> _buildChartData() {
42+
List<_ElectronicData> _buildChartData() {
3543
return [
36-
_ChartSampleData('Smartphones', 150),
37-
_ChartSampleData('Laptops', 145),
38-
_ChartSampleData('Tablets', 140),
39-
_ChartSampleData('Smartwatches', 138),
40-
_ChartSampleData('Headphones', 135),
41-
_ChartSampleData('Smart Home Devices', 133),
42-
_ChartSampleData('TVs', 130),
43-
_ChartSampleData('Gaming Consoles', 128),
44-
_ChartSampleData('Monitors', 125),
45-
_ChartSampleData('Printers', 123),
46-
_ChartSampleData('Cameras', 120),
47-
_ChartSampleData('Routers', 118),
48-
_ChartSampleData('External Storage', 115),
49-
_ChartSampleData('Projectors', 113),
50-
_ChartSampleData('VR Headsets', 110),
51-
_ChartSampleData('Microphones', 108),
52-
_ChartSampleData('Webcams', 105),
53-
_ChartSampleData('Smart Glasses', 103),
54-
_ChartSampleData('Fitness Trackers', 100),
55-
_ChartSampleData('Bluetooth Speakers', 98),
56-
_ChartSampleData('E-readers', 95),
57-
_ChartSampleData('Network Switches', 93),
58-
_ChartSampleData('Docking Stations', 90),
59-
_ChartSampleData('Power Banks', 88),
60-
_ChartSampleData('Smart Thermostats', 85),
61-
_ChartSampleData('Surge Protectors', 83),
62-
_ChartSampleData('Home Security Cameras', 80),
63-
_ChartSampleData('Smart Plugs', 78),
64-
_ChartSampleData('Electronic Keyboards', 75),
65-
_ChartSampleData('Digital Picture Frames', 73),
66-
_ChartSampleData('Satellite Radios', 70),
67-
_ChartSampleData('Car Dash Cameras', 68),
68-
_ChartSampleData('Digital Pianos', 65),
69-
_ChartSampleData('Smart Doorbells', 63),
70-
_ChartSampleData('Weather Stations', 60),
71-
_ChartSampleData('Robotic Vacuums', 58),
72-
_ChartSampleData('Home Audio Systems', 55),
44+
_ElectronicData('Smartphones', 150),
45+
_ElectronicData('Laptops', 145),
46+
_ElectronicData('Tablets', 140),
47+
_ElectronicData('Smartwatches', 138),
48+
_ElectronicData('Headphones', 135),
49+
_ElectronicData('Smart Home Devices', 133),
50+
_ElectronicData('TVs', 130),
51+
_ElectronicData('Gaming Consoles', 128),
52+
_ElectronicData('Monitors', 125),
53+
_ElectronicData('Printers', 123),
54+
_ElectronicData('Cameras', 120),
55+
_ElectronicData('Routers', 118),
56+
_ElectronicData('External Storage', 115),
57+
_ElectronicData('Projectors', 113),
58+
_ElectronicData('VR Headsets', 110),
59+
_ElectronicData('Microphones', 108),
60+
_ElectronicData('Webcams', 105),
61+
_ElectronicData('Smart Glasses', 103),
62+
_ElectronicData('Fitness Trackers', 100),
63+
_ElectronicData('Bluetooth Speakers', 98),
64+
_ElectronicData('E-readers', 95),
65+
_ElectronicData('Network Switches', 93),
66+
_ElectronicData('Docking Stations', 90),
67+
_ElectronicData('Power Banks', 88),
68+
_ElectronicData('Smart Thermostats', 85),
69+
_ElectronicData('Surge Protectors', 83),
70+
_ElectronicData('Home Security Cameras', 80),
71+
_ElectronicData('Smart Plugs', 78),
72+
_ElectronicData('Electronic Keyboards', 75),
73+
_ElectronicData('Digital Picture Frames', 73),
74+
_ElectronicData('Satellite Radios', 70),
75+
_ElectronicData('Car Dash Cameras', 68),
76+
_ElectronicData('Digital Pianos', 65),
77+
_ElectronicData('Smart Doorbells', 63),
78+
_ElectronicData('Weather Stations', 60),
79+
_ElectronicData('Robotic Vacuums', 58),
80+
_ElectronicData('Home Audio Systems', 55),
7381
];
7482
}
7583

7684
@override
7785
Widget build(BuildContext context) {
7886
return Scaffold(
87+
key: _scaffoldKey,
88+
appBar: AppBar(
89+
title: const Text(''),
90+
actions: [
91+
IconButton(
92+
icon: const Icon(Icons.menu),
93+
onPressed: () {
94+
setState(() {
95+
_scaffoldKey.currentState?.openEndDrawer();
96+
});
97+
},
98+
),
99+
],
100+
),
101+
endDrawer: Drawer(
102+
child: _buildForm(),
103+
),
79104
body: Row(
80105
children: [
81106
Expanded(
82-
child: _buildCircularChartWithLabels(),
107+
flex: 3,
108+
child: _buildCircularChart(),
83109
),
84-
_buildLabelControlButtons(),
85110
],
86111
),
87112
);
88113
}
89114

90-
/// Builds the circular chart with data labels and custom connector line settings.
91-
Widget _buildCircularChartWithLabels() {
115+
/// Builds the circular chart with data labels and
116+
/// custom connector line settings.
117+
Widget _buildCircularChart() {
92118
return SfCircularChart(
93119
series: <CircularSeries>[
94-
PieSeries<_ChartSampleData, String>(
120+
PieSeries<_ElectronicData, String>(
95121
dataSource: _electronicProducts,
96-
xValueMapper: (_ChartSampleData data, int index) => data.x,
97-
yValueMapper: (_ChartSampleData data, int index) => data.y,
98-
dataLabelMapper: (_ChartSampleData data, int index) =>
122+
xValueMapper: (_ElectronicData data, int index) => data.x,
123+
yValueMapper: (_ElectronicData data, int index) => data.y,
124+
dataLabelMapper: (_ElectronicData data, int index) =>
99125
'${data.x}: ${data.y}',
100126
dataLabelSettings: DataLabelSettings(
101127
isVisible: true,
@@ -112,56 +138,72 @@ class _MyHomePageState extends State<_MyHomePage> {
112138
);
113139
}
114140

115-
/// Builds the buttons to control label intersection behavior (Shift, Hide, None).
116-
Widget _buildLabelControlButtons() {
117-
return Row(
118-
mainAxisAlignment: MainAxisAlignment.center,
119-
children: [
120-
Padding(
121-
padding: const EdgeInsets.all(50),
122-
child: Column(
123-
mainAxisAlignment: MainAxisAlignment.center,
141+
/// Method to build the form with text and dropdown.
142+
Widget _buildForm() {
143+
return Padding(
144+
padding: const EdgeInsets.all(15),
145+
child: Column(
146+
crossAxisAlignment: CrossAxisAlignment.start,
147+
children: [
148+
Row(
124149
children: [
125-
const Text(
126-
"LabelIntersectAction",
127-
style: TextStyle(
128-
fontWeight: FontWeight.bold,
150+
const Expanded(
151+
flex: 7,
152+
child: Text(
153+
"Label intersect action",
154+
style: TextStyle(fontWeight: FontWeight.bold),
129155
),
130156
),
131-
TextButton(
132-
onPressed: () {
133-
setState(() {
134-
_labelIntersectAction = LabelIntersectAction.shift;
135-
});
136-
},
137-
child: const Text('Shift'),
138-
),
139-
TextButton(
140-
onPressed: () {
141-
setState(() {
142-
_labelIntersectAction = LabelIntersectAction.hide;
143-
});
144-
},
145-
child: const Text('Hide'),
146-
),
147-
TextButton(
148-
onPressed: () {
149-
setState(() {
150-
_labelIntersectAction = LabelIntersectAction.none;
151-
});
152-
},
153-
child: const Text('None'),
157+
Expanded(
158+
flex: 3,
159+
child: Container(
160+
alignment: Alignment.centerRight,
161+
child: DropdownButton<String>(
162+
focusColor: Colors.transparent,
163+
underline: Container(
164+
color: const Color(0xFFBDBDBD),
165+
height: 1,
166+
),
167+
value: _selectedItem,
168+
items: _labelIntersectActionMode!.map((String value) {
169+
return DropdownMenuItem<String>(
170+
value: value,
171+
child: Text(value,
172+
style: const TextStyle(color: Colors.black)),
173+
);
174+
}).toList(),
175+
onChanged: (dynamic value) {
176+
_updateLabelIntersectAction(value.toString());
177+
},
178+
),
179+
),
154180
),
155181
],
156182
),
157-
),
158-
],
183+
],
184+
),
159185
);
160186
}
187+
188+
/// Method to update the change in label intersect action.
189+
void _updateLabelIntersectAction(String item) {
190+
setState(() {
191+
_selectedItem = item;
192+
if (_selectedItem == 'shift') {
193+
_labelIntersectAction = LabelIntersectAction.shift;
194+
}
195+
if (_selectedItem == 'none') {
196+
_labelIntersectAction = LabelIntersectAction.none;
197+
}
198+
if (_selectedItem == 'hide') {
199+
_labelIntersectAction = LabelIntersectAction.hide;
200+
}
201+
});
202+
}
161203
}
162204

163-
class _ChartSampleData {
164-
_ChartSampleData(this.x, this.y);
205+
class _ElectronicData {
206+
_ElectronicData(this.x, this.y);
165207
String x;
166208
int y;
167209
}

0 commit comments

Comments
 (0)