voyagehandbook/lib/views/pageview.dart
2023-03-27 19:45:57 +02:00

78 lines
2.1 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';
/// 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 {
_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),
)
];
setState(() {});
}
void addToRecents() {
StorageAccess.addToRecents(widget.name, widget.pageKey);
}
}