سأضيف الحقل لإدخال الرصيد الإضافي، ونعدل زر المواصلة ليضيف الرصيد الجديد إلى الرصيد الموجود.
dart Copy code
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
class ThousandsSeparatorInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue,
TextEditingValue newValue,
) {
String onlyDigits = newValue.text.replaceAll(RegExp(r'[^0-9]'), '');
if (onlyDigits.isEmpty) return newValue.copyWith(text: '');
final buffer = StringBuffer();
int len = onlyDigits.length;
for (int i = 0; i < len; i++) {
buffer.write(onlyDigits[i]);
int posFromRight = len - i - 1;
if (posFromRight % 3 == 0 && i != len - 1) buffer.write(',');
}
return TextEditingValue(
text: buffer.toString(),
selection: TextSelection.collapsed(offset: buffer.length),
);
}
}
class SplashScreen extends StatefulWidget {
const SplashScreen({super.key});
@override
State<SplashScreen> createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen>
with TickerProviderStateMixin {
bool _isLoading = true;
bool _hasSavedBalance = false;
int _currentBalance = 0;
late TextEditingController _balanceController;
late TextEditingController _additionalBalanceController;
late AnimationController _animationController;
late Animation<double> _logoScaleAnimation;
late Animation<Offset> _contentSlideAnimation;
late Animation<double> _fadeAnimation;
@override
void initState() {
super.initState();
_balanceController = TextEditingController();
_additionalBalanceController = TextEditingController();
_animationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 1000),
);
_logoScaleAnimation = Tween<double>(begin: 0.8, end: 1).animate(
CurvedAnimation(parent: _animationController, curve: Curves.easeOutBack),
);
_contentSlideAnimation = Tween<Offset>(
begin: const Offset(0, 30),
end: Offset.zero,
).animate(
CurvedAnimation(parent: _animationController, curve: Curves.easeOutQuart),
);
_fadeAnimation = Tween<double>(begin: 0, end: 1).animate(
CurvedAnimation(parent: _animationController, curve: Curves.easeInOut),
);
_animationController.forward();
_loadBalance();
}
@override
void dispose() {
_balanceController.dispose();
_additionalBalanceController.dispose();
_animationController.dispose();
super.dispose();
}
Future<void> _loadBalance() async {
final prefs = await SharedPreferences.getInstance();
final savedBalance = prefs.getInt('current_balance');
if (savedBalance != null) {
_currentBalance = savedBalance;
_hasSavedBalance = true;
}
setState(() {
_isLoading = false;
if (_hasSavedBalance) {
_balanceController.text = _formatWithThousandsSeparator(_currentBalance);
}
});
}
String _formatWithThousandsSeparator(int value) {
final s = value.toString();
final buffer = StringBuffer();
final len = s.length;
for (int i = 0; i < len; i++) {
buffer.write(s[i]);
final posFromRight = len - i - 1;
if (posFromRight % 3 == 0 && i != len - 1) buffer.write(',');
}
return buffer.toString();
}
Future<void> _saveBalance(int balance) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('current_balance', balance);
}
void _onContinue() {
final currentInput = _balanceController.text.replaceAll(',', '').trim();
final additionalInput = _additionalBalanceController.text.replaceAll(',', '').trim();
final currentBalanceInput = int.tryParse(currentInput);
final additionalBalanceInput = int.tryParse(additionalInput.isEmpty ? '0' : additionalInput);
if (currentBalanceInput == null || currentBalanceInput < 0) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('يرجى إدخال مبلغ رصيد صالح (صفر أو أكثر) في الرصيد الحالي'),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
backgroundColor: Theme.of(context).colorScheme.error,
),
);
return;
}
if (additionalInput.isNotEmpty && (additionalBalanceInput == null || additionalBalanceInput < 0)) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('يرجى إدخال مبلغ رصيد إضافي صالح (صفر أو أكثر) أو تركه فارغًا'),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
backgroundColor: Theme.of(context).colorScheme.error,
),
);
return;
}
// حساب الرصيد الجديد
int totalBalance = currentBalanceInput + (additionalBalanceInput ?? 0);
_saveBalance(totalBalance).then((_) {
Navigator.pushReplacementNamed(context, '/home');
});
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final colors = theme.colorScheme;
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: _isLoading
? Center(
child: CircularProgressIndicator(
color: colors.primary,
strokeWidth: 2,
),
)
: Column(
children: [
Expanded(
child: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(height: 40),
ScaleTransition(
scale: _logoScaleAnimation,
child: Container(
width: 120,
height: 120,
decoration: BoxDecoration(
color: colors.primary.withOpacity(0.1),
shape: BoxShape.circle,
),
child: Icon(
Icons.account_balance_wallet,
size: 60,
color: colors.primary,
),
),
),
const SizedBox(height: 40),
SlideTransition(
position: _contentSlideAnimation,
child: FadeTransition(
opacity: _fadeAnimation,
child: Column(
children: [
Text(
'مرحبًا بك في محول الرصيد',
style: theme.textTheme.headlineSmall?.copyWith(
color: Colors.black,
fontWeight: FontWeight.bold,
height: 1.4,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
Text(
'أدخل رصيدك الحالي أو أضف رصيدًا إضافيًا لبدء إدارة وتحويل رصيدك بسهولة',
style: theme.textTheme.bodyLarge?.copyWith(
color: Colors.grey[700],
height: 1.6,
),
textAlign: TextAlign.center,
),
],
),
),
),
const SizedBox(height: 40),
SlideTransition(
position: _contentSlideAnimation,
child: FadeTransition(
opacity: _fadeAnimation,
child: TextField(
controller: _balanceController,
keyboardType: TextInputType.number,
inputFormatters: [
ThousandsSeparatorInputFormatter(),
],
textAlign: TextAlign.right,
style: theme.textTheme.titleMedium?.copyWith(fontWeight: FontWeight.w600),
decoration: InputDecoration(
filled: true,
fillColor: Colors.grey[50],
hintText: 'مثال: 1,000',
labelText: 'الرصيد الحالي',
prefixIcon: Icon(
Icons.account_balance_wallet,
color: colors.primary,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: Colors.grey[300]!,
),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: Colors.grey[300]!,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: colors.primary,
width: 2,
),
),
floatingLabelBehavior: FloatingLabelBehavior.always,
contentPadding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 16,
),
),
),
),
),
const SizedBox(height: 24),
// حقل الرصيد الإضافي
SlideTransition(
position: _contentSlideAnimation,
child: FadeTransition(
opacity: _fadeAnimation,
child: TextField(
controller: _additionalBalanceController,
keyboardType: TextInputType.number,
inputFormatters: [
ThousandsSeparatorInputFormatter(),
],
textAlign: TextAlign.right,
style: theme.textTheme.titleMedium?.copyWith(fontWeight: FontWeight.w600),
decoration: InputDecoration(
filled: true,
fillColor: Colors.grey[50],
hintText: 'مثال: 500 (اختياري)',
labelText: 'رصيد إضافي (اختياري)',
prefixIcon: Icon(
Icons.add_circle_outline,
color: colors.primary,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: Colors.grey[300]!,
),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: Colors.grey[300]!,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: colors.primary,
width: 2,
),
),
floatingLabelBehavior: FloatingLabelBehavior.always,
contentPadding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 16,
),
),
),
),
),
],
),
),
),
const SizedBox(height: 24),
SlideTransition(
position: _contentSlideAnimation,
child: FadeTransition(
opacity: _fadeAnimation,
child: SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _onContinue,
style: ElevatedButton.styleFrom(
backgroundColor: colors.primary,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(
vertical: 16,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
elevation: 0,
shadowColor: Colors.transparent,
),
child: Text(
'ابدأ الآن',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
),
),
),
],
),
),
),
);
}
}
يمكنك استخدام الكود هذا مباشرة في مشروعك. إذا أردت التخصيص أكثر أو دمجه بطريقة مختلفة أخبرني!