hydro 0.0.1-beta1 hydro: ^0.0.1-beta1 copied to clipboard
A simple, fast state management & service container solution for Flutter, without acrobatics.
import 'package:example/some_class.dart';
import 'package:example/some_page.dart';
import 'package:flutter/material.dart';
import 'package:hydro/hydro.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Hydro Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late SomeClass someClass;
@override
void initState() {
Hydro.set(SomeClass());
someClass = Hydro.mustGet<SomeClass>(this);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
"${someClass.counter}",
style: Theme.of(context).textTheme.headline4,
),
Text(
"Name: ${someClass.name}",
style: Theme.of(context).textTheme.headline5,
),
ElevatedButton(
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (ctx) => const SomePage()));
},
child: const Text("Some Page"))
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
someClass.incrementCounter();
},
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}