2019-06-27 17:29:40 -07:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'global_helper_functions.dart';
|
|
|
|
import 'database_helper.dart';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
class EditNotes extends StatefulWidget {
|
|
|
|
final String mapLocation;
|
|
|
|
|
|
|
|
EditNotes({
|
|
|
|
Key key,
|
|
|
|
this.mapLocation,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_EditNotesState createState() => _EditNotesState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _EditNotesState extends State<EditNotes> {
|
|
|
|
final double textHeight = 1.5;
|
|
|
|
final _textEditingController = TextEditingController();
|
|
|
|
final dbHelper = DatabaseHelper.instance;
|
|
|
|
String _inputText = '';
|
|
|
|
String _oldNotes = '';
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
loadNotes();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
Future<bool> _requestPop() {
|
|
|
|
if (_inputText != _oldNotes) {
|
|
|
|
showDialog(
|
2019-07-27 15:34:01 -07:00
|
|
|
context: context,
|
|
|
|
barrierDismissible: false,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
backgroundColor: ivory,
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(6.0)),
|
|
|
|
),
|
|
|
|
content: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
Text(
|
|
|
|
'Discard your changes?',
|
|
|
|
textAlign: TextAlign.center,
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
Container(
|
|
|
|
margin: EdgeInsets.only(
|
|
|
|
top: 40,
|
|
|
|
bottom: 10,
|
|
|
|
),
|
|
|
|
child: Wrap(
|
|
|
|
runSpacing: 30,
|
|
|
|
children: <Widget>[
|
|
|
|
Container(
|
|
|
|
margin: EdgeInsets.symmetric(
|
|
|
|
horizontal: 15,
|
|
|
|
),
|
|
|
|
child: ButtonTheme(
|
|
|
|
height: 75,
|
|
|
|
child: RaisedButton(
|
|
|
|
color: peacockBlue,
|
|
|
|
child: Text(
|
|
|
|
"No",
|
|
|
|
style: TextStyle(
|
|
|
|
height: textHeight,
|
|
|
|
color: Colors.white,
|
|
|
|
fontSize: 24,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}),
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
Container(
|
|
|
|
margin: EdgeInsets.symmetric(
|
|
|
|
horizontal: 15,
|
|
|
|
),
|
|
|
|
child: ButtonTheme(
|
|
|
|
height: 75,
|
|
|
|
child: RaisedButton(
|
|
|
|
color: peacockBlue,
|
|
|
|
child: Text(
|
|
|
|
"Discard",
|
|
|
|
style: TextStyle(
|
|
|
|
height: textHeight,
|
|
|
|
color: Colors.white,
|
|
|
|
fontSize: 24,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}),
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
],
|
|
|
|
),
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
2019-06-27 17:29:40 -07:00
|
|
|
return Future.value(false);
|
|
|
|
} else {
|
|
|
|
return Future.value(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return WillPopScope(
|
|
|
|
onWillPop: _requestPop,
|
|
|
|
child: Scaffold(
|
|
|
|
backgroundColor: ivory,
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text('Notes, MarkDown Supported'),
|
|
|
|
actions: <Widget>[
|
|
|
|
IconButton(
|
2019-07-27 15:34:01 -07:00
|
|
|
icon: Icon(Icons.save),
|
|
|
|
onPressed: () async {
|
|
|
|
Navigator.of(context).pop(this._inputText);
|
|
|
|
Map<String, dynamic> row = {
|
|
|
|
DatabaseHelper.columnMapLocation: widget.mapLocation,
|
|
|
|
DatabaseHelper.columnNotes: this._inputText,
|
|
|
|
};
|
|
|
|
await dbHelper.update(row);
|
|
|
|
}),
|
2019-06-27 17:29:40 -07:00
|
|
|
],
|
|
|
|
),
|
|
|
|
body: Column(
|
2019-07-27 15:34:01 -07:00
|
|
|
children: <Widget>[
|
2019-06-27 17:29:40 -07:00
|
|
|
Expanded(
|
|
|
|
child: TextField(
|
|
|
|
controller: _textEditingController,
|
|
|
|
keyboardType: TextInputType.multiline,
|
|
|
|
maxLines: null,
|
|
|
|
onChanged: (text) {
|
|
|
|
_inputText = text;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Row(
|
2019-07-27 15:34:01 -07:00
|
|
|
children: <Widget>[
|
2019-06-27 17:29:40 -07:00
|
|
|
Expanded(
|
|
|
|
child: Container(),
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
margin: EdgeInsets.all(20.0),
|
|
|
|
child: ButtonTheme(
|
|
|
|
height: 55,
|
|
|
|
padding: EdgeInsets.only(
|
2019-07-27 15:34:01 -07:00
|
|
|
top: 5, right: 12, bottom: 12, left: 10),
|
2019-06-27 17:29:40 -07:00
|
|
|
child: RaisedButton(
|
2019-07-27 15:34:01 -07:00
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(6.0)),
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
color: peacockBlue,
|
|
|
|
child: Text(
|
|
|
|
'Import',
|
|
|
|
style: TextStyle(
|
|
|
|
height: textHeight,
|
|
|
|
color: Colors.white,
|
|
|
|
fontSize: 24,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () async {
|
|
|
|
Map<PermissionGroup, PermissionStatus>
|
|
|
|
_storagePermissions;
|
|
|
|
PermissionStatus _storagePermission =
|
|
|
|
await PermissionHandler().checkPermissionStatus(
|
|
|
|
PermissionGroup.storage);
|
|
|
|
if (_storagePermission == PermissionStatus.denied) {
|
|
|
|
_storagePermissions = await PermissionHandler()
|
|
|
|
.requestPermissions([PermissionGroup.storage]);
|
|
|
|
}
|
|
|
|
if ((_storagePermission ==
|
|
|
|
PermissionStatus.granted) ||
|
|
|
|
(_storagePermissions.toString() ==
|
|
|
|
PermissionStatus.granted.toString())) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
backgroundColor: ivory,
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.all(
|
|
|
|
Radius.circular(6.0)),
|
|
|
|
),
|
|
|
|
title: Text(
|
|
|
|
'You can import a markdown (or plain text) file.',
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
color: candyApple,
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
),
|
|
|
|
content: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
Container(
|
|
|
|
margin: EdgeInsets.only(
|
|
|
|
top: 40,
|
|
|
|
bottom: 10,
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
child: ButtonTheme(
|
|
|
|
height: 55,
|
|
|
|
padding: EdgeInsets.only(
|
|
|
|
top: 5,
|
|
|
|
right: 12,
|
|
|
|
bottom: 12,
|
|
|
|
left: 10),
|
|
|
|
child: RaisedButton(
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius:
|
|
|
|
BorderRadius.all(
|
|
|
|
Radius.circular(6.0)),
|
|
|
|
),
|
|
|
|
color: peacockBlue,
|
|
|
|
child: Text(
|
|
|
|
'Select File',
|
|
|
|
style: TextStyle(
|
|
|
|
height: textHeight,
|
|
|
|
color: Colors.white,
|
|
|
|
fontSize: 24,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () async {
|
|
|
|
String _filePath;
|
|
|
|
_filePath = await FilePicker
|
|
|
|
.getFilePath(
|
|
|
|
type: FileType.ANY);
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
try {
|
|
|
|
String _contents =
|
|
|
|
await File(_filePath)
|
|
|
|
.readAsString();
|
|
|
|
setState(() {
|
|
|
|
this
|
|
|
|
._textEditingController
|
|
|
|
.text = _contents;
|
|
|
|
this._inputText =
|
|
|
|
_contents;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext
|
|
|
|
context) {
|
|
|
|
return AlertDialog(
|
|
|
|
backgroundColor:
|
|
|
|
ivory,
|
|
|
|
shape:
|
|
|
|
RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius
|
|
|
|
.all(Radius
|
|
|
|
.circular(
|
|
|
|
6.0)),
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
title: Text(
|
|
|
|
'Oops, something went wrong',
|
|
|
|
textAlign:
|
|
|
|
TextAlign
|
|
|
|
.center,
|
|
|
|
style: TextStyle(
|
|
|
|
color:
|
|
|
|
candyApple,
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
),
|
|
|
|
content: Column(
|
|
|
|
mainAxisSize:
|
|
|
|
MainAxisSize
|
|
|
|
.min,
|
|
|
|
children: <
|
|
|
|
Widget>[
|
|
|
|
Container(
|
|
|
|
margin:
|
|
|
|
EdgeInsets
|
|
|
|
.only(
|
|
|
|
top: 40,
|
|
|
|
bottom: 10,
|
|
|
|
),
|
|
|
|
child:
|
|
|
|
ButtonTheme(
|
|
|
|
height: 55,
|
|
|
|
padding: EdgeInsets.only(
|
|
|
|
top: 5,
|
|
|
|
right:
|
|
|
|
12,
|
|
|
|
bottom:
|
|
|
|
12,
|
|
|
|
left:
|
|
|
|
10),
|
|
|
|
child: RaisedButton(
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius:
|
|
|
|
BorderRadius.all(Radius.circular(6.0)),
|
|
|
|
),
|
|
|
|
color: peacockBlue,
|
|
|
|
child: Text(
|
|
|
|
'OK',
|
|
|
|
style:
|
|
|
|
TextStyle(
|
|
|
|
height:
|
|
|
|
textHeight,
|
|
|
|
color:
|
|
|
|
Colors.white,
|
|
|
|
fontSize:
|
|
|
|
24,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () async {
|
|
|
|
Navigator.of(context)
|
|
|
|
.pop();
|
|
|
|
}),
|
|
|
|
),
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
],
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
),
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
],
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
2019-07-27 15:34:01 -07:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
2019-06-27 17:29:40 -07:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-27 15:34:01 -07:00
|
|
|
Future<void> loadNotes() async {
|
2019-06-27 17:29:40 -07:00
|
|
|
String _notes = await dbHelper.queryNotes(widget.mapLocation);
|
|
|
|
setState(() {
|
|
|
|
_oldNotes = _notes;
|
|
|
|
_textEditingController.text = _notes;
|
|
|
|
_inputText = _notes;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|