95 lines
2.6 KiB
Dart
95 lines
2.6 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_highlight/flutter_highlight.dart';
|
|
import 'package:flutter_highlight/themes/github.dart';
|
|
import 'package:flutter_markdown/flutter_markdown.dart';
|
|
import 'package:gitea_client/model/user.dart';
|
|
import 'package:gitea_client/service/gitea_service.dart';
|
|
|
|
import '../model/File.dart';
|
|
|
|
class CodePageData {
|
|
final String filePath;
|
|
final String repo;
|
|
final String owner;
|
|
final SavedUser user;
|
|
|
|
const CodePageData(this.filePath, this.repo, this.owner, this.user);
|
|
}
|
|
|
|
class CodePage extends StatefulWidget {
|
|
final String filePath;
|
|
final String repo;
|
|
final String owner;
|
|
final SavedUser user;
|
|
|
|
const CodePage(
|
|
{Key? key,
|
|
required this.filePath,
|
|
required this.repo,
|
|
required this.owner,
|
|
required this.user})
|
|
: super(key: key);
|
|
|
|
@override
|
|
_CodePage createState() => _CodePage();
|
|
}
|
|
|
|
class _CodePage extends State<CodePage> {
|
|
Future<RepoFile>? fileRequest;
|
|
|
|
@override
|
|
void initState() {
|
|
GiteaService giteaService = GiteaService(apiAccess: widget.user.apiAccess);
|
|
fileRequest =
|
|
giteaService.getFile(widget.owner, widget.repo, widget.filePath);
|
|
}
|
|
|
|
@override
|
|
Widget build(context) {
|
|
final media = MediaQuery.of(context).size;
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(widget.filePath),
|
|
),
|
|
body: FutureBuilder<RepoFile>(
|
|
future: fileRequest,
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasError) {
|
|
return Center(
|
|
child: Column(
|
|
children: [
|
|
Text("No Readme file found!",
|
|
style: Theme.of(context).textTheme.headline6),
|
|
],
|
|
),
|
|
);
|
|
} else if (snapshot.hasData) {
|
|
final file = snapshot.data!;
|
|
|
|
final content = utf8.decode(base64.decode(file.content!));
|
|
|
|
return Center(
|
|
child: SizedBox(
|
|
width:
|
|
(media.width > 600) ? media.width * 0.6 : media.width * 0.9,
|
|
height: media.height * 0.7,
|
|
child: (file.name.endsWith(".md"))
|
|
? Markdown(selectable: true, data: content)
|
|
: SingleChildScrollView(
|
|
child: HighlightView(
|
|
content,
|
|
language: "dart",
|
|
theme: githubTheme,
|
|
)),
|
|
));
|
|
} else {
|
|
return const Center(
|
|
child: CircularProgressIndicator(),
|
|
);
|
|
}
|
|
}),
|
|
);
|
|
}
|
|
}
|