|
| 1 | +import 'package:flutter/foundation.dart'; |
| 2 | +import 'package:flutter/gestures.dart'; |
| 3 | +import 'package:flutter/rendering.dart'; |
| 4 | +import 'package:flutter/services.dart'; |
| 5 | +import 'package:flutter/widgets.dart'; |
| 6 | +import 'package:stripe_platform_interface/stripe_platform_interface.dart'; |
| 7 | + |
| 8 | +class AubecsFormField extends StatelessWidget { |
| 9 | + const AubecsFormField({ |
| 10 | + this.height = 300, |
| 11 | + this.style, |
| 12 | + this.companyName, |
| 13 | + this.controller, |
| 14 | + Key? key, |
| 15 | + }) : super(key: key); |
| 16 | + |
| 17 | + @override |
| 18 | + Widget build(BuildContext context) { |
| 19 | + return _AubecsFormField( |
| 20 | + height: height, |
| 21 | + style: style, |
| 22 | + controller: controller, |
| 23 | + companyName: companyName, |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + final AubecsFormStyle? style; |
| 28 | + final AubecsEditFormController? controller; |
| 29 | + final String? companyName; |
| 30 | + final double height; |
| 31 | +} |
| 32 | + |
| 33 | +class _AubecsFormField extends StatefulWidget { |
| 34 | + const _AubecsFormField({ |
| 35 | + required this.height, |
| 36 | + this.style, |
| 37 | + this.companyName, |
| 38 | + this.controller, |
| 39 | + Key? key, |
| 40 | + }) : super(key: key); |
| 41 | + |
| 42 | + @override |
| 43 | + _AubecsFormFieldState createState() => _AubecsFormFieldState(); |
| 44 | + |
| 45 | + final AubecsFormStyle? style; |
| 46 | + final AubecsEditFormController? controller; |
| 47 | + final String? companyName; |
| 48 | + final double height; |
| 49 | +} |
| 50 | + |
| 51 | +class _AubecsFormFieldState extends State<_AubecsFormField> { |
| 52 | + static const _viewType = 'flutter.stripe/aubecs_form_field'; |
| 53 | + |
| 54 | + MethodChannel? _methodChannel; |
| 55 | + AubecsFormStyle? _lastStyle; |
| 56 | + |
| 57 | + AubecsEditFormController? _fallbackContoller; |
| 58 | + AubecsEditFormController get controller { |
| 59 | + if (widget.controller != null) return widget.controller!; |
| 60 | + _fallbackContoller ??= AubecsEditFormController(); |
| 61 | + return _fallbackContoller!; |
| 62 | + } |
| 63 | + |
| 64 | + @override |
| 65 | + void initState() { |
| 66 | + super.initState(); |
| 67 | + } |
| 68 | + |
| 69 | + @override |
| 70 | + void didChangeDependencies() { |
| 71 | + _lastStyle ??= widget.style; |
| 72 | + final style = widget.style; |
| 73 | + |
| 74 | + if (style != _lastStyle && style != null) { |
| 75 | + _methodChannel?.invokeMethod('onStyleChanged', { |
| 76 | + 'formStyle': style.toJson(), |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + _lastStyle = style; |
| 81 | + super.didChangeDependencies(); |
| 82 | + } |
| 83 | + |
| 84 | + @override |
| 85 | + void didUpdateWidget(covariant _AubecsFormField oldWidget) { |
| 86 | + if (widget.style != null && widget.style != oldWidget.style) { |
| 87 | + _methodChannel?.invokeMethod('onStyleChanged', { |
| 88 | + 'formStyle': widget.style!.toJson(), |
| 89 | + }); |
| 90 | + } |
| 91 | + if (widget.companyName != oldWidget.companyName) { |
| 92 | + _methodChannel?.invokeMethod('onCompanyNameChanged', { |
| 93 | + 'companyName': widget.companyName ?? '', |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + _lastStyle = widget.style; |
| 98 | + super.didUpdateWidget(oldWidget); |
| 99 | + } |
| 100 | + |
| 101 | + void onPlatformViewCreated(int viewId) { |
| 102 | + _methodChannel = MethodChannel('flutter.stripe/aubecs_form_field/$viewId'); |
| 103 | + _methodChannel!.setMethodCallHandler((call) async { |
| 104 | + if (call.method == 'onCompleteAction') { |
| 105 | + final tmp = _createData(call.arguments); |
| 106 | + controller.data = tmp; |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + AubecsFormInputDetails _createData(dynamic rawData) { |
| 112 | + final map = Map<String, dynamic>.from(rawData); |
| 113 | + return AubecsFormInputDetails.fromJson(map); |
| 114 | + } |
| 115 | + |
| 116 | + @override |
| 117 | + Widget build(BuildContext context) { |
| 118 | + final creationParams = <String, Object>{ |
| 119 | + 'formStyle': widget.style?.toJson() ?? const AubecsFormStyle(), |
| 120 | + 'companyName': widget.companyName ?? '', |
| 121 | + }; |
| 122 | + |
| 123 | + return SizedBox( |
| 124 | + height: widget.height, |
| 125 | + child: defaultTargetPlatform == TargetPlatform.iOS |
| 126 | + ? UiKitView( |
| 127 | + viewType: _viewType, |
| 128 | + creationParamsCodec: const StandardMessageCodec(), |
| 129 | + creationParams: creationParams, |
| 130 | + onPlatformViewCreated: onPlatformViewCreated, |
| 131 | + ) |
| 132 | + : PlatformViewLink( |
| 133 | + surfaceFactory: (context, controller) { |
| 134 | + return AndroidViewSurface( |
| 135 | + controller: controller as AndroidViewController, |
| 136 | + hitTestBehavior: PlatformViewHitTestBehavior.opaque, |
| 137 | + gestureRecognizers: const < |
| 138 | + Factory<OneSequenceGestureRecognizer>>{}, |
| 139 | + ); |
| 140 | + }, |
| 141 | + onCreatePlatformView: (params) { |
| 142 | + onPlatformViewCreated(params.id); |
| 143 | + return PlatformViewsService.initSurfaceAndroidView( |
| 144 | + id: params.id, |
| 145 | + viewType: _viewType, |
| 146 | + layoutDirection: TextDirection.ltr, |
| 147 | + creationParams: creationParams, |
| 148 | + creationParamsCodec: const StandardMessageCodec(), |
| 149 | + ) |
| 150 | + ..addOnPlatformViewCreatedListener( |
| 151 | + params.onPlatformViewCreated) |
| 152 | + ..create(); |
| 153 | + }, |
| 154 | + viewType: _viewType, |
| 155 | + ), |
| 156 | + ); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +class AubecsEditFormController extends ChangeNotifier { |
| 161 | + set data(AubecsFormInputDetails? newData) { |
| 162 | + if (data == newData) { |
| 163 | + return; |
| 164 | + } else { |
| 165 | + _data = newData; |
| 166 | + notifyListeners(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + bool get isComplete => |
| 171 | + _data?.bsbNumber != null && |
| 172 | + _data?.accountNumber != null && |
| 173 | + _data?.email != null && |
| 174 | + _data?.name != null; |
| 175 | + |
| 176 | + AubecsFormInputDetails? get data => _data; |
| 177 | + |
| 178 | + AubecsFormInputDetails? _data; |
| 179 | +} |
0 commit comments