Skip to content

Commit 326fd27

Browse files
rstiegerlesnitsky
andauthored
feat(ui_auth): Add "Go Back" button to EmailLinkSignInView (#63)
* Add "Go Back" button to EmailLinkSignInView * Protect against going back if this is the root view * Only check Navigator.canPop() on state init * Add unit tests * Add license header and fix formatting * Update packages/firebase_ui_auth/test/views/email_link_sign_in_view_test.dart * fix formatting --------- Co-authored-by: Andrei Lesnitsky <[email protected]>
1 parent 7cb9925 commit 326fd27

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

Diff for: packages/firebase_ui_auth/lib/src/views/email_link_sign_in_view.dart

+12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class EmailLinkSignInView extends StatefulWidget {
3939

4040
class _EmailLinkSignInViewState extends State<EmailLinkSignInView> {
4141
final emailCtrl = TextEditingController();
42+
late final canPop = Navigator.canPop(context);
43+
4244
@override
4345
Widget build(BuildContext context) {
4446
final l = FirebaseUILocalizations.labelsOf(context);
@@ -82,6 +84,16 @@ class _EmailLinkSignInViewState extends State<EmailLinkSignInView> {
8284
},
8385
),
8486
],
87+
if (canPop) ...[
88+
const SizedBox(height: 8),
89+
UniversalButton(
90+
text: l.goBackButtonLabel,
91+
variant: ButtonVariant.text,
92+
onPressed: () {
93+
Navigator.of(context).pop();
94+
},
95+
),
96+
],
8597
const SizedBox(height: 8),
8698
if (state is AuthFailed) ErrorText(exception: state.exception),
8799
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2023, the Chromium project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:firebase_auth/firebase_auth.dart';
6+
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
7+
import 'package:firebase_ui_localizations/firebase_ui_localizations.dart';
8+
import 'package:flutter/material.dart';
9+
import 'package:flutter_test/flutter_test.dart';
10+
11+
import '../test_utils.dart';
12+
13+
void main() {
14+
const labels = DefaultLocalizations();
15+
late MockAuth auth;
16+
late MockDynamicLinks dynamicLinks;
17+
late EmailLinkAuthProvider emailLinkProvider;
18+
19+
setUp(() {
20+
auth = MockAuth();
21+
dynamicLinks = MockDynamicLinks();
22+
final actionCodeSettings = ActionCodeSettings(
23+
url: 'https://example.com',
24+
);
25+
emailLinkProvider = EmailLinkAuthProvider(
26+
actionCodeSettings: actionCodeSettings,
27+
dynamicLinks: dynamicLinks,
28+
);
29+
});
30+
31+
/// If EmailLinkSignInView is the root view, there should be
32+
/// no option to go back.
33+
testWidgets('no go back option if root view', (tester) async {
34+
await tester.pumpWidget(TestMaterialApp(
35+
child: EmailLinkSignInView(
36+
provider: emailLinkProvider,
37+
auth: auth,
38+
),
39+
));
40+
41+
final button = find.text(labels.goBackButtonLabel);
42+
expect(button, findsNothing);
43+
});
44+
45+
/// If EmailLinkSignInView is pushed from another view, there
46+
/// should be a button allowing a user to go back.
47+
testWidgets('show go back option if not root', (tester) async {
48+
await tester.pumpWidget(
49+
TestMaterialApp(
50+
child: Builder(
51+
builder: (context) => TextButton(
52+
child: const Text("Push"),
53+
onPressed: () => Navigator.push(
54+
context,
55+
MaterialPageRoute(
56+
builder: (context) => Scaffold(
57+
body: EmailLinkSignInView(
58+
provider: emailLinkProvider, auth: auth),
59+
),
60+
),
61+
),
62+
),
63+
),
64+
),
65+
);
66+
67+
await tester.tap(find.textContaining("Push"));
68+
await tester.pumpAndSettle();
69+
final button = find.text(labels.goBackButtonLabel);
70+
expect(button, findsOneWidget);
71+
});
72+
}

0 commit comments

Comments
 (0)