obs_state_mixin 0.0.1-dev.6 obs_state_mixin: ^0.0.1-dev.6 copied to clipboard
Lightweight Flutter package for reactive state management using Obs and ObsStateMixin.
import 'package:flutter/material.dart';
import 'package:obs_state_mixin/obs_state_mixin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Obs State Demo',
home: Home(),
);
}
}
class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> with ObsStateMixin {
// Creates a new observable called _counter.
// This will automatically be disposed when the widget is disposed.
late final _counter = obs(0);
// Increments the value of _counter. No need to call setState().
void _incrementCounter() => _counter.value++;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}