-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonboarding_accept_document_screen.dart
167 lines (147 loc) · 4.32 KB
/
onboarding_accept_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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import 'dart:developer' as developer;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:widgetbook/widgetbook.dart';
import 'package:widgetbook_annotation/widgetbook_annotation.dart' as widgetbook;
import '../../di.dart';
import '../../strings_context.dart';
import '../../use_case/get_html_document_version_use_case.dart';
import '../../util/errors.dart';
import '../app_theme.dart';
import '../fragment/show_web_page_fragment.dart';
import '../onboarding.dart';
import '../widgets/loading_indicator.dart';
import '../widgets/step_indicator.dart';
import 'show_document_screen.dart';
/// [Onboarding] screen to accept document - Privacy Policy or Terms of Service.
///
/// Uses [GetHtmlDocumentVersionUseCase].
///
/// See also:
/// - [ShowDocumentScreen]
class OnboardingAcceptDocumentScreen extends StatefulWidget {
final String title;
final Uri url;
final int step;
final AsyncValueSetter<String> versionSetter;
final void Function(BuildContext context) onAccepted;
const OnboardingAcceptDocumentScreen({
super.key,
required this.title,
required this.url,
required this.step,
required this.versionSetter,
required this.onAccepted,
});
@override
State<OnboardingAcceptDocumentScreen> createState() =>
_OnboardingAcceptDocumentScreenState();
}
class _OnboardingAcceptDocumentScreenState
extends State<OnboardingAcceptDocumentScreen> {
bool documentLoaded = false;
bool documentVersionIsLoading = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: const BackButton(),
title: Text(widget.title),
),
body: SafeArea(
child: _getBody(),
),
);
}
Widget _getBody() {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: ShowWebPageFragment(
url: widget.url,
onUrlLoaded: () {
if (mounted) {
setState(() {
documentLoaded = true;
});
}
},
),
),
// Steps
Padding(
padding: const EdgeInsets.only(top: 8),
child: StepIndicator(stepNumber: widget.step, totalSteps: 3),
),
// Primary button
Padding(
padding: kScreenMargin,
child: FilledButton(
style: FilledButton.styleFrom(
minimumSize: kPrimaryButtonMinimumSize,
),
onPressed:
documentLoaded && !documentVersionIsLoading ? _onAccept : null,
child: !documentLoaded || documentVersionIsLoading
? const LoadingIndicator()
: Text(context.strings.buttonAgreeLabel),
),
),
],
);
}
void _onAccept() async {
final getDocumentVersion = getIt.get<GetHtmlDocumentVersionUseCase>();
try {
setState(() {
documentVersionIsLoading = true;
});
final documentVersion = await getDocumentVersion(widget.url);
await widget.versionSetter(documentVersion);
if (mounted) {
widget.onAccepted.call(context);
}
} catch (error) {
_showError(error);
setState(() {
documentVersionIsLoading = false;
});
}
}
void _showError(Object error) {
final snackBar = SnackBar(
content: Text(getErrorMessage(error)),
);
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(snackBar);
}
}
@widgetbook.UseCase(
path: '[Screens]',
name: '',
type: OnboardingAcceptDocumentScreen,
)
Widget previewOnboardingAcceptDocumentScreen(BuildContext context) {
final strings = context.strings;
final title = context.knobs.string(
label: "Title",
initialValue: strings.privacyPolicyTitle,
);
final url = context.knobs.string(
label: "URL",
initialValue: strings.privacyPolicyUrl,
);
return OnboardingAcceptDocumentScreen(
title: title,
url: Uri.parse(url),
step: 1,
versionSetter: (version) async {
developer.log("$version accepted");
},
onAccepted: (_) {
developer.log("onAccepted");
},
);
}