/
main.dart
126 lines (101 loc) · 2.86 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:package_replaced/package_replaced.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
void main() {
runApp(MyApp());
PackageReplaced.setHandlerFunction(handlePackageReplaced);
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
void handlePackageReplaced() async {
await _incrementReplaceCount();
}
class _MyAppState extends State<MyApp> {
int _replaceCount = 0;
String _miscMessage = "";
@override
void initState() {
super.initState();
_showReplaceCount();
}
void _showReplaceCount() async {
final replaceCount = await _getReplaceCount();
setState(() {
_replaceCount = replaceCount;
});
}
void _artificalIncrement() async {
await _incrementReplaceCount();
_showReplaceCount();
}
void _artificalReset() async {
await _setReplaceCount(0);
_showReplaceCount();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('package_replaced plugin example app'),
),
body: Center(
child: Column(
children: <Widget>[
Text('Package replaced $_replaceCount time(s)'),
Text(_miscMessage)
],
),
),
floatingActionButton: Column(
children: <Widget>[
FloatingActionButton(
child: Text("+"),
onPressed: _artificalIncrement,
),
FloatingActionButton(
child: Text("0"),
onPressed: _artificalReset,
),
],
mainAxisAlignment: MainAxisAlignment.end,
)),
);
}
}
Future<File> getCountFile() async {
final myStorageDirectory =
await path_provider.getApplicationDocumentsDirectory();
var countFilePath = myStorageDirectory.path;
if (!countFilePath.endsWith("/")) {
countFilePath += "/";
}
countFilePath += "org.thebus.package_replaced_example.replaceCount";
return File(countFilePath);
}
Future<int> _getReplaceCount() async {
var retCount = 0;
final countFile = await getCountFile();
if (await countFile.exists()) {
retCount = int.tryParse((await countFile.readAsString()));
if (retCount == null) {
retCount = 0;
}
}
return retCount;
}
Future<void> _setReplaceCount(int newCountValue) async {
final countFile = await getCountFile();
if (!(await countFile.parent.exists())) {
await countFile.parent.create(recursive: true);
}
await countFile.writeAsString(newCountValue.toString());
}
Future<void> _incrementReplaceCount() async {
final currentReplaceCount = await _getReplaceCount();
await _setReplaceCount(currentReplaceCount + 1);
}