result_command 1.2.0
result_command: ^1.2.0 copied to clipboard
A command pattern implementation for Dart and Flutter using result_dart package.
import 'package:flutter/material.dart';
import 'package:result_command/result_command.dart';
import 'package:result_dart/result_dart.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
int _counter = 0;
final incrementCommand = Command0(() async {
await Future.delayed(const Duration(seconds: 1));
_counter++;
return Success(_counter);
});
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
ValueListenableBuilder(
valueListenable: incrementCommand,
builder: (context, state, snapshot) {
return Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
);
},
),
],
),
),
floatingActionButton: ValueListenableBuilder<CommandState<int>>(
valueListenable: incrementCommand,
builder: (context, state, snapshot) {
return FloatingActionButton(
onPressed: incrementCommand.isRunning ? null : () => incrementCommand.execute(),
tooltip: 'Increment',
child: const Icon(Icons.add),
);
},
),
);
}
}