Skip to content

Commit

Permalink
feat: create addressheet model
Browse files Browse the repository at this point in the history
  • Loading branch information
Remon committed Dec 15, 2024
1 parent fbfcddf commit a6dd083
Show file tree
Hide file tree
Showing 6 changed files with 1,259 additions and 0 deletions.
108 changes: 108 additions & 0 deletions packages/stripe/lib/src/widgets/adresssheet.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import 'package:flutter/foundation.dart';

Check warning on line 1 in packages/stripe/lib/src/widgets/adresssheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

Filename: packages/stripe/lib/src/widgets/adresssheet.dart

"adresssheet" in the filename is a typo. Did you mean "addressee"?
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}');

Check warning on line 65 in packages/stripe/lib/src/widgets/adresssheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

blaat

"blaat" is a typo. Did you mean "blast"?
// widget.onSubmit.call();
} else if (call.method == 'onError') {
print('blaat details ${call.arguments}');

Check warning on line 68 in packages/stripe/lib/src/widgets/adresssheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

blaat

"blaat" is a typo. Did you mean "blast"?
}
});
}

@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 packages/stripe_platform_interface/lib/src/models/address_sheet.dart
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';

Check warning on line 6 in packages/stripe_platform_interface/lib/src/models/address_sheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

freezed

"freezed" is a typo. Did you mean "freezes"?
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

Check warning on line 45 in packages/stripe_platform_interface/lib/src/models/address_sheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

freezed

"freezed" is a typo. Did you mean "freezes"?
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

Check warning on line 71 in packages/stripe_platform_interface/lib/src/models/address_sheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

freezed

"freezed" is a typo. Did you mean "freezes"?
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

Check warning on line 94 in packages/stripe_platform_interface/lib/src/models/address_sheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

freezed

"freezed" is a typo. Did you mean "freezes"?
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

Check warning on line 110 in packages/stripe_platform_interface/lib/src/models/address_sheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

prepoulate

"prepoulate" is a typo. Did you mean "repopulate"?
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);
}
Loading

0 comments on commit a6dd083

Please sign in to comment.