import 'package:flutter/material.dart'; import 'package:mhunt_launcher/Widgets/CustomWidgets.dart'; import 'package:toastification/toastification.dart'; import 'package:url_launcher/url_launcher_string.dart'; import '../../../Backend/Backend.dart'; import '../../../Backend/DebugHelper.dart'; import '../../../Shared/Dialogs.dart'; import '../../../Shared/Helpers.dart'; import '../../../Shared/TextStyles.dart'; Route buyTicketsDialogBuilder( BuildContext context, Object? arguments) { return DialogRoute( context: context, builder: (BuildContext context) { return AlertDialog( content: PurchaseTicketsContent(), ); }, ); } class PurchaseTicketsContent extends StatefulWidget { const PurchaseTicketsContent({super.key}); @override State createState() => _PurchaseTicketsContentState(); } int ticketCount = 1; bool isLoading = false; class _PurchaseTicketsContentState extends State { @override void initState() { // TODO: implement initState super.initState(); isLoading=false; setState(() { }); } @override Widget build(BuildContext context) { return isLoading ? Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Text("Loading... Please wait"), ], ) :BuyWindow( context: context, onTicketDecrease: () => { setState(() { if (ticketCount > 1) ticketCount--; }) }, onTicketIncrease: () => { setState(() { ticketCount++; }) }, onPurchase: ()async{ setState(() { isLoading=true; }); dynamic result =await Backend.PurchaseTickets(ticketCount); if(result['status'] == "Success"){ toastification.show( context: context, // optional if you use ToastificationWrapper title: Text('$ticketCount Tickets purchased successfully, Click to show Transaction'), callbacks: ToastificationCallbacks( onTap: (toastItem){ launchUrlString('https://explorer.solana.com/tx/${result['tx']}?cluster=devnet'); } ), autoCloseDuration: const Duration(seconds: 10), style: ToastificationStyle.simple, ); } Navigator.of(context).pop(); } ); } } Widget BuyWindow( {required BuildContext context, required Function() onTicketDecrease, required Function() onTicketIncrease, required Function() onPurchase }) { int totalCostLamports = Helpers.TICKET_PRICE * ticketCount; double totalCostSol = totalCostLamports / Helpers.LAMPORTS_PER_SOL; bool hasEnoughSol = Backend.SolBalance > totalCostLamports; Debug.Log( 'I have ${Backend.SolBalance} LAMPORTS, Ticket price is ${totalCostLamports}, do I have enough? :${hasEnoughSol}'); return Column( mainAxisSize: MainAxisSize.min, children: [ Text( 'Select an amount of Tickets you want to purchase', ), SizedBox( height: 20, ), Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ Row( mainAxisAlignment: MainAxisAlignment.center, children: [ InkWell( onTap: onTicketDecrease, child: Icon(Icons.remove_circle), ), Padding( padding: const EdgeInsets.all(8.0), child: Text( '$ticketCount', style: TextStyle(fontSize: 40), ), ), InkWell( onTap: onTicketIncrease, child: Icon(Icons.add_circle_rounded), ), ], ), Column( children: [ Text( '${Helpers.trimTrailingZeros(totalCostSol)} SOL', style: TextStyles.titleTextStyle, ), (!hasEnoughSol ? Text( "Insufficient balance", style: TextStyle(color: Colors.red), ) : Container()) ], ), ], ), Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ (hasEnoughSol ? GlassButton( onTap: onPurchase, child: Text("Purchase"), width: 250, color: Colors.green) : GlassButton( onTap: () { launchUrlString( '${Helpers.MOONPAY_URL}/?wallet=${Backend.pubKey}'); }, child: Text("Recharge"), width: 250, color: Colors.blue)), ], ), Padding( padding: const EdgeInsets.all(8.0), child: GlassButton( onTap: () { Navigator.of(context).pop(); }, child: Text("Cancel"), width: 150, color: Colors.red), ) ], ); }