Skip to content

Commit 0a01285

Browse files
committed
Revert to the counter app example
But using ComputedWidget instead of StatefulWidget
1 parent 290868d commit 0a01285

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

example/main.dart

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
1-
import 'package:built_collection/built_collection.dart';
21
import 'package:computed_flutter/computed_flutter.dart';
32

43
import 'package:flutter/material.dart';
54

6-
final source = ValueNotifier(<int>[].toBuiltList());
5+
final count = ValueNotifier(0);
76

87
void main() {
9-
() async {
10-
source.value = [1, 2, -3, 4].toBuiltList();
11-
await Future.delayed(const Duration(seconds: 3));
12-
source.value = [1, 2, -3, -4].toBuiltList();
13-
await Future.delayed(const Duration(seconds: 3));
14-
source.value = [4, 5, 6].toBuiltList();
15-
await Future.delayed(const Duration(seconds: 3));
16-
source.value = [4, 5, 6].toBuiltList();
17-
}();
18-
198
runApp(const MyApp());
209
}
2110

@@ -30,40 +19,39 @@ class MyApp extends StatelessWidget {
3019
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
3120
useMaterial3: true,
3221
),
33-
home: MyHomePage(title: 'Computed Flutter Demo'),
22+
home: const MyHomePage(),
3423
);
3524
}
3625
}
3726

27+
// Note that unlike the vanilla counter app, this widget is stateless
3828
class MyHomePage extends ComputedWidget {
39-
MyHomePage({super.key, required this.title});
40-
final String title;
41-
42-
final Computed<BuiltList<int>> list = $(() {
43-
final anyNegative = source.use.any((element) => element < 0);
44-
final maybeReversed =
45-
anyNegative ? source.use.reversed.toBuiltList() : source.use;
46-
return maybeReversed.rebuild((p0) => p0.add(0));
47-
});
29+
const MyHomePage({super.key});
4830

4931
@override
5032
Widget build(BuildContext context) {
5133
return Scaffold(
5234
appBar: AppBar(
5335
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
54-
title: Text(title),
36+
title: const Text('Computed Flutter Demo'),
5537
),
5638
body: Center(
5739
child: Column(
5840
mainAxisAlignment: MainAxisAlignment.center,
5941
children: <Widget>[
60-
Text(
61-
list.use.toString(),
62-
style: Theme.of(context).textTheme.headlineMedium,
63-
)
42+
const Text(
43+
'You have pushed the button this many times:',
44+
),
45+
Text('${count.use}',
46+
style: Theme.of(context).textTheme.headlineMedium)
6447
],
6548
),
6649
),
50+
floatingActionButton: FloatingActionButton(
51+
onPressed: () => count.value++,
52+
tooltip: 'Increment',
53+
child: const Icon(Icons.add),
54+
),
6755
);
6856
}
6957
}

pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ dev_dependencies:
1919
sdk: flutter
2020
flutter_lints: ^3.0.0
2121
test: ^1.24.3
22+
23+
flutter:
24+
uses-material-design: true

0 commit comments

Comments
 (0)