-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
tier_list_page.dart
152 lines (143 loc) · 5.71 KB
/
tier_list_page.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
144
145
146
147
148
149
150
151
152
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:screenshot/screenshot.dart';
import 'package:shiori/application/bloc.dart';
import 'package:shiori/domain/extensions/iterable_extensions.dart';
import 'package:shiori/generated/l10n.dart';
import 'package:shiori/injection.dart';
import 'package:shiori/presentation/shared/styles.dart';
import 'package:shiori/presentation/shared/utils/toast_utils.dart';
import 'package:shiori/presentation/tierlist/widgets/tierlist_fab.dart';
import 'package:shiori/presentation/tierlist/widgets/tierlist_row.dart';
class TierListPage extends StatefulWidget {
@override
_TierListPageState createState() => _TierListPageState();
}
class _TierListPageState extends State<TierListPage> {
final screenshotController = ScreenshotController();
@override
Widget build(BuildContext context) {
const double fabHeight = 100;
return BlocProvider<TierListBloc>(
create: (ctx) => Injection.tierListBloc..add(const TierListEvent.init()),
child: Scaffold(
appBar: _AppBar(screenshotController: screenshotController),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: const TierListFab(height: fabHeight),
body: SafeArea(
child: SingleChildScrollView(
child: Screenshot(
controller: screenshotController,
child: BlocBuilder<TierListBloc, TierListState>(
builder: (ctx, state) => Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
...state.rows.mapIndex(
(e, index) => TierListRow(
index: index,
title: e.tierText,
color: Color(e.tierColor),
items: e.items,
isUpButtonEnabled: index != 0,
isDownButtonEnabled: index != state.rows.length - 1,
numberOfRows: state.rows.length,
showButtons: !state.readyToSave,
isTheLastRow: state.rows.length == 1,
),
),
if (!state.readyToSave && state.charsAvailable.isNotEmpty)
SizedBox.fromSize(
size: const Size.fromHeight(fabHeight),
)
],
),
),
),
),
),
),
);
}
}
class _AppBar extends StatelessWidget implements PreferredSizeWidget {
final ScreenshotController screenshotController;
const _AppBar({required this.screenshotController});
@override
Widget build(BuildContext context) {
final s = S.of(context);
return BlocBuilder<TierListBloc, TierListState>(
builder: (ctx, state) => AppBar(
title: Text(s.tierListBuilder),
actions: [
if (!state.readyToSave)
Tooltip(
message: s.confirm,
child: IconButton(
splashRadius: Styles.mediumButtonSplashRadius,
icon: const Icon(Icons.check),
onPressed: () => ctx.read<TierListBloc>().add(const TierListEvent.readyToSave(ready: true)),
),
),
if (!state.readyToSave)
Tooltip(
message: s.clearAll,
child: IconButton(
splashRadius: Styles.mediumButtonSplashRadius,
icon: const Icon(Icons.clear_all),
onPressed: () => context.read<TierListBloc>().add(const TierListEvent.clearAllRows()),
),
),
if (!state.readyToSave)
Tooltip(
message: s.restore,
child: IconButton(
splashRadius: Styles.mediumButtonSplashRadius,
icon: const Icon(Icons.settings_backup_restore_sharp),
onPressed: () => context.read<TierListBloc>().add(const TierListEvent.init(reset: true)),
),
),
if (state.readyToSave)
Tooltip(
message: s.save,
child: IconButton(
splashRadius: Styles.mediumButtonSplashRadius,
icon: const Icon(Icons.save_alt),
onPressed: () => _takeScreenshot(context),
),
),
if (state.readyToSave)
Tooltip(
message: s.cancel,
child: IconButton(
splashRadius: Styles.mediumButtonSplashRadius,
icon: const Icon(Icons.undo),
onPressed: () => context.read<TierListBloc>().add(const TierListEvent.readyToSave(ready: false)),
),
),
],
),
);
}
Future<void> _takeScreenshot(BuildContext context) async {
final s = S.of(context);
final fToast = ToastUtils.of(context);
final bloc = context.read<TierListBloc>();
try {
if (!await Permission.storage.request().isGranted) {
ToastUtils.showInfoToast(fToast, s.acceptToSaveImg);
return;
}
final bytes = await screenshotController.capture(pixelRatio: 1.5);
await ImageGallerySaver.saveImage(bytes!, quality: 100);
ToastUtils.showSucceedToast(fToast, s.imgSavedSuccessfully);
bloc.add(const TierListEvent.screenshotTaken(succeed: true));
} catch (e, trace) {
ToastUtils.showErrorToast(fToast, s.unknownError);
bloc.add(TierListEvent.screenshotTaken(succeed: false, ex: e, trace: trace));
}
}
@override
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}