Last updated:
0 purchases
imperative flutter
Imperative Flutter #
Manage the state of your widgets using imperative programming concepts.
Setup #
Install imperative_flutter package in pubspec.yaml
dependencies:
imperative_flutter: ^1.0.0
copied to clipboard
Then it's just import in your code
import 'package:imperative_flutter/imperative_flutter.dart';
copied to clipboard
Usage #
ImperativeProvider is responsible for storing state and handling the references for ImperativeBuilder, it can be global scope when MaterialApp is his child or local scope when Scaffold is your child.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ImperativeProvider(
child: MaterialApp(
title: 'Imperative Flutter Demo',
home: MyHomePage(),
),
);
}
}
copied to clipboard
ImperativeBuilder is responsible for reconstructing it when that state is changed, note that the id must be unique for each ImperativeProvider Scope.
// Inside StatelessWidget
ImperativeBuilder<int>(
id: 'count',
initialData: 0,
/* the builder method will be called every time the state changes,
updating only the widget within the scope */
builder: (context, state) {
return Container(
height: 48,
width: 48,
color: Colors.pink,
child: Center(
child: Text(
'$state',
style: TextStyle(color: Colors.white),
),
),
);
},
),
// ...
class Whatever extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Get instance of ImperativeProvider
final provider = ImperativeProvider.of(context);
return ElevatedButton.icon(
onPressed: () {
// Set state of ImperativeBuilder where id equal 'count' and rebuild
provider.updateState<int>('count', (current) => current + 1);
},
icon: Icon(Icons.add),
label: Text('plus'),
),
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.