Files
rive-flutter/example/lib/custom_asset_loading.dart
HayesGordon ca712600bc refactor: asset resolving
Adds some cleanup to this previous PR: https://github.com/rive-app/rive/pull/5411/files

- Brings back some classes and parameters and mark them as deprecated.
- Changed the naming for parameters
- Removed asset resolving from the high level `RiveAnimation` widget, prefer to manage this by loading in your own `RiveFile`. This is to avoid introducing too many changes that we may revert down the line
- Removed code comments that were intended as questions, marked some as TODOs
- Improved documentation and cleaned up example

The cached asset example now has a button to hot swap out assets at runtime by keeping a reference to the asset. This works for images, but not for Fonts.
- We can either remove this example for the time being
- Or investigate why it does not swap out (I would expect it to)

Diffs=
94e3490ae refactor: asset resolving (#5563)
bae069339 Stop automatically pruning empty segments in RawPath (#5557)
2d2d8c413 Line Height & Paragraph Spacing (#5552)

Co-authored-by: Gordon <pggordonhayes@gmail.com>
2023-07-13 14:06:41 +00:00

171 lines
4.4 KiB
Dart

import 'dart:math';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:rive/rive.dart';
import 'package:http/http.dart' as http;
/// An example showing how to load image or font assets dynamically.
///
/// In this example you'll note that there is a delay in the assets
/// loading/refreshing when you tap the back/forward buttons.
/// This is because the assets are being loaded asynchronously.
/// If you want to avoid this delay you can cache the assets in memory
/// and provide them instantly.
///
/// See `custom_cached_asset_loading.dart` for an example of this.
class CustomAssetLoading extends StatefulWidget {
const CustomAssetLoading({Key? key}) : super(key: key);
@override
State<CustomAssetLoading> createState() => _CustomAssetLoadingState();
}
class _CustomAssetLoadingState extends State<CustomAssetLoading> {
var _index = 0;
void next() => setState(() {
_index += 1;
});
void previous() => setState(() {
_index -= 1;
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Custom Asset Loading'),
),
body: Center(
child: Row(
children: [
GestureDetector(
onTap: previous,
child: const Icon(Icons.arrow_back),
),
Expanded(
child: (_index % 2 == 0)
? const _RiveRandomImage()
: const _RiveRandomFont(),
),
GestureDetector(
onTap: next,
child: const Icon(Icons.arrow_forward),
),
],
),
),
);
}
}
/// Loads a random image as an asset.
class _RiveRandomImage extends StatefulWidget {
const _RiveRandomImage();
@override
State<_RiveRandomImage> createState() => _RiveRandomImageState();
}
class _RiveRandomImageState extends State<_RiveRandomImage> {
@override
void initState() {
super.initState();
_loadFiles();
}
RiveFile? _riveImageSampleFile;
Future<void> _loadFiles() async {
final imageFile = await RiveFile.asset(
'assets/asset.riv',
loadEmbeddedAssets: false,
assetLoader: CallbackAssetLoader(
(asset) async {
final res =
await http.get(Uri.parse('https://picsum.photos/1000/1000'));
await asset.decode(Uint8List.view(res.bodyBytes.buffer));
return true;
},
),
);
setState(() {
_riveImageSampleFile = imageFile;
});
}
@override
Widget build(BuildContext context) {
if (_riveImageSampleFile == null) {
return const Center(child: CircularProgressIndicator());
}
return RiveAnimation.direct(
_riveImageSampleFile!,
fit: BoxFit.cover,
);
}
}
/// Loads a random font as an asset.
class _RiveRandomFont extends StatefulWidget {
const _RiveRandomFont();
@override
State<_RiveRandomFont> createState() => _RiveRandomFontState();
}
class _RiveRandomFontState extends State<_RiveRandomFont> {
@override
void initState() {
super.initState();
_loadFiles();
}
RiveFile? _riveFontSampleFile;
Future<void> _loadFiles() async {
final fontFile = await RiveFile.asset(
'assets/sampletext.riv',
loadEmbeddedAssets: false, // disable loading embedded assets.
assetLoader: CallbackAssetLoader(
(asset) async {
final urls = [
'https://cdn.rive.app/runtime/flutter/IndieFlower-Regular.ttf',
'https://cdn.rive.app/runtime/flutter/comic-neue.ttf',
'https://cdn.rive.app/runtime/flutter/inter.ttf',
'https://cdn.rive.app/runtime/flutter/inter-tight.ttf',
'https://cdn.rive.app/runtime/flutter/josefin-sans.ttf',
'https://cdn.rive.app/runtime/flutter/send-flowers.ttf',
];
final res = await http.get(
// pick a random url from the list of fonts
Uri.parse(urls[Random().nextInt(urls.length)]),
);
await asset.decode(Uint8List.view(res.bodyBytes.buffer));
return true;
},
),
);
setState(() {
_riveFontSampleFile = fontFile;
});
}
@override
Widget build(BuildContext context) {
if (_riveFontSampleFile == null) {
return const Center(child: CircularProgressIndicator());
}
return RiveAnimation.direct(
_riveFontSampleFile!,
fit: BoxFit.cover,
);
}
}