|
| 1 | +# Address Sheet Implementation Guide |
| 2 | + |
| 3 | +The Address Sheet is a pre-built UI component that allows you to collect shipping or billing addresses from your users in a Flutter application using Stripe's native UI components. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Make sure you have the `flutter_stripe` package installed in your `pubspec.yaml`: |
| 8 | + |
| 9 | +```yaml |
| 10 | +dependencies: |
| 11 | + flutter_stripe: ^latest_version |
| 12 | +``` |
| 13 | +
|
| 14 | +### Set up Stripe [Server Side] [Client Side] |
| 15 | +
|
| 16 | +First, you need a Stripe account. [Register now](https://dashboard.stripe.com/register). |
| 17 | +
|
| 18 | +#### Server-side |
| 19 | +
|
| 20 | +This integration requires endpoints on your server that talk to the Stripe API. Use one official libraries for access to the Stripe API from your server. [Follow the steps here](https://stripe.com/docs/payments/accept-a-payment?platform=ios&ui=payment-sheet#setup-server-side) |
| 21 | +
|
| 22 | +#### Client-side |
| 23 | +
|
| 24 | +The Flutter SDK is open source, fully documented. |
| 25 | +
|
| 26 | +To install the SDK, follow these steps: |
| 27 | + - Run the commmand `flutter pub add flutter_stripe` |
| 28 | + - This will add a line like this to your project's pubspec.yaml with the latest package version |
| 29 | + |
| 30 | + |
| 31 | +For details on the latest SDK release and past versions, see the [Releases](https://github.com/flutter-stripe/flutter_stripe/releases) page on GitHub. To receive notifications when a new release is published, [watch releases for the repository](https://docs.github.com/en/github/managing-subscriptions-and-notifications-on-github/managing-subscriptions-for-activity-on-github/viewing-your-subscriptions#watching-releases-for-a-repository). |
| 32 | + |
| 33 | + |
| 34 | +When your app starts, configure the SDK with your Stripe [publishable key](https://dashboard.stripe.com/) so that it can make requests to the Stripe API. |
| 35 | + |
| 36 | +```dart |
| 37 | +void main() async { |
| 38 | + Stripe.publishableKey = stripePublishableKey; |
| 39 | + runApp(const App()); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +Use your [test mode](https://stripe.com/docs/keys#obtain-api-keys) keys while you test and develop, and your [live mode](https://stripe.com/docs/keys#test-live-modes) keys when you publish your app. |
| 44 | + |
| 45 | + |
| 46 | +### Basic Implementation |
| 47 | + |
| 48 | +Here's a basic example of how to implement the Address Sheet: |
| 49 | + |
| 50 | +```dart |
| 51 | +import 'package:flutter/material.dart'; |
| 52 | +import 'package:flutter_stripe/flutter_stripe.dart'; |
| 53 | +
|
| 54 | +AddressSheet( |
| 55 | + onSubmit: (details) { |
| 56 | + // Handle the submitted address details |
| 57 | + print(details.toJson()); |
| 58 | + }, |
| 59 | + onError: (error) { |
| 60 | + // Handle any errors that occur |
| 61 | + print(error.error.localizedMessage); |
| 62 | + }, |
| 63 | + params: AddressSheetParams(), |
| 64 | +) |
| 65 | +``` |
| 66 | + |
| 67 | +## Parameters |
| 68 | + |
| 69 | +### Required Parameters |
| 70 | + |
| 71 | +- `onSubmit`: Callback function that is called when the user successfully submits their address information. Receives a `CollectAddressResult` object containing: |
| 72 | + - `address`: The collected address details |
| 73 | + - `name`: The customer's name |
| 74 | + - `phoneNumber`: The customer's phone number (optional) |
| 75 | + |
| 76 | +- `onError`: Callback function that is called when an error occurs or when the user closes the sheet before submitting. Receives a `StripeException` object. |
| 77 | + |
| 78 | +- `params`: An `AddressSheetParams` object that configures the address sheet behavior and appearance. |
| 79 | + |
| 80 | +### Address Result Structure |
| 81 | + |
| 82 | +The `CollectAddressResult` object contains the following information: |
| 83 | + |
| 84 | +```dart |
| 85 | +class CollectAddressResult { |
| 86 | + final Address address; |
| 87 | + final String name; |
| 88 | + final String? phoneNumber; |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +The `Address` object contains standard address fields like street, city, state, postal code, and country. |
| 93 | + |
| 94 | +## Example Implementation |
| 95 | + |
| 96 | +Here's a complete example showing how to implement the Address Sheet in a Flutter screen: |
| 97 | + |
| 98 | +```dart |
| 99 | +import 'package:flutter/material.dart'; |
| 100 | +import 'package:flutter_stripe/flutter_stripe.dart'; |
| 101 | +
|
| 102 | +class AddressSheetExample extends StatefulWidget { |
| 103 | + const AddressSheetExample({super.key}); |
| 104 | +
|
| 105 | + @override |
| 106 | + State<AddressSheetExample> createState() => _AddressSheetExampleState(); |
| 107 | +} |
| 108 | +
|
| 109 | +class _AddressSheetExampleState extends State<AddressSheetExample> { |
| 110 | + String? result; |
| 111 | +
|
| 112 | + @override |
| 113 | + Widget build(BuildContext context) { |
| 114 | + return Scaffold( |
| 115 | + appBar: AppBar( |
| 116 | + title: Text('Address Sheet Example'), |
| 117 | + ), |
| 118 | + body: Padding( |
| 119 | + padding: EdgeInsets.symmetric(horizontal: 16), |
| 120 | + child: Column( |
| 121 | + children: [ |
| 122 | + AddressSheet( |
| 123 | + onError: (error) { |
| 124 | + setState(() { |
| 125 | + result = error.error.localizedMessage; |
| 126 | + }); |
| 127 | + }, |
| 128 | + onSubmit: (details) { |
| 129 | + setState(() { |
| 130 | + result = details.toJson().toString(); |
| 131 | + }); |
| 132 | + }, |
| 133 | + params: AddressSheetParams(), |
| 134 | + ), |
| 135 | + SizedBox(height: 20), |
| 136 | + Text(result ?? ''), |
| 137 | + ], |
| 138 | + ), |
| 139 | + ), |
| 140 | + ); |
| 141 | + } |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +### Customization |
| 146 | + |
| 147 | +You can customize the Address Sheet behavior by configuring the `AddressSheetParams`. This allows you to: |
| 148 | +- Set default values |
| 149 | +- Configure which fields are required |
| 150 | +- Customize the appearance |
| 151 | +- Set specific country restrictions |
| 152 | + |
| 153 | +## Platform Support |
| 154 | + |
| 155 | +The Address Sheet is supported on both iOS and Android platforms, providing a native UI experience on each platform. |
| 156 | + |
| 157 | +## Best Practices |
| 158 | + |
| 159 | +1. Always handle both the `onSubmit` and `onError` callbacks to ensure a good user experience. |
| 160 | +2. Validate the collected address information before using it in your application. |
| 161 | +3. Consider implementing proper error handling and display appropriate error messages to users. |
| 162 | +4. Store the collected address information securely if you need to reuse it later. |
| 163 | + |
| 164 | +## Related Resources |
| 165 | + |
| 166 | +- [Stripe Documentation](https://stripe.com/docs) |
| 167 | +- [Flutter Stripe Package](https://pub.dev/packages/flutter_stripe) |
0 commit comments