voyagehandbook/lib/views/pageview.dart
2023-03-28 18:05:05 +02:00

94 lines
2.4 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:voyagehandbook/api/wikimedia.dart';
import 'package:voyagehandbook/util/drawer.dart';
import 'package:voyagehandbook/util/render.dart';
import 'package:voyagehandbook/util/storage.dart';
import 'package:voyagehandbook/util/styles.dart';
/// Renders a single WikiVoyage article
class ArticleView extends StatefulWidget {
const ArticleView({super.key, required this.pageKey, required this.name});
final String pageKey;
final String name;
@override
State<ArticleView> createState() => _ArticleViewState();
}
class _ArticleViewState extends State<ArticleView> {
var _content = <Widget>[];
@override
didChangeDependencies() {
super.didChangeDependencies();
loadPage();
}
@override
void initState() {
super.initState();
addToRecents();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: (kDebugMode)
? [
IconButton(
onPressed: () => loadPage(),
icon: const Icon(Icons.restart_alt))
]
: null),
drawer: genDrawer(0, context),
body: Center(
child: SizedBox(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width * 0.9,
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: _content,
),
),
),
),
);
}
void loadPage() async {
try {
_content = [
SizedBox(
width: MediaQuery.of(context).size.width * 0.9,
height: MediaQuery.of(context).size.height,
child: renderFromPageHTML(
await WikiApi.getRawPage(widget.pageKey),
MediaQuery.of(context).size.height,
MediaQuery.of(context).size.width),
)
];
} catch (e) {
if (kDebugMode) print(e);
_content = [
const Text(
"Error while rendering:",
style: PageStyles.h1,
),
const SizedBox(
height: 10,
),
Text(e.toString())
];
}
setState(() {});
}
void addToRecents() {
StorageAccess.addToRecents(widget.name, widget.pageKey);
}
}