sharedpreferencies: save settings and sort files in homepage

master
EmaMaker 2021-09-20 16:45:33 +02:00
parent d56dcddadc
commit b96e227b57
7 changed files with 140 additions and 36 deletions

View File

@ -46,7 +46,6 @@ class CounterStorage {
}
Future<String> getNote(String name) async {
//TODO: what if a note with this name already exists?
final dir = (await localDir()).path;
File file = File('$dir/$name');
@ -71,9 +70,9 @@ class CounterStorage {
await f.delete();
}
void deleteNote(String note_name) async {
void deleteNote(String noteName) async {
final dir = await _localPath();
await File('$dir/$note_name').delete();
await File('$dir/$noteName').delete();
}
void renameNote(String oldName, String newName) async {

View File

@ -2,7 +2,9 @@ import 'package:flutter/material.dart';
import 'package:notes_app/screens/home/home.dart';
import 'package:notes_app/components/storage.dart';
void main() => runApp(MyApp());
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {

View File

@ -42,7 +42,7 @@ class MyListTileState extends State<MyListTile> {
@override
Widget build(BuildContext context) {
print("${widget.isChecked} / ${widget.file}");
// print("${widget.isChecked} / ${widget.file}");
return widget.selectMode
? CheckboxListTile(
@ -125,9 +125,10 @@ class MyListTileState extends State<MyListTile> {
TextButton(
onPressed: () {
Navigator.pop(context);
widget.toggleFunction(widget.file, false);
widget.storage.renameNote(
basename(widget.file.path), _controller.text);
widget.deleteFunction(widget.file);
_controller.text = "";
},
child: Text('Yes')),
],
@ -177,7 +178,7 @@ class MyListTileState extends State<MyListTile> {
}
String _lastEdited(DateTime now) {
return "Last edited: ${now.year.toString()}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')} ${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}";
return "Last edited: ${now.year.toString()}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')} at ${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}";
}
Color getColor(Set<MaterialState> states) {

View File

@ -3,6 +3,7 @@ import 'package:flutter/widgets.dart';
import 'package:notes_app/components/storage.dart';
import 'package:notes_app/screens/home/components/MyListTile.dart';
import 'package:notes_app/screens/settings/settings.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:io';
@ -18,13 +19,24 @@ class _MyHomePageState extends State<MyHomePage> {
List<File> _allFilesNames = [];
List<String> _selected = [];
String _sortBy = "";
@override
void initState() {
super.initState();
}
void getSortBy() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_sortBy = prefs.getString("sortBy") ?? "Name";
});
}
@override
Widget build(BuildContext context) {
getSortBy();
return new WillPopScope(
child: Scaffold(
appBar: AppBar(
@ -79,9 +91,21 @@ class _MyHomePageState extends State<MyHomePage> {
bool selectMode = false;
Widget _buildList(BuildContext context) {
print(_selected);
void sortFiles() {
if (_sortBy == "Name") {
_allFilesNames.sort((a, b) {
return a.path.compareTo(b.path);
});
} else if (_sortBy == "Last Modified") {
_allFilesNames.sort((a, b) {
return b.lastModifiedSync().compareTo(a.lastModifiedSync());
});
}
}
Widget _buildList(BuildContext context) {
sortFiles();
final tiles = _allFilesNames.map(
(
File f,
@ -124,12 +148,11 @@ class _MyHomePageState extends State<MyHomePage> {
}
void toggleFile(File f, bool b) {
if (_selected.contains(f.path))
_selected.remove(f.path);
else
_selected.add(f.path);
setState(() {
});
if (_selected.contains(f.path))
_selected.remove(f.path);
else
_selected.add(f.path);
setState(() {});
}
void _deleteFile(File f) {
@ -172,7 +195,8 @@ class _MyHomePageState extends State<MyHomePage> {
List<String> newSelected = [];
_allFilesNames.forEach((element) {
if (!(_selected.contains(element.path))) newSelected.add(element.path);
if (!(_selected.contains(element.path)))
newSelected.add(element.path);
});
print("new $newSelected");
_selected = newSelected;

View File

@ -1,8 +1,7 @@
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:notes_app/components/storage.dart';
import 'package:notes_app/screens/home/home.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Settings extends StatefulWidget {
const Settings({Key? key, required this.storage}) : super(key: key);
@ -13,6 +12,31 @@ class Settings extends StatefulWidget {
}
class _SettingsState extends State<Settings> {
@override
void initState() {
getTheme();
getSortBy();
super.initState();
}
String _theme = "";
String _sortBy = "";
void getTheme() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_theme = prefs.getString("theme") ?? "Follow System";
});
}
void getSortBy() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_sortBy = prefs.getString("sortBy") ?? "Name";
});
}
@override
Widget build(BuildContext context) {
return WillPopScope(
@ -69,10 +93,7 @@ class _SettingsState extends State<Settings> {
builder: (context) => MyHomePage(storage: widget.storage)));
}
String _theme = "";
String _sortBy = "";
void _sortByDialog(BuildContext context){
void _sortByDialog(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext ctx) {
@ -80,7 +101,8 @@ class _SettingsState extends State<Settings> {
title: const Text('Choose theme'),
children: <Widget>[
_radioListTile("Name", "Name", _sortBy, _setSortBy),
_radioListTile("Last Modified", "Last Modified", _sortBy, _setSortBy),
_radioListTile(
"Last Modified", "Last Modified", _sortBy, _setSortBy),
],
);
});
@ -93,7 +115,8 @@ class _SettingsState extends State<Settings> {
return SimpleDialog(
title: const Text('Choose theme'),
children: <Widget>[
_radioListTile("Follow System", "Follow System", _theme, _setTheme),
_radioListTile(
"Follow System", "Follow System", _theme, _setTheme),
_radioListTile("Dark", "Dark", _theme, _setTheme),
_radioListTile("Light", "Light", _theme, _setTheme),
],
@ -102,11 +125,7 @@ class _SettingsState extends State<Settings> {
}
RadioListTile _radioListTile(
String title,
String value,
String groupValue,
Function(dynamic) f
) {
String title, String value, String groupValue, Function(dynamic) f) {
return new RadioListTile(
title: Text(title),
value: value,
@ -116,16 +135,20 @@ class _SettingsState extends State<Settings> {
});
}
void _setTheme(dynamic newTheme) {
void _setTheme(dynamic newTheme) async {
final prefs = await SharedPreferences.getInstance();
prefs.setString("theme", _theme);
setState(() {
_theme = newTheme;
Navigator.of(context).pop();
});
}
void _setSortBy(dynamic newTheme) {
void _setSortBy(dynamic newSortby) async {
final prefs = await SharedPreferences.getInstance();
prefs.setString("sortBy", newSortby);
setState(() {
_sortBy = newTheme;
_sortBy = newSortby;
Navigator.of(context).pop();
});
}

View File

@ -81,6 +81,18 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3"
matcher:
dependency: transitive
description:
@ -108,14 +120,14 @@ packages:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
version: "2.0.4"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
version: "2.1.0"
path_provider_macos:
dependency: transitive
description:
@ -158,6 +170,48 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.3"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.7"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
shared_preferences_macos:
dependency: transitive
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
sky_engine:
dependency: transitive
description: flutter

View File

@ -27,8 +27,9 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
path_provider:
cupertino_icons: ^1.0.3
path_provider: ^2.0.4
shared_preferences: ^2.0.7
dev_dependencies:
flutter_test: