1
- // ignore_for_file: prefer_typing_uninitialized_variables
2
-
3
1
import 'dart:io' ;
4
-
2
+ import 'package:home_widget/home_widget.dart' ;
3
+ import 'dart:ui' ;
5
4
import 'package:flutter/material.dart' ;
5
+ import 'package:flutter/rendering.dart' ;
6
6
import 'package:get/get.dart' ;
7
+ import 'package:shared_preferences/shared_preferences.dart' ;
7
8
import 'package:syncfusion_flutter_charts/charts.dart' ;
8
9
import 'package:taskwarrior/api_service.dart' ;
9
10
import 'package:taskwarrior/app/models/json/task.dart' ;
@@ -16,7 +17,8 @@ import 'package:taskwarrior/app/utils/constants/taskwarrior_fonts.dart';
16
17
import 'package:taskwarrior/app/utils/constants/utilites.dart' ;
17
18
import 'package:taskwarrior/app/utils/gen/fonts.gen.dart' ;
18
19
import 'package:taskwarrior/app/utils/app_settings/app_settings.dart' ;
19
-
20
+ import 'package:path_provider/path_provider.dart' ;
21
+ import 'package:flutter/services.dart' ;
20
22
import 'package:tutorial_coach_mark/tutorial_coach_mark.dart' ;
21
23
22
24
class ReportsController extends GetxController
@@ -34,6 +36,69 @@ class ReportsController extends GetxController
34
36
late Storage storage;
35
37
var storageWidget;
36
38
39
+ final GlobalKey _chartKey = GlobalKey ();
40
+
41
+ GlobalKey get chartKey => _chartKey;
42
+
43
+ Future <void > captureChart () async {
44
+ try {
45
+ if (chartKey.currentContext == null ) {
46
+ print ('Error: chartKey.currentContext is null' );
47
+ return ;
48
+ }
49
+
50
+ RenderRepaintBoundary ? boundary =
51
+ chartKey.currentContext! .findRenderObject () as RenderRepaintBoundary ? ;
52
+
53
+ if (boundary == null ) {
54
+ print ('Error: boundary is null' );
55
+ return ;
56
+ }
57
+
58
+ final image = await boundary.toImage ();
59
+ final byteData = await image.toByteData (format: ImageByteFormat .png);
60
+
61
+ if (byteData == null ) {
62
+ print ('Error: byteData is null' );
63
+ return ;
64
+ }
65
+
66
+ final pngBytes = byteData.buffer.asUint8List ();
67
+
68
+ // Get the documents directory
69
+ final directory = await getApplicationDocumentsDirectory ();
70
+ final imagePath = '${directory .path }/daily_burndown_chart.png' ;
71
+
72
+ // Save the image to the documents directory
73
+ File imgFile = File (imagePath);
74
+ await imgFile.writeAsBytes (pngBytes);
75
+ print ('Image saved to: $imagePath ' );
76
+
77
+ // Save the image path to HomeWidget
78
+ await HomeWidget .saveWidgetData <String >('chart_image' , imagePath);
79
+
80
+ // Verify that the file exists
81
+ if (await imgFile.exists ()) {
82
+ print ('Image file exists!' );
83
+ } else {
84
+ print ('Image file does not exist!' );
85
+ }
86
+
87
+ // Add a delay before sending the broadcast
88
+ await Future .delayed (const Duration (milliseconds: 500 ));
89
+
90
+ // Send a broadcast to update the widget
91
+ const platform = MethodChannel ('com.example.taskwarrior/widget' );
92
+ try {
93
+ await platform.invokeMethod ('updateWidget' );
94
+ } on PlatformException catch (e) {
95
+ print ("Failed to Invoke: '${e .message }'." );
96
+ }
97
+ } catch (e) {
98
+ print ('Error capturing chart: $e ' );
99
+ }
100
+ }
101
+
37
102
// void _initReportsTour() {
38
103
// tutorialCoachMark = TutorialCoachMark(
39
104
// targets: reportsDrawer(
0 commit comments