osam 6.0.3
osam: ^6.0.3 copied to clipboard
Lightweight and predictable state management
example/lib/main.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:osam/osam.dart';
void main() async {
final core = Core();
runApp(MaterialApp(
home: App(core),
));
}
class App extends StatefulWidget {
final Core core;
const App(this.core);
@override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
bool isVisible = true;
var num = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: ValueListenableBuilder<List<News>>(
valueListenable: widget.core.news,
builder: (ctx, value, _) {
return GestureDetector(
child: Text(value.length.toString()),
onTap: () {
widget.core.news.update((value) => value.add(News(num)));
num++;
},
);
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
widget.core.news.update((value) {
value.clear();
value.add(News(0));
});
},
),
body: ValueListenableBuilder<List<News>>(
valueListenable: widget.core.news,
builder: (ctx, value, _) {
return ListView.builder(
itemBuilder: (ctx, index) {
return ValueListenableBuilder<bool>(
valueListenable: value[index].isFavorite,
key: ValueKey(value[index].id),
builder: (ctx, isFavorite, _) => GestureDetector(
child: Container(
child: Row(
children: <Widget>[
Text(value[index].id.toString()),
Icon(isFavorite ? Icons.plus_one : Icons.minimize),
],
),
height: 200,
width: 200,
color: Colors.greenAccent,
margin: const EdgeInsets.all(8),
),
onTap: () {
value[index].isFavorite.value = !isFavorite;
},
),
);
},
itemCount: value.length,
);
},
),
);
}
}
class Core {
var news = PropertyNotifier(<News>[]);
}
class News {
var isFavorite = PropertyNotifier(false);
final int id;
News(this.id);
}