Skip to content

Instantly share code, notes, and snippets.

@evoL
Last active July 16, 2023 19:21
Show Gist options
  • Save evoL/00398769300a9e28c231ba37615828af to your computer and use it in GitHub Desktop.
Save evoL/00398769300a9e28c231ba37615828af to your computer and use it in GitHub Desktop.
Flutter's GIF renderer is too compatible with the GIF89a spec unlike the rest of the world
import 'package:flutter/material.dart';
import 'dart:typed_data';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// Delay in milliseconds. Valid range is 0-655350.
int blackDelayMs = 500;
int redDelayMs = 0;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Column(
children: [
Slider(
min: 0,
max: 1000,
divisions: 100,
activeColor: Colors.black,
value: blackDelayMs.toDouble(),
label: "${blackDelayMs.round()} ms",
onChanged: (double value) {
setState(() {
blackDelayMs = value.round().toInt();
});
},
),
Slider(
min: 0,
max: 1000,
divisions: 100,
activeColor: Colors.red,
value: redDelayMs.toDouble(),
label: "${redDelayMs.round()} ms",
onChanged: (double value) {
setState(() {
redDelayMs = value.round().toInt();
});
},
),
SizedBox(
width: 200,
height: 200,
child: FittedBox(
fit: BoxFit.contain,
child: Image.memory(generateGif(
blackDelayMs: blackDelayMs,
redDelayMs: redDelayMs,
)),
),
),
],
),
),
),
);
}
}
// GIF data follows.
List<int> toBytes(int millis) {
final hundreds = millis ~/ 10;
return [hundreds & 0xff, (hundreds & 0xffff) >>> 8];
}
Uint8List generateGif({required int blackDelayMs, required int redDelayMs}) {
return Uint8List.fromList([
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x50, 0x00,
0x50, 0x00, 0xf1, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff,
0x00, 0x21, 0xff, 0x0b, 0x4e, 0x45, 0x54, 0x53,
0x43, 0x41, 0x50, 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xcb, 0xdb, 0xeb, 0xfb, 0x0b,
0x1c, 0x2c, 0x3c, 0x4c, 0x5c, 0x6c, 0x7c, 0x8c,
0x9c, 0xac, 0xbc, 0xcc, 0xdc, 0xec, 0xfc, 0x0c,
0x1d, 0x2d, 0x3d, 0xed, 0x51, 0x00, 0x00, 0x21,
0xf9, 0x04, 0x04, ...toBytes(redDelayMs), 0x00, 0x00, 0x2c,
0x28, 0x00, 0x00, 0x00, 0x28, 0x00, 0x28, 0x00,
0x00, 0x02, 0x27, 0x94, 0x8f, 0xa9, 0xcb, 0xed,
0x0f, 0xa3, 0x9c, 0xb4, 0xda, 0x8b, 0xb3, 0xde,
0xbc, 0xfb, 0x0f, 0x86, 0xe2, 0x48, 0x96, 0xe6,
0x89, 0xa6, 0xea, 0xca, 0xb6, 0xee, 0x0b, 0xc7,
0xf2, 0x4c, 0xd7, 0xf6, 0x8d, 0xe7, 0xfa, 0xce,
0x23, 0x05, 0x00, 0x21, 0xf9, 0x04, 0x04, ...toBytes(blackDelayMs),
0x00, 0x00, 0x2c, 0x28, 0x00, 0x00, 0x00,
0x28, 0x00, 0x28, 0x00, 0x00, 0x02, 0x27, 0x8c,
0x8f, 0xa9, 0xcb, 0xed, 0x0f, 0xa3, 0x9c, 0xb4,
0xda, 0x8b, 0xb3, 0xde, 0xbc, 0xfb, 0x0f, 0x86,
0xe2, 0x48, 0x96, 0xe6, 0x89, 0xa6, 0xea, 0xca,
0xb6, 0xee, 0x0b, 0xc7, 0xf2, 0x4c, 0xd7, 0xf6,
0x8d, 0xe7, 0xfa, 0xce, 0x23, 0x05, 0x00, 0x21,
0xf9, 0x04, 0x04, ...toBytes(redDelayMs), 0x00, 0x00, 0x2c,
0x28, 0x00, 0x28, 0x00, 0x28, 0x00, 0x28, 0x00,
0x00, 0x02, 0x27, 0x94, 0x8f, 0xa9, 0xcb, 0xed,
0x0f, 0xa3, 0x9c, 0xb4, 0xda, 0x8b, 0xb3, 0xde,
0xbc, 0xfb, 0x0f, 0x86, 0xe2, 0x48, 0x96, 0xe6,
0x89, 0xa6, 0xea, 0xca, 0xb6, 0xee, 0x0b, 0xc7,
0xf2, 0x4c, 0xd7, 0xf6, 0x8d, 0xe7, 0xfa, 0xce,
0x23, 0x05, 0x00, 0x21, 0xf9, 0x04, 0x04, ...toBytes(blackDelayMs),
0x00, 0x00, 0x2c, 0x28, 0x00, 0x28, 0x00,
0x28, 0x00, 0x28, 0x00, 0x00, 0x02, 0x27, 0x8c,
0x8f, 0xa9, 0xcb, 0xed, 0x0f, 0xa3, 0x9c, 0xb4,
0xda, 0x8b, 0xb3, 0xde, 0xbc, 0xfb, 0x0f, 0x86,
0xe2, 0x48, 0x96, 0xe6, 0x89, 0xa6, 0xea, 0xca,
0xb6, 0xee, 0x0b, 0xc7, 0xf2, 0x4c, 0xd7, 0xf6,
0x8d, 0xe7, 0xfa, 0xce, 0x23, 0x05, 0x00, 0x21,
0xf9, 0x04, 0x04, ...toBytes(redDelayMs), 0x00, 0x00, 0x2c,
0x00, 0x00, 0x28, 0x00, 0x28, 0x00, 0x28, 0x00,
0x00, 0x02, 0x27, 0x94, 0x8f, 0xa9, 0xcb, 0xed,
0x0f, 0xa3, 0x9c, 0xb4, 0xda, 0x8b, 0xb3, 0xde,
0xbc, 0xfb, 0x0f, 0x86, 0xe2, 0x48, 0x96, 0xe6,
0x89, 0xa6, 0xea, 0xca, 0xb6, 0xee, 0x0b, 0xc7,
0xf2, 0x4c, 0xd7, 0xf6, 0x8d, 0xe7, 0xfa, 0xce,
0x23, 0x05, 0x00, 0x21, 0xf9, 0x04, 0x04, ...toBytes(blackDelayMs),
0x00, 0x00, 0x2c, 0x00, 0x00, 0x28, 0x00,
0x28, 0x00, 0x28, 0x00, 0x00, 0x02, 0x27, 0x8c,
0x8f, 0xa9, 0xcb, 0xed, 0x0f, 0xa3, 0x9c, 0xb4,
0xda, 0x8b, 0xb3, 0xde, 0xbc, 0xfb, 0x0f, 0x86,
0xe2, 0x48, 0x96, 0xe6, 0x89, 0xa6, 0xea, 0xca,
0xb6, 0xee, 0x0b, 0xc7, 0xf2, 0x4c, 0xd7, 0xf6,
0x8d, 0xe7, 0xfa, 0xce, 0x23, 0x05, 0x00, 0x3b
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment