Skip to content

Commit

Permalink
partial support for loading images on wiki embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed Jun 12, 2024
1 parent 6d5e247 commit 45b91c2
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions lib/Frontend/pages/html_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -42,6 +43,7 @@ class _HtmlPageState extends State<HtmlPage> {
child: HtmlWidget(
body,
renderMode: RenderMode.listView,
factoryBuilder: MyWidgetFactory.new,
onTapUrl: (p0) async => launchUrlString('$p0$getOutboundUtm()'),
),
),
Expand All @@ -67,3 +69,61 @@ class _HtmlPageState extends State<HtmlPage> {
}
}
}

class MyWidgetFactory extends WidgetFactory {
@override
Widget? buildImageWidget(BuildMetadata tree, ImageSource src) {
final url = src.url;
return LoadImage(url: src.url);
}
}

class LoadImage extends StatefulWidget {
final String url;

const LoadImage({required this.url, super.key});

@override
State<StatefulWidget> createState() => _LoadImageState();
}

class _LoadImageState extends State<LoadImage> {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: loadImage(),
builder: (context, snapshot) {
return AnimatedSwitcher(
duration: animationTransitionDuration,
child: Builder(
builder: (context) {
if (snapshot.hasData) {
return snapshot.data!;
} else {
return const CircularProgressIndicator();
}
},
),
);
},
);
}

Future<Widget> loadImage() async {
Dio dio = await initDio();
Response<List<int>> response = await dio.get(
widget.url,
options: Options(
responseType: ResponseType.bytes,
followRedirects: true,
),
);
if (response.statusCode! < 400 && mounted) {
return Image.memory(
Uint8List.fromList(response.data!),
fit: BoxFit.fill,
);
}
return Container();
}
}

0 comments on commit 45b91c2

Please sign in to comment.