-
-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Remon
committed
Dec 15, 2024
1 parent
fbfcddf
commit a6dd083
Showing
6 changed files
with
1,259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/gestures.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/rendering.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:flutter_stripe/flutter_stripe.dart'; | ||
|
||
class AddressSheet extends StatelessWidget { | ||
const AddressSheet({ | ||
required this.onSubmit, | ||
required this.onError, | ||
required this.params, | ||
this.height = 300, | ||
super.key, | ||
}); | ||
|
||
/// The height of the address sheet | ||
final int height; | ||
|
||
/// Called when the user submits their information | ||
final OnAddressSheetError onSubmit; | ||
|
||
/// Called when the user taps the button to close the sheet before submitting their information, or when an error occurs | ||
final OnAddressSheetError onError; | ||
|
||
/// The parameters for the address sheet | ||
final AddressSheetParams params; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return _AddressSheet( | ||
onSubmit: onSubmit, | ||
onError: onError, | ||
params: params, | ||
height: height, | ||
); | ||
} | ||
} | ||
|
||
class _AddressSheet extends StatefulWidget { | ||
const _AddressSheet({ | ||
required this.onSubmit, | ||
required this.onError, | ||
required this.height, | ||
required this.params, | ||
}); | ||
|
||
final AddressSheetParams params; | ||
final OnAddressSheetError onSubmit; | ||
final OnAddressSheetError onError; | ||
final int height; | ||
|
||
@override | ||
State<_AddressSheet> createState() => _AddressSheetState(); | ||
} | ||
|
||
class _AddressSheetState extends State<_AddressSheet> { | ||
static const _viewType = 'flutter.stripe/address_sheet'; | ||
MethodChannel? _methodChannel; | ||
|
||
void onPlatformViewCreated(int viewId) { | ||
_methodChannel = MethodChannel('flutter.stripe/address_sheet/$viewId'); | ||
_methodChannel?.setMethodCallHandler((call) async { | ||
if (call.method == 'onSubmit') { | ||
print('blaat details ${call.arguments}'); | ||
// widget.onSubmit.call(); | ||
} else if (call.method == 'onError') { | ||
print('blaat details ${call.arguments}'); | ||
} | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return SizedBox( | ||
height: widget.height.toDouble(), | ||
child: defaultTargetPlatform == TargetPlatform.iOS | ||
? UiKitView( | ||
viewType: _viewType, | ||
creationParams: const StandardMessageCodec(), | ||
) | ||
: PlatformViewLink( | ||
surfaceFactory: (context, controller) { | ||
return AndroidViewSurface( | ||
controller: controller as AndroidViewController, | ||
hitTestBehavior: PlatformViewHitTestBehavior.opaque, | ||
gestureRecognizers: const <Factory< | ||
OneSequenceGestureRecognizer>>{}, | ||
); | ||
}, | ||
onCreatePlatformView: (params) { | ||
onPlatformViewCreated(params.id); | ||
return PlatformViewsService.initExpensiveAndroidView( | ||
id: params.id, | ||
viewType: _viewType, | ||
layoutDirection: TextDirection.ltr, | ||
creationParams: {}, | ||
creationParamsCodec: const StandardMessageCodec(), | ||
) | ||
..addOnPlatformViewCreatedListener( | ||
params.onPlatformViewCreated) | ||
..create(); | ||
}, | ||
viewType: _viewType, | ||
), | ||
); | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
packages/stripe_platform_interface/lib/src/models/address_sheet.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:stripe_platform_interface/stripe_platform_interface.dart'; | ||
|
||
part 'address_sheet.freezed.dart'; | ||
part 'address_sheet.g.dart'; | ||
|
||
/// The style of how the address sheet should be presented | ||
enum AddressSheetPresentationStyle { | ||
/// Full screen | ||
fullscreen, | ||
|
||
/// Popover | ||
popover, | ||
|
||
/// Page sheet | ||
pageSheet, | ||
|
||
/// Form sheet | ||
formSheet, | ||
|
||
/// Automatic | ||
automatic, | ||
|
||
/// Over full screen | ||
overFullScreen, | ||
} | ||
|
||
/// How the address sheet should animate | ||
enum AddressSheetAnimationStyle { | ||
/// flip animation style | ||
flip, | ||
|
||
/// curl animation style | ||
curl, | ||
|
||
/// slide animation style | ||
slide, | ||
|
||
/// dissolve animation style | ||
dissolve, | ||
} | ||
|
||
@freezed | ||
class AddressSheetAdditionalFields with _$AddressSheetAdditionalFields { | ||
const factory AddressSheetAdditionalFields({ | ||
///Determines whether the phone number is hidden, required, or optional. Defaults to hidden | ||
required AddressSheetPhoneNumberField phoneNumber, | ||
|
||
/// The label of a checkbox displayed below other fields. If null or undefined, the checkbox is not displayed | ||
required String? checkboxLabel, | ||
}) = _AddressSheetAdditionalFields; | ||
|
||
factory AddressSheetAdditionalFields.fromJson(Map<String, dynamic> json) => | ||
_$AddressSheetAdditionalFieldsFromJson(json); | ||
} | ||
|
||
/// The style of how the phone number should be presented | ||
enum AddressSheetPhoneNumberField { | ||
/// The phone number is required | ||
required, | ||
|
||
/// The phone number is optional | ||
optional, | ||
|
||
/// The phone number is hidden | ||
hidden, | ||
} | ||
|
||
@freezed | ||
class CollectAddressResult with _$CollectAddressResult { | ||
const factory CollectAddressResult({ | ||
/// The customer's full name | ||
required String name, | ||
|
||
/// The customer's address | ||
required Address address, | ||
|
||
/// The customer's phone number | ||
String? phoneNumber, | ||
}) = _CollectAddressResult; | ||
|
||
factory CollectAddressResult.fromJson(Map<String, dynamic> json) => | ||
_$CollectAddressResultFromJson(json); | ||
} | ||
|
||
typedef OnAddressSheetSubmit = FutureOr<void> Function( | ||
CollectAddressResult result); | ||
|
||
typedef OnAddressSheetError = FutureOr<void> Function( | ||
StripeError<AddressSheetError> error); | ||
|
||
@freezed | ||
class AddressSheetParams with _$AddressSheetParams { | ||
@JsonSerializable(explicitToJson: true) | ||
const factory AddressSheetParams({ | ||
/// Whether the address sheet is visible | ||
@Default(true) bool visible, | ||
|
||
/// Controls how the modal is presented (after animation). iOS only. | ||
AddressSheetPresentationStyle? presentationStyle, | ||
|
||
/// Controls how the modal animates. iOS only. | ||
AddressSheetAnimationStyle? animationStyle, | ||
|
||
/// Configuration for the appearance of the address sheet | ||
PaymentSheetAppearance? appearance, | ||
|
||
/// Default values to prepoulate the address sheet | ||
AddressDetails? defaultValues, | ||
|
||
/// Configuration for additional fields besides the physical address | ||
AddressSheetAdditionalFields? additionalFields, | ||
|
||
/// A list of two-letter country codes representing countries the customers can select. If the list is empty (the default), we display all countries. | ||
List<String>? allowedCountries, | ||
|
||
/// A list of two-letter country codes representing countries that support address autocomplete. Defaults to a list of countries that Stripe has audited to ensure a good autocomplete experience. | ||
List<String>? autocompleteCountries, | ||
|
||
/// The title of the primary button displayed at the bottom of the screen. Defaults to "Save Address" | ||
String? primaryButtonTitle, | ||
|
||
/// The title of the address sheet. Defaults to "Address" | ||
String? sheetTitle, | ||
|
||
/// Android only. Google Places api key used to provide autocomplete suggestions. When null, autocomplete is disabled on Android. | ||
String? googlePlacesApiKey, | ||
}) = _AddressSheetParams; | ||
|
||
factory AddressSheetParams.fromJson(Map<String, dynamic> json) => | ||
_$AddressSheetParamsFromJson(json); | ||
} |
Oops, something went wrong.