2020-01-30 07:06:25 +01:00
|
|
|
import 'dart:convert';
|
2019-12-11 16:58:25 +01:00
|
|
|
import 'package:path/path.dart' as p;
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter_highlight/theme_map.dart';
|
|
|
|
import 'package:flutter_svg/flutter_svg.dart';
|
|
|
|
import 'package:git_touch/models/code.dart';
|
|
|
|
import 'package:git_touch/models/theme.dart';
|
|
|
|
import 'package:git_touch/widgets/markdown_view.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_highlight/flutter_highlight.dart';
|
|
|
|
import 'package:photo_view/photo_view.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:git_touch/utils/utils.dart';
|
|
|
|
|
|
|
|
class BlobView extends StatelessWidget {
|
2020-01-30 07:06:25 +01:00
|
|
|
final String name;
|
|
|
|
final String text;
|
|
|
|
final String base64Text;
|
|
|
|
final String networkUrl;
|
|
|
|
BlobView(this.name, {this.text, this.base64Text, this.networkUrl})
|
|
|
|
: assert(text == null || base64Text == null);
|
2019-12-11 16:58:25 +01:00
|
|
|
|
|
|
|
String get _extname {
|
2020-01-30 07:06:25 +01:00
|
|
|
var dotext = p.extension(name);
|
2019-12-11 16:58:25 +01:00
|
|
|
if (dotext.isEmpty) return '';
|
|
|
|
return dotext.substring(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
String get _language => _extname.isEmpty ? 'plaintext' : _extname;
|
2020-01-30 07:06:25 +01:00
|
|
|
String get _text => text ?? utf8.decode(base64.decode(base64Text));
|
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);
|
|
|
|
switch (_extname) {
|
|
|
|
// 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)),
|
|
|
|
// backgroundDecoration: BoxDecoration(color: theme.palette.background),
|
|
|
|
// );
|
|
|
|
return base64Text == null
|
|
|
|
? Image.network(networkUrl)
|
|
|
|
: Image.memory(base64.decode(base64Text));
|
|
|
|
case 'svg':
|
|
|
|
return base64Text == null
|
|
|
|
? SvgPicture.network(networkUrl)
|
|
|
|
: SvgPicture.memory(base64.decode(base64Text));
|
2019-12-11 16:58:25 +01:00
|
|
|
case 'md':
|
|
|
|
case 'markdown':
|
|
|
|
return Padding(
|
|
|
|
padding: CommonStyle.padding,
|
2020-01-30 07:06:25 +01:00
|
|
|
child: MarkdownView(_text), // TODO: basePath
|
2019-12-11 16:58:25 +01:00
|
|
|
);
|
|
|
|
default:
|
|
|
|
return SingleChildScrollView(
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
child: HighlightView(
|
2020-01-30 07:06:25 +01:00
|
|
|
_text,
|
2019-12-11 16:58:25 +01:00
|
|
|
language: _language,
|
2020-01-29 05:44:10 +01:00
|
|
|
theme: themeMap[theme.brightness == Brightness.dark
|
|
|
|
? codeProvider.themeDark
|
|
|
|
: codeProvider.theme],
|
2019-12-11 16:58:25 +01:00
|
|
|
padding: CommonStyle.padding,
|
|
|
|
textStyle: TextStyle(
|
|
|
|
fontSize: codeProvider.fontSize.toDouble(),
|
|
|
|
fontFamily: codeProvider.fontFamilyUsed),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|