Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rodydavis/provider_persist
Persist your UI State with Provider.
https://github.com/rodydavis/provider_persist
dart flutter provider task-manager todo
Last synced: about 1 month ago
JSON representation
Persist your UI State with Provider.
- Host: GitHub
- URL: https://github.com/rodydavis/provider_persist
- Owner: rodydavis
- License: mit
- Created: 2019-06-12T18:14:39.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-06-12T18:51:59.000Z (over 5 years ago)
- Last Synced: 2024-12-24T00:10:48.435Z (about 1 month ago)
- Topics: dart, flutter, provider, task-manager, todo
- Language: Objective-C
- Size: 38.8 MB
- Stars: 20
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# provider_persist
A Flutter Template to show how to persist provider.
## Getting Started
Example of how to persist UI state with [Provider](https://pub.dev/packages/provider).
![task-screen](https://github.com/AppleEducate/provider_persist/blob/master/screenshots/1.png)
## UI
``` dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';import '../../data/classes/index.dart';
import '../../data/models/index.dart';
import '../index.dart';class TasksScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Task Manager'),
),
body: Consumer(
builder: (context, model, child) {
if (model?.tasks == null) {
return Center(
child: CircularProgressIndicator(),
);
}
if (model.tasks.isEmpty) {
return Center(
child: Text('No Tasks Found'),
);
}
return ListView.separated(
separatorBuilder: (context, index) => Divider(),
itemCount: model.tasks.length,
itemBuilder: (context, index) {
final _item = model.tasks[index];
return ListTile(
title: Text(_item.name),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () => model.deleteTask(index),
),
onTap: () => _editTask(context, _item, index),
);
},
);
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => _addTask(context),
),
);
}void _addTask(BuildContext context) async {
final _task = await Navigator.push(
context,
MaterialPageRoute(
builder: (_) => EditTaskScreen(),
fullscreenDialog: true,
));if (_task != null) {
final _model = Provider.of(context);
_model.addTask(_task);
}
}void _editTask(BuildContext context, Task task, int index) async {
final _task = await Navigator.push(
context,
MaterialPageRoute(
builder: (_) => EditTaskScreen(task: task),
fullscreenDialog: false,
));if (_task != null) {
final _model = Provider.of(context);
_model.editTask(index, _task);
}
}
}```
## Model
``` dart
import 'package:flutter/cupertino.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:localstorage/localstorage.dart';import '../classes/index.dart';
part 'task_state.g.dart';
class TaskProvider extends ChangeNotifier {
TaskState _state;
final String _stateKey = 'state';
final _storage = LocalStorage('Task-state');void init() async {
await _storage.ready;final _data = _storage.getItem(_stateKey);
try {
if (_data != null) {
_state = TaskState.fromJson(_data);
}
} catch (e) {
print('Error Loading Task State: $e');
}if (_data == null) {
_state = TaskState(tasks: []);
}
notifyListeners();
}List get tasks => _state?.tasks;
void addTask(Task value) {
_state.tasks.add(value);
notifyListeners();
_saveState();
}void editTask(int index, Task value) {
_state.tasks[index] = value;
notifyListeners();
_saveState();
}void deleteTask(int index) {
_state.tasks.removeAt(index);
notifyListeners();
_saveState();
}void _saveState() async => await _storage.setItem(_stateKey, _state.toJson());
}@JsonSerializable(nullable: false, explicitToJson: true)
class TaskState {
TaskState({this.tasks});factory TaskState.fromJson(Map json) =>
_$TaskStateFromJson(json);List tasks;
Map toJson() => _$TaskStateToJson(this);
}```
## Class
To update run: `flutter packages pub run build_runner watch —delete-conflicting-outputs`
``` dart
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';part 'task.g.dart';
@JsonSerializable(nullable: false)
class Task {
final String name;
final bool active;
final DateTime dataCreated;
Task({
@required this.name,
@required this.dataCreated,
this.active: true,
});factory Task.fromJson(Map json) => _$TaskFromJson(json);
Map toJson() => _$TaskToJson(this);
}```