-
Notifications
You must be signed in to change notification settings - Fork 1
/
languages_table.dart
143 lines (135 loc) · 5.1 KB
/
languages_table.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import 'package:app4training/l10n/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:app4training/data/globals.dart';
import 'package:app4training/data/languages.dart';
import 'package:app4training/data/updates.dart';
import 'package:app4training/widgets/delete_language_button.dart';
import 'package:app4training/widgets/download_language_button.dart';
import 'package:app4training/widgets/update_language_button.dart';
/// For the settings page: Show table with list of available languages.
/// Features:
/// - select which of them should be available offline
/// - delete language files
/// - download language files (if not yet downloaded)
/// - update language if updates are available
/// - show disk usage at the end
class LanguagesTable extends ConsumerWidget {
/// Optional: highlight the download button of this language
final String? highlightLang;
const LanguagesTable({this.highlightLang, super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
int sizeInKB = ref.watch(diskUsageProvider);
String countLanguages = context.l10n
.countLanguages(ref.watch(countDownloadedLanguagesProvider));
List<TableRow> rows = [];
bool allDownloaded = true; // are all languages downloaded?
// Sort languages alphabetically
List<String> sortedLanguages =
List.from(ref.read(availableLanguagesProvider));
sortedLanguages.sort((a, b) => context.l10n
.getLanguageName(a)
.compareTo(context.l10n.getLanguageName(b)));
// Add a table row for each language
for (var languageCode in sortedLanguages) {
// watch this to rebuild if a Language object gets renewed
Language lang = ref.watch(languageProvider(languageCode));
allDownloaded = allDownloaded && lang.downloaded;
LanguageStatus status = ref.watch(languageStatusProvider(languageCode));
rows.add(TableRow(children: [
SizedBox(height: 32, width: 32, child: IsDownloaded(lang.downloaded)),
Container(
height: 32,
alignment: Alignment.centerLeft,
child: Text(context.l10n.getLanguageName(languageCode),
style: Theme.of(context).textTheme.bodyMedium)),
SizedBox(
height: 32,
width: 32,
child: status.updatesAvailable
? UpdateLanguageButton(languageCode)
: const Text("")),
SizedBox(
height: 32,
width: 32,
child: lang.downloaded
? DeleteLanguageButton(languageCode)
: DownloadLanguageButton(languageCode,
highlight: languageCode == highlightLang)),
]));
}
return Column(
children: [
Table(
columnWidths: const {
0: IntrinsicColumnWidth(),
1: FlexColumnWidth(),
2: IntrinsicColumnWidth(),
3: IntrinsicColumnWidth(),
4: IntrinsicColumnWidth(),
},
children: [
// header with all-languages-buttons
TableRow(
decoration: const BoxDecoration(
border: Border(
bottom: BorderSide(width: 3, color: Colors.grey))),
children: [
SizedBox(
height: 32,
width: 32,
child: IsDownloaded(allDownloaded)),
Container(
height: 32,
alignment: Alignment.centerLeft,
child: Text(
'${context.l10n.allLanguages} ($countAvailableLanguages)',
style: const TextStyle(fontWeight: FontWeight.bold))),
const SizedBox(
height: 32, width: 32, child: UpdateAllLanguagesButton()),
const SizedBox(
height: 32,
width: 32,
child: DownloadAllLanguagesButton()),
const SizedBox(
height: 32, width: 32, child: DeleteAllLanguagesButton()),
])
],
),
const SizedBox(height: 5),
Expanded(
child: SingleChildScrollView(
child: Table(
//border: TableBorder.all(color: Colors.black26),
columnWidths: const {
0: IntrinsicColumnWidth(),
1: FlexColumnWidth(),
2: IntrinsicColumnWidth(),
3: IntrinsicColumnWidth(),
},
children: rows,
))),
const SizedBox(height: 5),
Text(
'${context.l10n.diskUsage}: $sizeInKB kB $countLanguages',
style: Theme.of(context).textTheme.bodyMedium,
),
],
);
}
}
/// Displays a ✓ (check) mark or a blank
class IsDownloaded extends StatelessWidget {
final bool downloaded;
const IsDownloaded(
this.downloaded, {
super.key,
});
@override
Widget build(BuildContext context) {
return downloaded
? const Padding(padding: EdgeInsets.all(4), child: Icon(Icons.check))
: const Text("");
}
}