Skip to content

Commit f47fa46

Browse files
committed
Add static resource initialisation
1 parent f070a97 commit f47fa46

File tree

3 files changed

+102
-37
lines changed

3 files changed

+102
-37
lines changed

intro_flutter_gpu/codelab_rebuild.yaml

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,7 @@ steps:
19561956
patch-u: |
19571957
--- b/intro_flutter_gpu/step_12/lib/main.dart
19581958
+++ a/intro_flutter_gpu/step_12/lib/main.dart
1959-
@@ -3,14 +3,11 @@
1959+
@@ -3,20 +3,21 @@
19601960
// found in the LICENSE file.
19611961
19621962
import 'dart:math' as math;
@@ -1970,17 +1970,29 @@ steps:
19701970
-import 'shaders.dart';
19711971
-
19721972
void main() {
1973-
runApp(const MainApp());
1973+
- runApp(const MainApp());
1974+
+ runApp(
1975+
+ MainApp(staticResourcesInitialized: scn.Scene.initializeStaticResources()),
1976+
+ );
19741977
}
1975-
@@ -25,6 +22,7 @@ class MainApp extends StatefulWidget {
1978+
1979+
class MainApp extends StatefulWidget {
1980+
- const MainApp({super.key});
1981+
+ const MainApp({super.key, required this.staticResourcesInitialized});
1982+
+
1983+
+ final Future<void> staticResourcesInitialized;
1984+
1985+
@override
1986+
State<MainApp> createState() => _MainAppState();
1987+
@@ -25,6 +26,7 @@ class MainApp extends StatefulWidget {
19761988
class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
19771989
late AnimationController _controller;
19781990
late Animation<double> _animation;
19791991
+ final scene = scn.Scene();
19801992
19811993
@override
19821994
void initState() {
1983-
@@ -35,6 +33,11 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
1995+
@@ -35,6 +37,11 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
19841996
)..repeat();
19851997
19861998
_animation = Tween<double>(begin: 0, end: 4 * math.pi).animate(_controller);
@@ -1992,7 +2004,7 @@ steps:
19922004
}
19932005
19942006
@override
1995-
@@ -46,14 +49,24 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
2007+
@@ -46,17 +53,35 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
19962008
@override
19972009
Widget build(BuildContext context) {
19982010
return MaterialApp(
@@ -2001,25 +2013,40 @@ steps:
20012013
debugShowCheckedModeBanner: false,
20022014
home: Scaffold(
20032015
body: SizedBox.expand(
2004-
child: AnimatedBuilder(
2005-
builder: (context, child) {
2006-
return CustomPaint(
2016+
- child: AnimatedBuilder(
2017+
- builder: (context, child) {
2018+
- return CustomPaint(
20072019
- painter: TrianglePainter(angle: _animation.value),
2008-
+ painter: ScenePainter(
2009-
+ scene: scene,
2010-
+ camera: scn.PerspectiveCamera(
2011-
+ position: vm.Vector3(
2012-
+ math.sin(_animation.value) * 3,
2013-
+ 2,
2014-
+ math.cos(_animation.value) * 3,
2020+
+ child: FutureBuilder(
2021+
+ future: widget.staticResourcesInitialized,
2022+
+ builder: (context, snapshot) {
2023+
+ if (snapshot.connectionState != ConnectionState.done) {
2024+
+ return const Center(child: CircularProgressIndicator());
2025+
+ }
2026+
+ return AnimatedBuilder(
2027+
+ builder: (context, child) {
2028+
+ return CustomPaint(
2029+
+ painter: ScenePainter(
2030+
+ scene: scene,
2031+
+ camera: scn.PerspectiveCamera(
2032+
+ position: vm.Vector3(
2033+
+ math.sin(_animation.value) * 3,
2034+
+ 2,
2035+
+ math.cos(_animation.value) * 3,
2036+
+ ),
2037+
+ target: vm.Vector3(0, 0, 0),
2038+
+ ),
20152039
+ ),
2016-
+ target: vm.Vector3(0, 0, 0),
2017-
+ ),
2018-
+ ),
2040+
+ );
2041+
+ },
2042+
+ animation: _animation,
20192043
);
20202044
},
2021-
animation: _animation,
2022-
@@ -64,151 +77,14 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
2045+
- animation: _animation,
2046+
),
2047+
),
2048+
),
2049+
@@ -64,151 +89,14 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
20232050
}
20242051
}
20252052
@@ -2176,6 +2203,29 @@ steps:
21762203
}
21772204
21782205
@override
2206+
- name: Patch text/widget_test.dart
2207+
path: intro_flutter_gpu/test/widget_test.dart
2208+
patch-u: |
2209+
--- b/intro_flutter_gpu/step_12/test/widget_test.dart
2210+
+++ a/intro_flutter_gpu/step_12/test/widget_test.dart
2211+
@@ -2,6 +2,7 @@
2212+
// Use of this source code is governed by a BSD-style license that can be
2213+
// found in the LICENSE file.
2214+
2215+
+import 'package:flutter_scene/scene.dart';
2216+
import 'package:flutter_test/flutter_test.dart';
2217+
2218+
import 'package:intro_flutter_gpu/main.dart';
2219+
@@ -9,6 +10,8 @@ import 'package:intro_flutter_gpu/main.dart';
2220+
void main() {
2221+
testWidgets('smoke test', (tester) async {
2222+
// Build our app and trigger a frame.
2223+
- await tester.pumpWidget(const MainApp());
2224+
+ await tester.pumpWidget(
2225+
+ MainApp(staticResourcesInitialized: Scene.initializeStaticResources()),
2226+
+ );
2227+
});
2228+
}
21792229
- name: Copy step_12
21802230
copydir:
21812231
from: intro_flutter_gpu

intro_flutter_gpu/step_12/lib/main.dart

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ import 'package:flutter_scene/scene.dart' as scn;
99
import 'package:vector_math/vector_math.dart' as vm;
1010

1111
void main() {
12-
runApp(const MainApp());
12+
runApp(
13+
MainApp(staticResourcesInitialized: scn.Scene.initializeStaticResources()),
14+
);
1315
}
1416

1517
class MainApp extends StatefulWidget {
16-
const MainApp({super.key});
18+
const MainApp({super.key, required this.staticResourcesInitialized});
19+
20+
final Future<void> staticResourcesInitialized;
1721

1822
@override
1923
State<MainApp> createState() => _MainAppState();
@@ -53,23 +57,31 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
5357
debugShowCheckedModeBanner: false,
5458
home: Scaffold(
5559
body: SizedBox.expand(
56-
child: AnimatedBuilder(
57-
builder: (context, child) {
58-
return CustomPaint(
59-
painter: ScenePainter(
60-
scene: scene,
61-
camera: scn.PerspectiveCamera(
62-
position: vm.Vector3(
63-
math.sin(_animation.value) * 3,
64-
2,
65-
math.cos(_animation.value) * 3,
60+
child: FutureBuilder(
61+
future: widget.staticResourcesInitialized,
62+
builder: (context, snapshot) {
63+
if (snapshot.connectionState != ConnectionState.done) {
64+
return const Center(child: CircularProgressIndicator());
65+
}
66+
return AnimatedBuilder(
67+
builder: (context, child) {
68+
return CustomPaint(
69+
painter: ScenePainter(
70+
scene: scene,
71+
camera: scn.PerspectiveCamera(
72+
position: vm.Vector3(
73+
math.sin(_animation.value) * 3,
74+
2,
75+
math.cos(_animation.value) * 3,
76+
),
77+
target: vm.Vector3(0, 0, 0),
78+
),
6679
),
67-
target: vm.Vector3(0, 0, 0),
68-
),
69-
),
80+
);
81+
},
82+
animation: _animation,
7083
);
7184
},
72-
animation: _animation,
7385
),
7486
),
7587
),

intro_flutter_gpu/step_12/test/widget_test.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter_scene/scene.dart';
56
import 'package:flutter_test/flutter_test.dart';
67

78
import 'package:intro_flutter_gpu/main.dart';
89

910
void main() {
1011
testWidgets('smoke test', (tester) async {
1112
// Build our app and trigger a frame.
12-
await tester.pumpWidget(const MainApp());
13+
await tester.pumpWidget(
14+
MainApp(staticResourcesInitialized: Scene.initializeStaticResources()),
15+
);
1316
});
1417
}

0 commit comments

Comments
 (0)