Skip to content

Commit 3a1995b

Browse files
committed
Restructure command_it getting_started documentation
- Move installation section to beginning - Update all examples to use watch_it (recommended approach) - Add info box explaining ValueListenableBuilder alternative - Create dedicated "Without watch_it" documentation page - Fix command properties descriptions (value, isRunning, errors types) - Update error handling example to use listen_it operators - Fix SVG flow diagram (Execute -> Run, isExecuting -> isRunning) - Create Weather App Tutorial for real-world example - Update Next Steps to link to tutorial instead of integration docs - Inline GetIt.instance calls when used once - Use registerSingleton instead of registerLazySingleton - Add explanatory comments for different command calling patterns - Format all command_it code samples - Add AI warning banner to tutorial
1 parent b065b01 commit 3a1995b

File tree

9 files changed

+812
-341
lines changed

9 files changed

+812
-341
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import 'package:command_it/command_it.dart';
2+
import 'package:flutter/material.dart';
3+
4+
// #region example
5+
class CounterModel {
6+
int _count = 0;
7+
8+
// Command wraps a function and acts as a ValueListenable
9+
late final incrementCommand = Command.createSyncNoParam<String>(
10+
() {
11+
_count++;
12+
return _count.toString();
13+
},
14+
initialValue: '0',
15+
);
16+
}
17+
18+
class CounterWidget extends StatelessWidget {
19+
CounterWidget({super.key});
20+
21+
final model = CounterModel();
22+
23+
@override
24+
Widget build(BuildContext context) {
25+
return Column(
26+
mainAxisAlignment: MainAxisAlignment.center,
27+
children: [
28+
Text('You have pushed the button this many times:'),
29+
// Command is a ValueListenable - use ValueListenableBuilder
30+
ValueListenableBuilder<String>(
31+
valueListenable: model.incrementCommand,
32+
builder: (context, value, _) => Text(
33+
value,
34+
style: Theme.of(context).textTheme.headlineMedium,
35+
),
36+
),
37+
SizedBox(height: 16),
38+
// Command has a .run method - use it as tearoff for onPressed
39+
ElevatedButton(
40+
onPressed: model.incrementCommand.run,
41+
child: Text('Increment'),
42+
),
43+
],
44+
);
45+
}
46+
}
47+
// #endregion example
48+
49+
void main() {
50+
runApp(MaterialApp(
51+
home: Scaffold(
52+
body: Center(child: CounterWidget()),
53+
),
54+
));
55+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'package:command_it/command_it.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:get_it/get_it.dart';
4+
import 'package:watch_it/watch_it.dart';
5+
6+
// #region example
7+
class CounterService {
8+
int _count = 0;
9+
10+
// Command wraps a function and acts as a ValueListenable
11+
late final incrementCommand = Command.createSyncNoParam<String>(
12+
() {
13+
_count++;
14+
return _count.toString();
15+
},
16+
initialValue: '0',
17+
);
18+
}
19+
20+
// Register with get_it (call this in main())
21+
void setup() {
22+
GetIt.instance.registerSingleton(CounterService());
23+
}
24+
25+
// Use watch_it to observe the command
26+
class CounterWidget extends WatchingWidget {
27+
const CounterWidget({super.key});
28+
29+
@override
30+
Widget build(BuildContext context) {
31+
// Watch the command value - rebuilds when it changes
32+
final count = watchValue((CounterService s) => s.incrementCommand);
33+
34+
return Column(
35+
mainAxisAlignment: MainAxisAlignment.center,
36+
children: [
37+
Text('You have pushed the button this many times:'),
38+
Text(
39+
count,
40+
style: Theme.of(context).textTheme.headlineMedium,
41+
),
42+
SizedBox(height: 16),
43+
// No parameters - use .run as tearoff
44+
ElevatedButton(
45+
onPressed: GetIt.instance<CounterService>().incrementCommand.run,
46+
child: Text('Increment'),
47+
),
48+
],
49+
);
50+
}
51+
}
52+
// #endregion example
53+
54+
void main() {
55+
setup();
56+
runApp(MaterialApp(
57+
home: Scaffold(
58+
body: Center(child: CounterWidget()),
59+
),
60+
));
61+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import 'package:command_it/command_it.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:get_it/get_it.dart';
4+
import 'package:watch_it/watch_it.dart';
5+
import '_shared/stubs.dart';
6+
7+
// #region example
8+
class WeatherService {
9+
late final loadWeatherCommand = Command.createAsync<String, String>(
10+
(city) async {
11+
await simulateDelay(1000);
12+
return 'Weather in $city: Sunny, 72°F';
13+
},
14+
initialValue: 'No data loaded',
15+
);
16+
}
17+
18+
// Register service with get_it (call this in main())
19+
void setup() {
20+
GetIt.instance.registerSingleton(WeatherService());
21+
}
22+
23+
// Use watch_it to observe commands without ValueListenableBuilder
24+
class WeatherWidget extends WatchingWidget {
25+
const WeatherWidget({super.key});
26+
27+
@override
28+
Widget build(BuildContext context) {
29+
// Watch the command value directly
30+
final weather = watchValue((WeatherService s) => s.loadWeatherCommand);
31+
32+
// Watch the loading state
33+
final isLoading =
34+
watchValue((WeatherService s) => s.loadWeatherCommand.isRunning);
35+
36+
return Column(
37+
mainAxisAlignment: MainAxisAlignment.center,
38+
children: [
39+
if (isLoading)
40+
CircularProgressIndicator()
41+
else
42+
Text(weather, style: TextStyle(fontSize: 18)),
43+
SizedBox(height: 16),
44+
// With parameters - call command directly (it's callable)
45+
ElevatedButton(
46+
onPressed: () =>
47+
GetIt.instance<WeatherService>().loadWeatherCommand('London'),
48+
child: Text('Load Weather'),
49+
),
50+
],
51+
);
52+
}
53+
}
54+
// #endregion example
55+
56+
void main() {
57+
setup();
58+
runApp(MaterialApp(home: Scaffold(body: Center(child: WeatherWidget()))));
59+
}

docs/.vitepress/config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export default defineConfig({
9797
{ text: 'Restrictions', link: '/documentation/command_it/restrictions.md' },
9898
{ text: 'Testing', link: '/documentation/command_it/testing.md' },
9999
{ text: 'watch_it Integration', link: '/documentation/command_it/watch_it_integration.md' },
100+
{ text: 'Without watch_it', link: '/documentation/command_it/without_watch_it.md' },
100101
{ text: 'Best Practices', link: '/documentation/command_it/best_practices.md' }
101102
]
102103
},

0 commit comments

Comments
 (0)