Skip to content

Commit 058a1c9

Browse files
FLUT-929668-[others]: updated sample
1 parent 95541c2 commit 058a1c9

File tree

8 files changed

+48
-83
lines changed

8 files changed

+48
-83
lines changed

assets/images/apple.png

25.7 KB
Loading

assets/images/facebook.png

-2.85 KB
Binary file not shown.

assets/images/instagram.png

-5.53 KB
Binary file not shown.

assets/images/orange.png

17.4 KB
Loading

assets/images/pears.png

8.77 KB
Loading

assets/images/snapchat.png

-3.13 KB
Binary file not shown.

assets/images/twitter.png

-3.19 KB
Binary file not shown.

lib/main.dart

Lines changed: 48 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class PieChartState extends State<PieChart> {
3232
ui.Image? _image1;
3333
ui.Image? _image2;
3434
ui.Image? _image3;
35-
ui.Image? _image4;
3635
Widget? _renderWidget;
3736

3837
@override
@@ -42,66 +41,48 @@ class PieChartState extends State<PieChart> {
4241
}
4342

4443
void _fetchImage() async {
45-
_image1 = await _loadImage('assets/images/instagram.png', [
46-
Colors.purple.withValues(alpha: 0.4),
47-
Colors.white,
48-
]);
49-
_image2 = await _loadImage('assets/images/facebook.png', [
50-
Colors.indigo.withValues(alpha: 0.4),
51-
Colors.white,
52-
]);
53-
_image3 = await _loadImage('assets/images/twitter.png', [
54-
Colors.blue.withValues(alpha: 0.4),
55-
Colors.white,
56-
]);
57-
_image4 = await _loadImage('assets/images/snapchat.png', [
58-
Colors.yellow.withValues(alpha: 0.4),
59-
Colors.white,
60-
]);
61-
62-
if (mounted) {
63-
setState(() {});
64-
}
65-
}
44+
final Completer<ImageInfo> completer = Completer<ImageInfo>();
45+
const ImageProvider imageProvider = AssetImage('assets/images/apple.png');
46+
imageProvider
47+
.resolve(ImageConfiguration.empty)
48+
.addListener(ImageStreamListener((ImageInfo info, bool _) async {
49+
completer.complete(info);
50+
final ImageInfo imageInfo = await completer.future;
51+
52+
_image1 = imageInfo.image;
53+
}));
6654

67-
Future<ui.Image> _loadImage(
68-
String assetPath, List<Color> gradientColors) async {
69-
final Completer<ui.Image> completer = Completer<ui.Image>();
70-
final ImageStream stream =
71-
AssetImage(assetPath).resolve(ImageConfiguration.empty);
72-
stream.addListener(ImageStreamListener((ImageInfo info, bool _) async {
73-
final ui.PictureRecorder recorder = ui.PictureRecorder();
74-
final Canvas canvas = Canvas(recorder);
75-
final Paint paint = Paint()
76-
..shader = ui.Gradient.linear(
77-
Offset(0, 0),
78-
Offset(info.image.width.toDouble(), info.image.height.toDouble()),
79-
gradientColors,
80-
);
81-
canvas.drawRect(
82-
Rect.fromLTWH(
83-
0,
84-
0,
85-
info.image.width.toDouble(),
86-
info.image.height.toDouble(),
87-
),
88-
paint,
89-
);
90-
canvas.drawImage(info.image, Offset.zero, Paint());
91-
final ui.Image finalImage = await recorder
92-
.endRecording()
93-
.toImage(info.image.width, info.image.height);
94-
completer.complete(finalImage);
55+
final Completer<ImageInfo> completer1 = Completer<ImageInfo>();
56+
const ImageProvider imageProvider1 = AssetImage('assets/images/orange.png');
57+
imageProvider1
58+
.resolve(ImageConfiguration.empty)
59+
.addListener(ImageStreamListener((ImageInfo info, bool _) async {
60+
completer1.complete(info);
61+
final ImageInfo imageInfo1 = await completer1.future;
62+
_image2 = imageInfo1.image;
63+
}));
64+
65+
final Completer<ImageInfo> completer2 = Completer<ImageInfo>();
66+
const ImageProvider imageProvider2 = AssetImage('assets/images/pears.png');
67+
imageProvider2
68+
.resolve(ImageConfiguration.empty)
69+
.addListener(ImageStreamListener((ImageInfo info, bool _) async {
70+
completer2.complete(info);
71+
final ImageInfo imageInfo2 = await completer2.future;
72+
73+
_image3 = imageInfo2.image;
74+
if (mounted) {
75+
setState(() {});
76+
}
9577
}));
96-
return completer.future;
9778
}
9879

99-
List<_ChartShaderData> _getChartData() {
80+
List<_ChartShaderData> _buildChartData() {
10081
return <_ChartShaderData>[
10182
_ChartShaderData(
102-
'Instagram',
103-
42,
104-
'42%',
83+
'Apple',
84+
45,
85+
'45%',
10586
ui.ImageShader(
10687
_image1!,
10788
TileMode.repeated,
@@ -110,9 +91,9 @@ class PieChartState extends State<PieChart> {
11091
),
11192
),
11293
_ChartShaderData(
113-
'Facebook',
114-
20,
115-
'20%',
94+
'Orange',
95+
30,
96+
'30%',
11697
ui.ImageShader(
11798
_image2!,
11899
TileMode.repeated,
@@ -121,36 +102,25 @@ class PieChartState extends State<PieChart> {
121102
),
122103
),
123104
_ChartShaderData(
124-
'Twitter',
125-
15,
126-
'15%',
105+
'Pears',
106+
25,
107+
'25%',
127108
ui.ImageShader(
128109
_image3!,
129110
TileMode.repeated,
130111
TileMode.repeated,
131112
Matrix4.identity().scaled(0.5).storage,
132113
),
133114
),
134-
_ChartShaderData(
135-
'Snapchat',
136-
23,
137-
'23%',
138-
ui.ImageShader(
139-
_image4!,
140-
TileMode.repeated,
141-
TileMode.repeated,
142-
Matrix4.identity().scaled(0.5).storage,
143-
),
144-
),
145115
];
146116
}
147117

148118
PieSeries<_ChartShaderData, String> _buildPieSeries() {
149119
return PieSeries<_ChartShaderData, String>(
150-
dataSource: _getChartData(),
120+
dataSource: _buildChartData(),
151121
xValueMapper: (_ChartShaderData data, int index) => data.x,
152122
yValueMapper: (_ChartShaderData data, int index) => data.y,
153-
strokeColor: Colors.purple,
123+
strokeColor: Colors.deepPurple.shade400,
154124
strokeWidth: 2,
155125
explode: true,
156126
explodeAll: true,
@@ -163,28 +133,25 @@ class PieChartState extends State<PieChart> {
163133
isVisible: true,
164134
labelPosition: ChartDataLabelPosition.outside,
165135
connectorLineSettings: ConnectorLineSettings(
166-
color: Colors.purple,
136+
color: Colors.deepPurple.shade400,
167137
width: 2,
168138
length: '15%',
169139
type: ConnectorType.line,
170140
),
171141
textStyle: TextStyle(
172142
fontSize: 16,
173-
color: Colors.purple,
143+
color: Colors.deepPurple.shade400,
174144
fontWeight: FontWeight.bold,
175145
)),
176146
);
177147
}
178148

179149
@override
180150
Widget build(BuildContext context) {
181-
if (_image1 != null &&
182-
_image2 != null &&
183-
_image3 != null &&
184-
_image4 != null) {
151+
if (_image1 != null && _image2 != null && _image3 != null) {
185152
_renderWidget = SfCircularChart(
186153
title: ChartTitle(
187-
text: 'Social Media App Usage in India',
154+
text: 'Sales comparison of fruits in a shop',
188155
textStyle: TextStyle(
189156
fontWeight: FontWeight.bold,
190157
),
@@ -210,8 +177,6 @@ class PieChartState extends State<PieChart> {
210177
_image1 = null;
211178
_image2 = null;
212179
_image3 = null;
213-
_image4 = null;
214-
215180
super.dispose();
216181
}
217182
}

0 commit comments

Comments
 (0)