-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_document_screen.dart
118 lines (103 loc) · 3.12 KB
/
open_document_screen.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
import 'dart:async';
import 'dart:io' show File;
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
import 'package:widgetbook_annotation/widgetbook_annotation.dart' as widgetbook;
import '../../bloc/create_document_cubit.dart';
import '../../data/settings.dart';
import '../../strings_context.dart';
import '../app_theme.dart';
import '../widgets/error_content.dart';
import '../widgets/loading_content.dart';
import 'preview_document_screen.dart';
/// Screen for opening single document from [file].
///
/// Uses [CreateDocumentCubit].
///
/// Navigates next to [PreviewDocumentScreen] on success.
class OpenDocumentScreen extends StatelessWidget {
final FutureOr<File> file;
const OpenDocumentScreen({super.key, required this.file});
@override
Widget build(BuildContext context) {
final body = BlocProvider<CreateDocumentCubit>(
create: (context) {
final settings = context.read<Settings>();
final pdfSigningOption = settings.signingPdfContainer.value;
return GetIt.instance.get<CreateDocumentCubit>(
param1: file,
param2: pdfSigningOption,
)..createDocument();
},
child: BlocConsumer<CreateDocumentCubit, CreateDocumentState>(
listener: (context, state) {
// On success move to next screen replacing this
if (state is CreateDocumentSuccessState) {
_onSuccess(context, state);
}
},
builder: (context, state) {
return _Body(state: state);
},
),
);
return Scaffold(
appBar: AppBar(
title: Text(context.strings.openDocumentTitle),
),
body: SafeArea(
child: body,
),
);
}
void _onSuccess(BuildContext context, CreateDocumentSuccessState state) {
final screen = PreviewDocumentScreen(
file: state.file,
documentId: state.documentId,
);
final route = MaterialPageRoute(
builder: (context) => screen,
);
Navigator.of(context).pushReplacement(route);
}
}
/// [OpenDocumentScreen] body.
class _Body extends StatelessWidget {
final CreateDocumentState state;
const _Body({required this.state});
@override
Widget build(BuildContext context) {
final child = switch (state) {
CreateDocumentErrorState state => ErrorContent(
title: context.strings.openDocumentErrorTitle,
error: state.error,
),
_ => const LoadingContent(),
};
return Padding(
padding: kScreenMargin,
child: child,
);
}
}
@widgetbook.UseCase(
path: '[Screens]',
name: 'loading',
type: OpenDocumentScreen,
)
Widget previewLoadingOpenDocumentScreen(BuildContext context) {
return const _Body(
state: CreateDocumentLoadingState(),
);
}
@widgetbook.UseCase(
path: '[Screens]',
name: 'error',
type: OpenDocumentScreen,
)
Widget previewErrorOpenDocumentScreen(BuildContext context) {
return const _Body(
state: CreateDocumentErrorState("Error message!"),
);
}