Skip to content

Commit 90ae52d

Browse files
author
lakscastro
committed
(#85, #79, #61) Add more actions related to the writeToFile API
1 parent 83fa900 commit 90ae52d

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

example/lib/screens/file_explorer/file_explorer_card.dart

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import 'dart:async';
2+
import 'dart:io';
3+
import 'dart:math';
24
import 'dart:typed_data';
35

46
import 'package:flutter/material.dart';
@@ -85,6 +87,12 @@ class _FileExplorerCardState extends State<FileExplorerCard> {
8587

8688
bool get _isDirectory => file.metadata?.isDirectory ?? false;
8789

90+
int _generateLuckNumber() {
91+
final random = Random();
92+
93+
return random.nextInt(1000);
94+
}
95+
8896
@override
8997
Widget build(BuildContext context) {
9098
return SimpleCard(
@@ -105,9 +113,19 @@ class _FileExplorerCardState extends State<FileExplorerCard> {
105113
return Image.memory(content!);
106114
}
107115

116+
final contentAsString = String.fromCharCodes(content!);
117+
118+
final fileIsEmpty = contentAsString.isEmpty;
119+
108120
return Container(
109121
padding: k8dp.all,
110-
child: Text(String.fromCharCodes(content!)),
122+
child: Text(
123+
fileIsEmpty ? 'This file is empty' : contentAsString,
124+
style: TextStyle(
125+
color: fileIsEmpty ? Colors.black26 : null,
126+
fontStyle: fileIsEmpty ? FontStyle.italic : null,
127+
),
128+
),
111129
);
112130
},
113131
);
@@ -173,7 +191,6 @@ class _FileExplorerCardState extends State<FileExplorerCard> {
173191
final uri = widget.partialFile.metadata!.uri!;
174192

175193
try {
176-
// OpenFile.open('/sdcard/example.txt');
177194
final launched = await openDocumentFile(uri);
178195

179196
if (launched ?? false) {
@@ -198,13 +215,46 @@ class _FileExplorerCardState extends State<FileExplorerCard> {
198215
}
199216
},
200217
),
201-
if (!_isDirectory)
218+
if (!_isDirectory) ...[
202219
DangerButton(
203220
'Write to File',
204221
onTap: () async {
205-
await writeToFile(widget.partialFile.metadata!.uri!, content: 'Hello World!');
222+
await writeToFile(
223+
widget.partialFile.metadata!.uri!,
224+
content:
225+
'Hello World! Your luck number is: ${_generateLuckNumber()}',
226+
mode: FileMode.write,
227+
);
228+
},
229+
),
230+
DangerButton(
231+
'Append to file',
232+
onTap: () async {
233+
final contents = await getDocumentContentAsString(
234+
widget.partialFile.metadata!.uri!,
235+
);
236+
237+
final prependWithNewLine = contents?.isNotEmpty ?? true;
238+
239+
await writeToFile(
240+
widget.partialFile.metadata!.uri!,
241+
content:
242+
"${prependWithNewLine ? '\n' : ''}You file got bigger! Here's your luck number: ${_generateLuckNumber()}",
243+
mode: FileMode.append,
244+
);
245+
},
246+
),
247+
DangerButton(
248+
'Erase file content',
249+
onTap: () async {
250+
await writeToFile(
251+
widget.partialFile.metadata!.uri!,
252+
content: '',
253+
mode: FileMode.write,
254+
);
206255
},
207256
),
257+
],
208258
],
209259
),
210260
],

0 commit comments

Comments
 (0)