localstorage 3.0.4+7 localstorage: ^3.0.4+7 copied to clipboard
Simple json file-based storage fo flutter. Alternative to react-native AsyncStorage
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:localstorage/localstorage.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Localstorage Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new HomePage(),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class TodoItem {
String title;
bool done;
TodoItem({this.title, this.done});
toJSONEncodable() {
Map<String, dynamic> m = new Map();
m['title'] = title;
m['done'] = done;
return m;
}
}
class TodoList {
List<TodoItem> items;
TodoList() {
items = new List();
}
toJSONEncodable() {
return items.map((item) {
return item.toJSONEncodable();
}).toList();
}
}
class _MyHomePageState extends State<HomePage> {
final TodoList list = new TodoList();
final LocalStorage storage = new LocalStorage('todo_app');
bool initialized = false;
TextEditingController controller = new TextEditingController();
_toggleItem(TodoItem item) {
setState(() {
item.done = !item.done;
_saveToStorage();
});
}
_addItem(String title) {
setState(() {
final item = new TodoItem(title: title, done: false);
list.items.add(item);
_saveToStorage();
});
}
_saveToStorage() {
storage.setItem('todos', list.toJSONEncodable());
}
_clearStorage() async {
await storage.clear();
setState(() {
list.items = storage.getItem('todos') ?? [];
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Localstorage demo'),
),
body: Container(
padding: EdgeInsets.all(10.0),
constraints: BoxConstraints.expand(),
child: FutureBuilder(
future: storage.ready,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Center(
child: CircularProgressIndicator(),
);
}
if (!initialized) {
var items = storage.getItem('todos');
if (items != null) {
list.items = List<TodoItem>.from(
(items as List).map(
(item) => TodoItem(
title: item['title'],
done: item['done'],
),
),
);
}
initialized = true;
}
List<Widget> widgets = list.items.map((item) {
return CheckboxListTile(
value: item.done,
title: Text(item.title),
selected: item.done,
onChanged: (bool selected) {
_toggleItem(item);
},
);
}).toList();
return Column(
children: <Widget>[
Expanded(
flex: 1,
child: ListView(
children: widgets,
itemExtent: 50.0,
),
),
ListTile(
title: TextField(
controller: controller,
decoration: InputDecoration(
labelText: 'What to do?',
),
onEditingComplete: _save,
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(Icons.save),
onPressed: _save,
tooltip: 'Save',
),
IconButton(
icon: Icon(Icons.delete),
onPressed: _clearStorage,
tooltip: 'Clear storage',
)
],
),
),
],
);
},
)),
);
}
void _save() {
_addItem(controller.value.text);
controller.clear();
}
}