git-touch-android-ios-app/lib/widgets/blob_view.dart

71 lines
2.1 KiB
Dart
Raw Normal View History

2020-01-30 07:06:25 +01:00
import 'dart:convert';
2022-09-17 14:35:45 +02:00
2019-12-11 16:58:25 +01:00
import 'package:flutter/cupertino.dart';
2022-09-17 14:35:45 +02:00
import 'package:flutter_highlight/flutter_highlight.dart';
2019-12-11 16:58:25 +01:00
import 'package:flutter_highlight/theme_map.dart';
import 'package:git_touch/models/code.dart';
import 'package:git_touch/models/theme.dart';
2022-09-17 14:35:45 +02:00
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/loading.dart';
2019-12-11 16:58:25 +01:00
import 'package:git_touch/widgets/markdown_view.dart';
import 'package:provider/provider.dart';
class BlobView extends StatelessWidget {
2022-09-06 18:28:12 +02:00
const BlobView(
2020-01-31 07:31:24 +01:00
this.name, {
2020-02-02 12:50:00 +01:00
this.text,
this.base64Text,
2020-01-31 07:31:24 +01:00
this.networkUrl,
});
2022-09-21 18:28:21 +02:00
final String? name;
final String? text;
final String? base64Text;
final String? networkUrl;
2019-12-11 16:58:25 +01:00
2021-05-16 09:16:35 +02:00
String get _text => text ?? base64Text!.base64ToUtf8;
2020-02-02 12:50:00 +01:00
2019-12-11 16:58:25 +01:00
@override
Widget build(BuildContext context) {
final codeProvider = Provider.of<CodeModel>(context);
final theme = Provider.of<ThemeModel>(context);
2021-05-16 09:16:35 +02:00
switch (name!.ext) {
2019-12-11 16:58:25 +01:00
// TODO: All image types
case 'png':
case 'jpg':
case 'jpeg':
case 'gif':
case 'webp':
2020-01-30 07:06:25 +01:00
// return PhotoView(
// imageProvider: MemoryImage(Uint8List.fromList(bits)),
2022-09-24 20:46:37 +02:00
// backgroundDecoration: BoxDecoration(color: AntTheme.of(context).background),
2020-01-30 07:06:25 +01:00
// );
return base64Text == null
? Image.network(
2021-05-16 09:16:35 +02:00
networkUrl!,
loadingBuilder: (_, child, p) {
if (p == null) return child;
// TODO: progress
2022-09-06 18:28:12 +02:00
return const Loading();
},
)
2021-05-16 09:16:35 +02:00
: Image.memory(base64.decode(base64Text!));
2019-12-11 16:58:25 +01:00
case 'md':
case 'markdown':
2020-11-04 09:41:04 +01:00
return MarkdownFlutterView(_text);
2019-12-11 16:58:25 +01:00
default:
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: HighlightView(
2020-02-02 12:50:00 +01:00
_text,
2021-05-16 09:16:35 +02:00
language: name!.ext ?? 'plaintext',
theme: themeMap[theme.brightness == Brightness.dark
? codeProvider.themeDark
2021-05-16 09:16:35 +02:00
: codeProvider.theme]!,
2019-12-11 16:58:25 +01:00
padding: CommonStyle.padding,
2021-07-20 08:33:34 +02:00
textStyle: codeProvider.fontStyle,
2019-12-11 16:58:25 +01:00
),
);
}
}
}