-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFileIOWidget.dart
225 lines (208 loc) · 6.02 KB
/
FileIOWidget.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/common/CommonShowModel.dart';
import 'package:path_provider/path_provider.dart';
///author: yang yi
///email: yangyirunning@163.com
class FileIOWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return FileIOState();
}
}
class FileIOState extends State<FileIOWidget> {
String cacheDir = "";
String packageDir = "";
String sdCardDir = "";
String content = "";
@override
Widget build(BuildContext context) {
// TODO: implement build
final arg = ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: getAppBar(arg),
body: Column(
children: <Widget>[
Container(
child: GridView.builder(
padding: EdgeInsets.all(10),
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 10,
crossAxisSpacing: 1.5,
),
itemBuilder: (BuildContext context, int index) {
return getRaisedButtonList()[index];
},
itemCount: getRaisedButtonList().length,
),
),
Expanded(
child: Container(
alignment: Alignment.center,
margin: EdgeInsets.fromLTRB(10, 0, 10, 10),
width: MediaQuery.of(context).size.width,
color: Colors.blue[100],
child: Text(
content,
style: TextStyle(fontSize: 20, color: Colors.grey),
),
),
),
],
),
);
}
List<RaisedButton> getRaisedButtonList() {
List<RaisedButton> raisedButtonList = [];
RaisedButton cacheButton = RaisedButton(
child: Text(
"获取缓存路径",
style: TextStyle(fontSize: 16),
),
color: Colors.blue,
textColor: Colors.white,
onPressed: () {
setState(() {
//相当于Android中的getCacheDir()
getCacheDir().then((v) {
cacheDir = v;
});
content = cacheDir;
print(content);
});
},
);
RaisedButton packageButton = RaisedButton(
child: Text(
"获取应用包路径",
style: TextStyle(fontSize: 16),
),
color: Colors.blue,
textColor: Colors.white,
onPressed: () {
setState(() {
getApplicationDocumentsDir().then((v) {
packageDir = v;
});
content = packageDir;
print(content);
});
},
);
RaisedButton sdCardButton = RaisedButton(
child: Text(
"获取SD卡路径",
style: TextStyle(fontSize: 16),
),
color: Colors.blue,
textColor: Colors.white,
onPressed: () {
setState(() {
//相当于Android的getExternalStorageDirectory()
getExternalStorageDir().then((v) {
sdCardDir = v;
});
content = sdCardDir;
print(content);
});
},
);
RaisedButton writeToApplicationDirButton = RaisedButton(
child: Text(
'将"踩坑Fultter"写入应用包路径下(也可用SP)',
style: TextStyle(fontSize: 16),
),
color: Colors.blue,
textColor: Colors.white,
onPressed: () {
setState(() {
getApplicationDocumentsDir().then((v) {
packageDir = v;
return packageDir;
}).then((v) {
return new File(packageDir + "/踩坑Flutter.txt");
}).then((v) {
setState(() {
content = "将内容写入 $v 中";
});
return v.writeAsString("踩坑Flutter " * 10);
}).whenComplete(() {
print("写入文件完成");
});
});
},
);
RaisedButton readFromApplicationDirButton = RaisedButton(
child: Text(
'从应用包路径下读取写入的内容',
style: TextStyle(fontSize: 16),
),
color: Colors.blue,
textColor: Colors.white,
onPressed: () {
setState(() {
getApplicationDocumentsDir().then((v) {
packageDir = v;
return packageDir;
}).then((v) {
return new File(packageDir + "/踩坑Flutter.txt");
}).then((v) {
return v.readAsString();
}).then((v) {
content = "读取的内容为: $v";
}).whenComplete(() {
print("读取文件完成");
});
});
},
);
RaisedButton clearFileButton = RaisedButton(
child: Text(
'清除文件',
style: TextStyle(fontSize: 16),
),
color: Colors.blue,
textColor: Colors.white,
onPressed: () {
setState(() {
getApplicationDocumentsDir().then((v) {
packageDir = v;
return packageDir;
}).then((v) {
return new File(packageDir + "/踩坑Flutter.txt");
}).then((v) {
setState(() {
content = "清除 $v 文件";
});
return v.delete();
}).whenComplete(() {
print("清除文件完成");
});
});
},
);
raisedButtonList.add(cacheButton);
raisedButtonList.add(packageButton);
raisedButtonList.add(sdCardButton);
raisedButtonList.add(writeToApplicationDirButton);
raisedButtonList.add(readFromApplicationDirButton);
raisedButtonList.add(clearFileButton);
return raisedButtonList;
}
Future<String> getCacheDir() async {
return (await getTemporaryDirectory()).path;
}
Future<String> getApplicationDocumentsDir() async {
return (await getApplicationDocumentsDirectory()).path;
}
Future<String> getExternalStorageDir() async {
return (await getExternalStorageDirectory()).path;
}
File getFile(String fileName) {
return new File(fileName);
}
}