yr_promo 0.0.4
yr_promo: ^0.0.4 copied to clipboard
YR-Promo
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:yr_promo/yr_promo.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'YR-Promo Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo YR-Promo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _promoCodeController = TextEditingController();
YRPromocode? promocode;
YRHttpException? error;
bool isLoading = false;
@override
void initState() {
super.initState();
init();
}
@override
void dispose() {
_promoCodeController.dispose();
super.dispose();
}
void init() {
// You must initialize the YRPromo instance before calling
YRPromo.initialize(
apiKey: '309a5214-34f6-4fb2-9cbd-42deec6d81a3',
appId: '18',
);
}
void checkPromocode () async {
if (!canCheck) return;
promocode = null;
error = null;
setState(() {
isLoading = true;
});
try {
promocode = await YRPromo.instance.checkPromocode(
promocode: _promoCodeController.text,
userId: 'test_user_id',
);
} on YRHttpException catch (e) {
error = e;
} catch (e) {
print(e);
}
setState(() {
isLoading = false;
});
}
bool get canCheck => _promoCodeController.text.isNotEmpty;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: isLoading
? const CircularProgressIndicator()
: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
children: [
Expanded(
child: TextField(
controller: _promoCodeController,
decoration: const InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide()
),
),
inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[a-zA-Z0-9]'))],
onChanged: (_) {
setState(() {});
},
),
),
const SizedBox(width: 24),
IconButton(
onPressed: checkPromocode,
icon: Icon(
Icons.check,
size: 32,
color: canCheck ? Colors.green : null,
),
),
],
),
const SizedBox(height: 24),
if (error != null)
Text('Error: ${error!.message}', style: const TextStyle(color: Colors.red)),
if (promocode != null)
Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('Promocode info:', style: TextStyle(fontWeight: FontWeight.bold)),
Text('App Id: ${promocode!.appId}'),
Text('Name: ${promocode!.name}'),
Text('Value: ${promocode!.value}'),
Text('Expiration date: ${promocode?.expDate ?? '-'}'),
],
),
],
),
),
),
);
}
}