1- import 'package:built_collection/built_collection.dart' ;
21import 'package:computed_flutter/computed_flutter.dart' ;
32
43import 'package:flutter/material.dart' ;
54
6- final source = ValueNotifier (< int > []. toBuiltList () );
5+ final count = ValueNotifier (0 );
76
87void 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
3828class 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}
0 commit comments