imperative_flutter 1.0.0 imperative_flutter: ^1.0.0 copied to clipboard
Manage the state of your widgets using imperative programming concepts.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:imperative_flutter/imperative_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return ImperativeProvider(
child: MaterialApp(
title: 'Flutter 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> {
static const id = 'count';
late final provider = ImperativeProvider.of(context);
void _incrementCounter() {
provider.updateState<int>(id, (current) => current * 2);
}
@override
void initState() {
WidgetsBinding.instance!.addPostFrameCallback((_) {
provider.listenerState<int>(id, (current) {
if (kDebugMode) {
print(current);
}
});
});
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:',
),
ImperativeBuilder<int>(
id: id,
initial: 2,
builder: (context, state) {
return Text(
'$state',
style: Theme.of(context).textTheme.headline4,
);
},
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}