|
| 1 | +# Customizing the UI |
| 2 | + |
| 3 | +When using the anonymous identity provider, you can customize the UI to your liking. You can use the `AnonymousSignInWidget` to display the anonymous sign-in button in your own layout, or you can use the `AnonymousAuthController` to build a completely custom authentication interface. |
| 4 | + |
| 5 | +:::info |
| 6 | +The `SignInWidget` uses the `AnonymousSignInWidget` internally when the anonymous provider is enabled. You can supply a custom `AnonymousSignInWidget` to the `SignInWidget` to override the default (e.g. to pass `createAnonymousToken` or change size and shape). |
| 7 | + |
| 8 | +```dart |
| 9 | +SignInWidget( |
| 10 | + client: client, |
| 11 | + anonymousSignInWidget: AnonymousSignInWidget( |
| 12 | + client: client, |
| 13 | + createAnonymousToken: () async => await getAppCheckToken(), |
| 14 | + size: AnonymousButtonSize.medium, |
| 15 | + shape: AnonymousButtonShape.rectangular, |
| 16 | + ), |
| 17 | +) |
| 18 | +``` |
| 19 | +::: |
| 20 | + |
| 21 | +## Using the `AnonymousSignInWidget` |
| 22 | + |
| 23 | +The `AnonymousSignInWidget` displays a single "Continue without account" button that starts the anonymous sign-in flow when pressed. You can customize the widget's behavior and appearance using its constructor parameters: |
| 24 | + |
| 25 | +```dart |
| 26 | +import 'package:serverpod_auth_idp_flutter/serverpod_auth_idp_flutter.dart'; |
| 27 | +
|
| 28 | +AnonymousSignInWidget( |
| 29 | + client: client, |
| 30 | + createAnonymousToken: () async { |
| 31 | + // Optional: provide a token for app attestation (e.g. Firebase App Check) |
| 32 | + return await getAppCheckToken(); |
| 33 | + }, |
| 34 | + onAuthenticated: () { |
| 35 | + // Do something when the user is authenticated. |
| 36 | + // |
| 37 | + // NOTE: You should not navigate to the home screen here, otherwise |
| 38 | + // the user will have to sign in again every time they open the app. |
| 39 | + }, |
| 40 | + onError: (error) { |
| 41 | + // Handle errors |
| 42 | + }, |
| 43 | + size: AnonymousButtonSize.large, // large (default), medium, or small |
| 44 | + shape: AnonymousButtonShape.pill, // pill (default) or rectangular |
| 45 | +) |
| 46 | +``` |
| 47 | + |
| 48 | +Optionally, you can provide an externally managed `AnonymousAuthController` instance to the widget. When a controller is provided, `client`, `onAuthenticated`, and `onError` are ignored in favor of the controller's configuration. |
| 49 | + |
| 50 | +```dart |
| 51 | +AnonymousSignInWidget( |
| 52 | + controller: controller, |
| 53 | + size: AnonymousButtonSize.medium, |
| 54 | + shape: AnonymousButtonShape.rectangular, |
| 55 | +) |
| 56 | +``` |
| 57 | + |
| 58 | +### Customizing the button appearance |
| 59 | + |
| 60 | +The widget renders a single **TextButton** with the label "Continue without account". The button uses Flutter's material design system, so it reacts to your app's `Theme`. You can wrap the widget in a `Theme` (or `ThemeData`) to change colors and typography: |
| 61 | + |
| 62 | +```dart |
| 63 | +Theme( |
| 64 | + data: Theme.of(context).copyWith( |
| 65 | + colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue), |
| 66 | + textButtonTheme: TextButtonThemeData( |
| 67 | + style: TextButton.styleFrom( |
| 68 | + foregroundColor: Colors.blue, |
| 69 | + ), |
| 70 | + ), |
| 71 | + ), |
| 72 | + child: AnonymousSignInWidget(client: client), |
| 73 | +) |
| 74 | +``` |
| 75 | + |
| 76 | +The widget constrains the button to a minimum width of 240 and maximum width of 400; you can place it in a `SizedBox`, `Expanded`, or `Flex` to control layout. |
| 77 | + |
| 78 | +## Building a custom UI with the `AnonymousAuthController` |
| 79 | + |
| 80 | +For full control over the UI, use the `AnonymousAuthController` class. It provides the anonymous sign-in logic without any built-in widget, so you can trigger login from your own button or flow and build a completely custom layout. |
| 81 | + |
| 82 | +```dart |
| 83 | +import 'package:serverpod_auth_idp_flutter/serverpod_auth_idp_flutter.dart'; |
| 84 | +
|
| 85 | +final controller = AnonymousAuthController( |
| 86 | + client: client, |
| 87 | + createAnonymousToken: () async => await getAppCheckToken(), |
| 88 | + onAuthenticated: () { |
| 89 | + // Do something when the user is authenticated. |
| 90 | + }, |
| 91 | + onError: (error) { |
| 92 | + // Handle errors |
| 93 | + }, |
| 94 | +); |
| 95 | +``` |
| 96 | + |
| 97 | +### AnonymousAuthController state management |
| 98 | + |
| 99 | +The controller notifies listeners when its state changes. Use these properties to drive your UI: |
| 100 | + |
| 101 | +```dart |
| 102 | +// Check if a request is in progress |
| 103 | +final isLoading = controller.isLoading; |
| 104 | +
|
| 105 | +// Check current state (idle, loading, error, authenticated) |
| 106 | +final state = controller.state; |
| 107 | +
|
| 108 | +// Listen to state changes |
| 109 | +controller.addListener(() { |
| 110 | + setState(() { |
| 111 | + // Rebuild when controller state changes |
| 112 | + }); |
| 113 | +}); |
| 114 | +``` |
| 115 | + |
| 116 | +### AnonymousAuthController methods |
| 117 | + |
| 118 | +The controller exposes a single action for anonymous sign-in: |
| 119 | + |
| 120 | +```dart |
| 121 | +// Start anonymous sign-in. |
| 122 | +// Obtains token if `createAnonymousToken` is set, then calls the login endpoint. |
| 123 | +await controller.login(); |
| 124 | +``` |
| 125 | + |
| 126 | +Call `controller.login()` from your custom button's `onPressed`, or from any other trigger (e.g. after a delay or when the user performs an action). The controller handles loading state, success, and errors and invokes `onAuthenticated` or `onError` as appropriate. |
| 127 | + |
| 128 | +:::tip |
| 129 | +Remember to dispose the controller when it is no longer needed (e.g. in your widget's `dispose`), unless the widget manages its own controller and disposes it for you. |
| 130 | +::: |
0 commit comments