Skip to content

Commit

Permalink
editor: Add saveFile function
Browse files Browse the repository at this point in the history
  • Loading branch information
tseli0s committed Jan 9, 2024
1 parent 922e6a7 commit 58f91cd
Showing 1 changed file with 51 additions and 47 deletions.
98 changes: 51 additions & 47 deletions lib/text_edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,55 +100,59 @@ class TextEditorState extends State<TextEditor> {
body: b,
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.save_alt),
onPressed: () async {
final contents = widget.textController.text;
String? filename = widget.filename;
if (filename != null) {
final f = File(filename);
f.writeAsString(contents);
final snackBar = SnackBar(
content: Text(AppLocalizations.of(context)!.saveSuccess),
padding: const EdgeInsets.all(16.0),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
action: SnackBarAction(
label: AppLocalizations.of(context)!.close,
onPressed: () {},
),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
} else {
String? result = await FilePicker.platform.saveFile(
dialogTitle: AppLocalizations.of(context)!.saveFileAt,
lockParentWindow: true,
);

if (result != null) {
final f = File(result);
f.writeAsString(contents);
final snackBar = SnackBar(
content: Text(AppLocalizations.of(context)!.saveSuccess),
padding: const EdgeInsets.all(16.0),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
action: SnackBarAction(
label: AppLocalizations.of(context)!.close,
onPressed: () {},
),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
} else {
final title = AppLocalizations.of(context)!.operationCancelled;
final message = AppLocalizations.of(context)!.noFileWasSelected;
showErrorDialog(context, title, message);
}
}
onPressed: () {
saveFile();
},
),
);
}

void saveFile() async {
final contents = widget.textController.text;
String? filename = widget.filename;
if (filename != null) {
final f = File(filename);
f.writeAsString(contents);
final snackBar = SnackBar(
content: Text(AppLocalizations.of(context)!.saveSuccess),
padding: const EdgeInsets.all(16.0),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
action: SnackBarAction(
label: AppLocalizations.of(context)!.close,
onPressed: () {},
),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
} else {
String? result = await FilePicker.platform.saveFile(
dialogTitle: AppLocalizations.of(context)!.saveFileAt,
lockParentWindow: true,
);

if (result != null) {
final f = File(result);
f.writeAsString(contents);
final snackBar = SnackBar(
content: Text(AppLocalizations.of(context)!.saveSuccess),
padding: const EdgeInsets.all(16.0),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
action: SnackBarAction(
label: AppLocalizations.of(context)!.close,
onPressed: () {},
),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
} else {
final title = AppLocalizations.of(context)!.operationCancelled;
final message = AppLocalizations.of(context)!.noFileWasSelected;
showErrorDialog(context, title, message);
}
}
}
}

0 comments on commit 58f91cd

Please sign in to comment.