-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.dart
127 lines (104 loc) · 3.21 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import 'dart:developer';
import 'package:example/data/random_article_web_pages.dart';
import 'package:example/web_view_page.dart';
import 'package:flutter/material.dart';
import 'package:xayn_readability/xayn_readability.dart';
import 'widgets/bottom_navigation_widget.dart';
import 'widgets/top_navigation_widget.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Read Web Articles',
theme: ThemeData.dark(),
home: const Home(),
);
}
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
final TextEditingController textEditingController = TextEditingController();
final ReaderModeController readerModeController = ReaderModeController();
bool isReaderModeView = true;
@override
void initState() {
readerModeController.addListener(() {
setState(() {
textEditingController.text = readerModeController.uri.toString();
});
});
loadRandomWebPage();
super.initState();
}
@override
Widget build(BuildContext context) {
final bottomNavigationBar = BottomNavigationWidget(
isReaderMode: isReaderModeView,
onTap: toggleReaderMode,
);
final floatingActionButton = FloatingActionButton(
onPressed: loadRandomWebPage,
child: const Icon(Icons.autorenew),
);
final topNavigation = TopNavigationWidget(
readerModeController: readerModeController,
textEditingController: textEditingController,
);
final body = isReaderModeView
? buildReaderMode()
: WebViewPage(
key: ValueKey(readerModeController.uri),
uri: readerModeController.uri);
return SafeArea(
child: Scaffold(
appBar: PreferredSize(
child: topNavigation,
preferredSize: const Size(double.infinity, 80.0),
),
bottomNavigationBar: bottomNavigationBar,
floatingActionButton: floatingActionButton,
body: body,
),
);
}
toggleReaderMode(_) => setState(() => isReaderModeView = !isReaderModeView);
buildReaderMode() {
const userAgent =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36';
const classesToPreserve = [
"caption",
"emoji",
"hidden",
"invisible",
"sr-only",
"visually-hidden",
"visuallyhidden",
"wp-caption",
"wp-caption-text",
"wp-smiley"
];
const loadingWidget = Center(child: CircularProgressIndicator());
final readerMode = ReaderMode(
userAgent: userAgent,
classesToPreserve: classesToPreserve,
onImageTap: (metadata) {
log(metadata?.sources.join(', ') ?? metadata?.title ?? 'image');
},
controller: readerModeController,
loadingBuilder: () => loadingWidget,
);
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: readerMode,
);
}
loadRandomWebPage() => readerModeController.loadUri(getRandomArticleUri());
}