flutter3d
Showcase Changelog 38 packages API reference

Storage on every platform: the source

since 0.7.0 Widgets and the rest

Regions the guide quotes: text (line 38), remove (line 42), binary (line 45).

1/// Small documents a player's choices live in, kept the right way on
2/// whichever of five platforms is running: a file on four of them, an
3/// IndexedDB record for anything too big for `localStorage` on the fifth.
4///
5/// Quoted by `storage.md` and shown whole in the Source tab.
6library;
7
8import 'dart:typed_data';
9
10import 'package:flutter/widgets.dart';
11import 'package:flutter3d/flutter3d.dart';
12import 'package:flutter3d_app/flutter3d_app.dart';
13import 'package:flutter3d_showcase/src/demo/demo.dart';
14import 'package:vector_math/vector_math.dart';
16final class StorageDemo extends ShowcaseDemo {
17 late final String _report;
19 @override
20 Scene build(DemoContext context) {
21 final material = Material(
22 name: 'disk',
23 baseColor: Vector4(0.6, 0.6, 0.7, 1.0),
24 );
25 final node = MeshNode(
26 DeviceMesh.upload(context.device, SphereShape(segments: 16).build()),
27 material,
28 );
29 return Scene()
30 ..add(node)
31 ..add(
32 LightNode(name: 'sun', intensity: 3.0)
33 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
34 );
35 }
37 static Future<String> _run() async {
38 final storage = defaultStorage('flutter3d-showcase-demo');
39 final wrote = storage.write('settings.json', '{"volume":0.8}');
40 final readBack = storage.read('settings.json');
42 storage.remove('settings.json');
43 final afterRemove = storage.read('settings.json');
45 final binary = defaultBinaryStorage('flutter3d-showcase-demo');
46 final bytes = Uint8List.fromList(<int>[1, 2, 3, 4]);
47 await binary.write('blob.bin', bytes);
48 final readBytes = await binary.read('blob.bin');
49 await binary.remove('blob.bin');
51 return 'wrote: $wrote, read back: $readBack, after remove: $afterRemove\n'
52 'binary round trip: $readBytes';
53 }
55 @override
56 Future<void> prepare(DemoContext context) async {
57 _report = await _run();
58 }
60 @override
61 Widget? customBody(BuildContext buildContext, DemoContext context) =>
62 Container(
63 color: const Color(0xFF14161A),
64 padding: const EdgeInsets.all(24),
65 alignment: Alignment.topLeft,
66 child: DefaultTextStyle(
67 style: const TextStyle(color: Color(0xFFE8E8EC), fontSize: 16),
68 child: Text(_report),
69 ),
70 );
72 @override
73 void verify(Scene scene, FrameResult frame) {
74 if (frame.drawCalls < 1) {
75 throw StateError('the disk marker was not drawn');
76 }
77 if (!_report.contains('read back: {"volume":0.8}')) {
78 throw StateError('what was written should read back exactly');
79 }
80 if (!_report.contains('after remove: null')) {
81 throw StateError('a removed document should read back as nothing');
82 }
83 if (!_report.contains('binary round trip: [1, 2, 3, 4]')) {
84 throw StateError('the bytes written should read back exactly');
85 }
86 }