|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/services.dart'; |
| 3 | +import 'package:tree_planting_protocol/utils/constants/ui/color_constants.dart'; |
| 4 | +import 'package:tree_planting_protocol/utils/constants/ui/dimensions.dart'; |
| 5 | + |
| 6 | +class TransactionDialog extends StatelessWidget { |
| 7 | + final String title; |
| 8 | + final String message; |
| 9 | + final String? transactionHash; |
| 10 | + final bool isSuccess; |
| 11 | + final VoidCallback? onClose; |
| 12 | + |
| 13 | + const TransactionDialog({ |
| 14 | + super.key, |
| 15 | + required this.title, |
| 16 | + required this.message, |
| 17 | + this.transactionHash, |
| 18 | + this.isSuccess = true, |
| 19 | + this.onClose, |
| 20 | + }); |
| 21 | + |
| 22 | + @override |
| 23 | + Widget build(BuildContext context) { |
| 24 | + final colors = getThemeColors(context); |
| 25 | + |
| 26 | + return Dialog( |
| 27 | + backgroundColor: colors['background'], |
| 28 | + shape: RoundedRectangleBorder( |
| 29 | + borderRadius: BorderRadius.circular(buttonCircularRadius), |
| 30 | + side: BorderSide( |
| 31 | + color: colors['border']!, |
| 32 | + width: buttonborderWidth, |
| 33 | + ), |
| 34 | + ), |
| 35 | + child: Container( |
| 36 | + constraints: const BoxConstraints(maxWidth: 400), |
| 37 | + padding: const EdgeInsets.all(24), |
| 38 | + decoration: BoxDecoration( |
| 39 | + color: colors['background'], |
| 40 | + borderRadius: BorderRadius.circular(buttonCircularRadius), |
| 41 | + ), |
| 42 | + child: SingleChildScrollView( |
| 43 | + child: Column( |
| 44 | + mainAxisSize: MainAxisSize.min, |
| 45 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 46 | + children: [ |
| 47 | + // Header with icon and title |
| 48 | + Row( |
| 49 | + children: [ |
| 50 | + Container( |
| 51 | + padding: const EdgeInsets.all(8), |
| 52 | + decoration: BoxDecoration( |
| 53 | + color: isSuccess ? colors['primary'] : colors['error'], |
| 54 | + borderRadius: BorderRadius.circular(8), |
| 55 | + border: Border.all( |
| 56 | + color: colors['border']!, |
| 57 | + width: 2, |
| 58 | + ), |
| 59 | + ), |
| 60 | + child: Icon( |
| 61 | + isSuccess ? Icons.check_circle : Icons.error, |
| 62 | + color: colors['textPrimary'], |
| 63 | + size: 24, |
| 64 | + ), |
| 65 | + ), |
| 66 | + const SizedBox(width: 12), |
| 67 | + Expanded( |
| 68 | + child: Text( |
| 69 | + title, |
| 70 | + style: TextStyle( |
| 71 | + fontSize: 20, |
| 72 | + fontWeight: FontWeight.bold, |
| 73 | + color: colors['textPrimary'], |
| 74 | + ), |
| 75 | + ), |
| 76 | + ), |
| 77 | + ], |
| 78 | + ), |
| 79 | + const SizedBox(height: 20), |
| 80 | + |
| 81 | + // Message |
| 82 | + Text( |
| 83 | + message, |
| 84 | + style: TextStyle( |
| 85 | + fontSize: 14, |
| 86 | + color: colors['textPrimary'], |
| 87 | + height: 1.5, |
| 88 | + ), |
| 89 | + ), |
| 90 | + |
| 91 | + if (transactionHash != null && transactionHash!.isNotEmpty) ...[ |
| 92 | + const SizedBox(height: 20), |
| 93 | + Container( |
| 94 | + padding: const EdgeInsets.all(12), |
| 95 | + decoration: BoxDecoration( |
| 96 | + color: colors['secondary'], |
| 97 | + borderRadius: BorderRadius.circular(8), |
| 98 | + border: Border.all( |
| 99 | + color: colors['border']!, |
| 100 | + width: 2, |
| 101 | + ), |
| 102 | + ), |
| 103 | + child: Column( |
| 104 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 105 | + children: [ |
| 106 | + Row( |
| 107 | + children: [ |
| 108 | + Icon( |
| 109 | + Icons.receipt_long, |
| 110 | + size: 16, |
| 111 | + color: colors['textPrimary'], |
| 112 | + ), |
| 113 | + const SizedBox(width: 8), |
| 114 | + Text( |
| 115 | + 'Transaction Hash', |
| 116 | + style: TextStyle( |
| 117 | + fontSize: 12, |
| 118 | + fontWeight: FontWeight.bold, |
| 119 | + color: colors['textPrimary'], |
| 120 | + ), |
| 121 | + ), |
| 122 | + ], |
| 123 | + ), |
| 124 | + const SizedBox(height: 8), |
| 125 | + Row( |
| 126 | + children: [ |
| 127 | + Expanded( |
| 128 | + child: Text( |
| 129 | + '${transactionHash!.substring(0, 10)}...${transactionHash!.substring(transactionHash!.length - 8)}', |
| 130 | + style: TextStyle( |
| 131 | + fontSize: 12, |
| 132 | + fontFamily: 'monospace', |
| 133 | + color: colors['textPrimary'], |
| 134 | + ), |
| 135 | + ), |
| 136 | + ), |
| 137 | + IconButton( |
| 138 | + padding: EdgeInsets.zero, |
| 139 | + constraints: const BoxConstraints(), |
| 140 | + onPressed: () { |
| 141 | + Clipboard.setData( |
| 142 | + ClipboardData(text: transactionHash!)); |
| 143 | + ScaffoldMessenger.of(context).showSnackBar( |
| 144 | + SnackBar( |
| 145 | + content: const Text('Hash copied!'), |
| 146 | + duration: const Duration(seconds: 1), |
| 147 | + backgroundColor: colors['primary'], |
| 148 | + ), |
| 149 | + ); |
| 150 | + }, |
| 151 | + icon: Icon( |
| 152 | + Icons.copy, |
| 153 | + size: 16, |
| 154 | + color: colors['textPrimary'], |
| 155 | + ), |
| 156 | + ), |
| 157 | + ], |
| 158 | + ), |
| 159 | + ], |
| 160 | + ), |
| 161 | + ), |
| 162 | + ], |
| 163 | + |
| 164 | + const SizedBox(height: 24), |
| 165 | + |
| 166 | + // Close Button |
| 167 | + SizedBox( |
| 168 | + width: double.infinity, |
| 169 | + height: 50, |
| 170 | + child: Material( |
| 171 | + elevation: 4, |
| 172 | + borderRadius: BorderRadius.circular(buttonCircularRadius), |
| 173 | + child: InkWell( |
| 174 | + onTap: () { |
| 175 | + Navigator.of(context).pop(); |
| 176 | + if (onClose != null) onClose!(); |
| 177 | + }, |
| 178 | + borderRadius: BorderRadius.circular(buttonCircularRadius), |
| 179 | + child: Container( |
| 180 | + decoration: BoxDecoration( |
| 181 | + color: |
| 182 | + isSuccess ? colors['primary'] : colors['secondary'], |
| 183 | + border: Border.all( |
| 184 | + color: colors['border']!, |
| 185 | + width: buttonborderWidth, |
| 186 | + ), |
| 187 | + borderRadius: |
| 188 | + BorderRadius.circular(buttonCircularRadius), |
| 189 | + ), |
| 190 | + child: Center( |
| 191 | + child: Text( |
| 192 | + 'Close', |
| 193 | + style: TextStyle( |
| 194 | + color: colors['textPrimary'], |
| 195 | + fontSize: 16, |
| 196 | + fontWeight: FontWeight.bold, |
| 197 | + ), |
| 198 | + ), |
| 199 | + ), |
| 200 | + ), |
| 201 | + ), |
| 202 | + ), |
| 203 | + ), |
| 204 | + ], |
| 205 | + ), |
| 206 | + ), |
| 207 | + ), |
| 208 | + ); |
| 209 | + } |
| 210 | + |
| 211 | + static void showSuccess( |
| 212 | + BuildContext context, { |
| 213 | + required String title, |
| 214 | + required String message, |
| 215 | + String? transactionHash, |
| 216 | + VoidCallback? onClose, |
| 217 | + }) { |
| 218 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 219 | + if (context.mounted) { |
| 220 | + showDialog( |
| 221 | + context: context, |
| 222 | + barrierDismissible: false, |
| 223 | + builder: (context) => TransactionDialog( |
| 224 | + title: title, |
| 225 | + message: message, |
| 226 | + transactionHash: transactionHash, |
| 227 | + isSuccess: true, |
| 228 | + onClose: onClose, |
| 229 | + ), |
| 230 | + ); |
| 231 | + } |
| 232 | + }); |
| 233 | + } |
| 234 | + |
| 235 | + static void showError( |
| 236 | + BuildContext context, { |
| 237 | + required String title, |
| 238 | + required String message, |
| 239 | + VoidCallback? onClose, |
| 240 | + }) { |
| 241 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 242 | + if (context.mounted) { |
| 243 | + showDialog( |
| 244 | + context: context, |
| 245 | + barrierDismissible: false, |
| 246 | + builder: (context) => TransactionDialog( |
| 247 | + title: title, |
| 248 | + message: message, |
| 249 | + isSuccess: false, |
| 250 | + onClose: onClose, |
| 251 | + ), |
| 252 | + ); |
| 253 | + } |
| 254 | + }); |
| 255 | + } |
| 256 | +} |
0 commit comments