|
| 1 | +// |
| 2 | +// Spine Runtimes License Agreement |
| 3 | +// Last updated April 5, 2025. Replaces all prior versions. |
| 4 | +// |
| 5 | +// Copyright (c) 2013-2025, Esoteric Software LLC |
| 6 | +// |
| 7 | +// Integration of the Spine Runtimes into software or otherwise creating |
| 8 | +// derivative works of the Spine Runtimes is permitted under the terms and |
| 9 | +// conditions of Section 2 of the Spine Editor License Agreement: |
| 10 | +// http://esotericsoftware.com/spine-editor-license |
| 11 | +// |
| 12 | +// Otherwise, it is permitted to integrate the Spine Runtimes into software |
| 13 | +// or otherwise create derivative works of the Spine Runtimes (collectively, |
| 14 | +// "Products"), provided that each user of the Products must obtain their own |
| 15 | +// Spine Editor license and redistribution of the Products in any form must |
| 16 | +// include this license and copyright notice. |
| 17 | +// |
| 18 | +// THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY |
| 19 | +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +// DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY |
| 22 | +// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, |
| 24 | +// BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND |
| 25 | +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | +// THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +// |
| 29 | + |
| 30 | +import 'dart:typed_data'; |
| 31 | +import 'package:spine_flutter/spine_flutter.dart'; |
| 32 | +import 'package:flutter/material.dart'; |
| 33 | +import 'package:flutter/services.dart'; |
| 34 | + |
| 35 | +/// This example demonstrates loading Spine skeleton data from memory using the |
| 36 | +/// fromMemory methods. This is useful when you want to: |
| 37 | +/// - Load data from custom storage (e.g., encrypted assets, databases) |
| 38 | +/// - Implement custom caching strategies |
| 39 | +/// - Download and cache assets at runtime |
| 40 | +/// - Pre-process assets before loading |
| 41 | +/// |
| 42 | +/// The example loads all files (atlas, skeleton, images) into memory first, |
| 43 | +/// then uses the fromMemory API to create a SpineWidget. |
| 44 | +class LoadFromMemory extends StatefulWidget { |
| 45 | + const LoadFromMemory({super.key}); |
| 46 | + |
| 47 | + @override |
| 48 | + State<LoadFromMemory> createState() => _LoadFromMemoryState(); |
| 49 | +} |
| 50 | + |
| 51 | +class _LoadFromMemoryState extends State<LoadFromMemory> { |
| 52 | + // In-memory cache of all loaded files |
| 53 | + final Map<String, Uint8List> _fileCache = {}; |
| 54 | + bool _isLoading = true; |
| 55 | + String _loadingStatus = 'Loading assets into memory...'; |
| 56 | + |
| 57 | + @override |
| 58 | + void initState() { |
| 59 | + super.initState(); |
| 60 | + _loadAssetsIntoMemory(); |
| 61 | + } |
| 62 | + |
| 63 | + Future<void> _loadAssetsIntoMemory() async { |
| 64 | + try { |
| 65 | + // Step 1: Load atlas file into memory |
| 66 | + setState(() => _loadingStatus = 'Loading atlas file...'); |
| 67 | + final atlasBytes = await rootBundle.load('assets/spineboy.atlas'); |
| 68 | + _fileCache['assets/spineboy.atlas'] = atlasBytes.buffer.asUint8List(); |
| 69 | + |
| 70 | + // Step 2: Load skeleton file into memory |
| 71 | + setState(() => _loadingStatus = 'Loading skeleton file...'); |
| 72 | + final skelBytes = await rootBundle.load('assets/spineboy-pro.skel'); |
| 73 | + _fileCache['assets/spineboy-pro.skel'] = skelBytes.buffer.asUint8List(); |
| 74 | + |
| 75 | + // Step 3: Load image file(s) into memory |
| 76 | + setState(() => _loadingStatus = 'Loading image files...'); |
| 77 | + final imageBytes = await rootBundle.load('assets/spineboy.png'); |
| 78 | + _fileCache['assets/spineboy.png'] = imageBytes.buffer.asUint8List(); |
| 79 | + |
| 80 | + // All files loaded into memory! |
| 81 | + setState(() { |
| 82 | + _loadingStatus = 'All assets loaded into memory (${_fileCache.length} files, ${_getTotalSize()} bytes)'; |
| 83 | + _isLoading = false; |
| 84 | + }); |
| 85 | + } catch (e) { |
| 86 | + setState(() { |
| 87 | + _loadingStatus = 'Error loading assets: $e'; |
| 88 | + _isLoading = false; |
| 89 | + }); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + int _getTotalSize() { |
| 94 | + return _fileCache.values.fold(0, (sum, bytes) => sum + bytes.length); |
| 95 | + } |
| 96 | + |
| 97 | + // Custom file loader that returns data from our in-memory cache |
| 98 | + Future<Uint8List> _loadFromCache(String filename) async { |
| 99 | + final data = _fileCache[filename]; |
| 100 | + if (data == null) { |
| 101 | + throw Exception('File not found in cache: $filename'); |
| 102 | + } |
| 103 | + return data; |
| 104 | + } |
| 105 | + |
| 106 | + @override |
| 107 | + Widget build(BuildContext context) { |
| 108 | + return Scaffold( |
| 109 | + appBar: AppBar(title: const Text('Load From Memory')), |
| 110 | + body: _isLoading |
| 111 | + ? Center( |
| 112 | + child: Column( |
| 113 | + mainAxisSize: MainAxisSize.min, |
| 114 | + children: [ |
| 115 | + const CircularProgressIndicator(), |
| 116 | + const SizedBox(height: 16), |
| 117 | + Text(_loadingStatus), |
| 118 | + ], |
| 119 | + ), |
| 120 | + ) |
| 121 | + : Column( |
| 122 | + children: [ |
| 123 | + Container( |
| 124 | + padding: const EdgeInsets.all(16), |
| 125 | + color: Colors.blue.shade50, |
| 126 | + child: Column( |
| 127 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 128 | + children: [ |
| 129 | + Text( |
| 130 | + 'Files in Memory Cache:', |
| 131 | + style: Theme.of(context).textTheme.titleMedium, |
| 132 | + ), |
| 133 | + const SizedBox(height: 8), |
| 134 | + ..._fileCache.entries.map((entry) => Text( |
| 135 | + ' ${entry.key}: ${entry.value.length} bytes', |
| 136 | + style: Theme.of(context).textTheme.bodySmall, |
| 137 | + )), |
| 138 | + const SizedBox(height: 8), |
| 139 | + Text( |
| 140 | + 'Total: ${_getTotalSize()} bytes', |
| 141 | + style: Theme.of(context).textTheme.titleSmall, |
| 142 | + ), |
| 143 | + ], |
| 144 | + ), |
| 145 | + ), |
| 146 | + Expanded( |
| 147 | + child: SpineWidget.fromMemory( |
| 148 | + 'assets/spineboy.atlas', |
| 149 | + 'assets/spineboy-pro.skel', |
| 150 | + _loadFromCache, |
| 151 | + SpineWidgetController( |
| 152 | + onInitialized: (controller) { |
| 153 | + controller.animationState.setAnimation(0, 'walk', true); |
| 154 | + }, |
| 155 | + ), |
| 156 | + ), |
| 157 | + ), |
| 158 | + ], |
| 159 | + ), |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
0 commit comments