Skip to content

Commit edbc466

Browse files
shinyfordslightfoot
authored andcommitted
fix: refactoring of user button [CLERK_SDK #20]
1 parent 029ef16 commit edbc466

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

packages/clerk_flutter/example/lib/main.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MainApp extends StatelessWidget {
2727
padding: horizontalPadding32,
2828
child: Center(
2929
child: ClerkAuthBuilder(
30-
signedInBuilder: (context, auth) => const ClerkUserButton(),
30+
signedInBuilder: (context, auth) => ClerkUserButton(),
3131
signedOutBuilder: (context, auth) => const ClerkAuthenticationWidget(),
3232
),
3333
),

packages/clerk_flutter/lib/src/widgets/control/clerk_auth.dart

+8-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ class ClerkAuth extends StatefulWidget {
3838
this.translator = const DefaultClerkTranslator(),
3939
this.persistor,
4040
this.loading,
41-
}) : assert((publicKey is String && publishableKey is String) != auth is ClerkAuthProvider);
41+
}) : assert(
42+
(publicKey is String) != (auth is ClerkAuthProvider),
43+
'Either publicKey or an auth instance must be provided, but not both',
44+
),
45+
assert(
46+
(publicKey is String) == (publishableKey is String),
47+
'Both publicKey and publishableKey must be provided, or neither',
48+
);
4249

4350
@override
4451
State<ClerkAuth> createState() => _ClerkAuthState();

packages/clerk_flutter/lib/src/widgets/user/clerk_user_button.dart

+14-15
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@ import 'package:clerk_flutter/clerk_flutter.dart';
55
import 'package:common/common.dart';
66
import 'package:flutter/material.dart';
77

8-
class ClerkUserButton extends StatefulWidget {
9-
final bool showName;
10-
11-
const ClerkUserButton({super.key, this.showName = true});
8+
class ClerkUserButton extends StatelessWidget {
9+
static const _closeDelay = Duration(milliseconds: 250);
1210

13-
@override
14-
State<ClerkUserButton> createState() => _ClerkUserButtonState();
15-
}
11+
final bool showName;
1612

17-
class _ClerkUserButtonState extends State<ClerkUserButton> {
18-
static const _closeDelay = Duration(milliseconds: 250);
13+
ClerkUserButton({super.key, this.showName = true});
1914

20-
List<Clerk.Session> _sessions = [];
15+
final _sessions = <Clerk.Session>[];
2116

2217
@override
2318
Widget build(BuildContext context) {
@@ -32,7 +27,7 @@ class _ClerkUserButtonState extends State<ClerkUserButton> {
3227
final sessions = auth.client.sessions;
3328

3429
// adding to a list of existing sessions means we have ones that are now deleted
35-
// available for prettier UI
30+
// still available in `_sessions`, making for prettier UI
3631
_sessions.addOrReplaceAll(sessions, by: (s) => s.id);
3732

3833
// after a delay period, all deleted sessions will have been closed, so we can
@@ -55,7 +50,7 @@ class _ClerkUserButtonState extends State<ClerkUserButton> {
5550
session: session,
5651
closed: auth.client.sessions.contains(session) == false,
5752
selected: session == auth.client.activeSession,
58-
showName: widget.showName,
53+
showName: showName,
5954
onTap: () => auth.call(context, () => auth.setActiveSession(session)),
6055
),
6156
if (auth.env.config.singleSessionMode == false)
@@ -71,6 +66,7 @@ class _ClerkUserButtonState extends State<ClerkUserButton> {
7166
icon: Icons.add,
7267
backgroundColor: ClerkColors.dawnPink,
7368
borderColor: ClerkColors.nobel,
69+
dashed: true,
7470
),
7571
horizontalMargin24,
7672
Text(
@@ -163,13 +159,15 @@ class _CircleIcon extends StatelessWidget {
163159
final Color color;
164160
final Color backgroundColor;
165161
final Color? borderColor;
162+
final bool dashed;
166163

167164
const _CircleIcon({
168165
super.key,
169166
required this.icon,
170167
this.color = ClerkColors.stormGrey,
171168
this.backgroundColor = Colors.transparent,
172169
this.borderColor,
170+
this.dashed = false,
173171
});
174172

175173
@override
@@ -181,7 +179,7 @@ class _CircleIcon extends StatelessWidget {
181179
color: borderColor ?? color,
182180
backgroundColor: backgroundColor,
183181
dashLength: 2,
184-
gapLength: 2,
182+
gapLength: dashed ? 2 : 0,
185183
),
186184
child: Icon(icon, size: 16, color: color),
187185
),
@@ -290,8 +288,9 @@ class _SessionRow extends StatelessWidget {
290288
if (showName)
291289
Text(
292290
user.name,
293-
style:
294-
ClerkTextStyle.buttonTitle.copyWith(color: ClerkColors.almostBlack),
291+
style: ClerkTextStyle.buttonTitle.copyWith(
292+
color: ClerkColors.almostBlack,
293+
),
295294
),
296295
if (user.email is String)
297296
Text(user.email!, style: ClerkTextStyle.buttonTitle),

packages/common/lib/src/extensions.dart

+6-11
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,14 @@ extension StringExtension on String {
3232
}
3333

3434
extension ListExtension on List {
35-
void addOrReplaceAll<T>(Iterable<T> list, {dynamic Function(T)? by}) {
36-
if (isEmpty) {
37-
addAll(list);
38-
} else {
39-
by ??= (t) => t;
40-
for (final item in list) {
41-
final identifier = by.call(item);
42-
final idx = indexWhere((i) => by!.call(i) == identifier);
43-
if (idx > -1) {
35+
void addOrReplaceAll<T, S>(Iterable<T> list, {S Function(T)? by}) {
36+
for (final item in list) {
37+
final identifier = by?.call(item) ?? item;
38+
switch (indexWhere((i) => (by?.call(i) ?? i) == identifier)) {
39+
case int idx when idx > -1:
4440
this[idx] = item;
45-
} else {
41+
default:
4642
add(item);
47-
}
4843
}
4944
}
5045
}

0 commit comments

Comments
 (0)