flutter_stripe 0.0.2
flutter_stripe: ^0.0.2 copied to clipboard
A new flutter plugin project.
example/lib/main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
void main() => runApp(HomeWidget());
class HomeWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(title: Text("Home")),
body: Center(
child: Builder(
builder: (ctx) => RaisedButton(
onPressed: () {
Navigator
.of(ctx)
.push(MaterialPageRoute(builder: (_) => MyApp()));
},
child: Text("Go to payment"),
),
),
),
));
}
}
class MyApp extends StatefulWidget {
final _paymentStatus = StreamController<bool>();
FlutterStripe stripe = FlutterStripe();
String _token;
String _error;
@override
MyAppState createState() {
return new MyAppState();
}
}
class MyAppState extends State<MyApp> {
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
onSaved: (v) => widget._token = v,
validator: (v) =>
v.isEmpty ? "Please enter device token" : null,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'One time token*',
),
),
RaisedButton(
onPressed: _validate,
child: Text("Pay"),
),
StreamBuilder<bool>(
stream: widget._paymentStatus.stream,
builder: (context, snapshot) => (snapshot?.data != null)
? Text("Payment ${snapshot.data
? "Success"
: "Fail (${widget._error})" }")
: Text("Wait for payment"))
],
),
),
));
}
_validate() {
FormState form = _formKey.currentState;
if (!form.validate()) return;
form.save();
_pay();
}
_pay() async {
try {
bool result = await widget.stripe.promptNativePayment(widget._token);
widget._paymentStatus.add(result);
} catch (e) {
widget._error = e.toString();
widget._paymentStatus.add(false);
}
}
}