Menu
CoddyTech
flag Ar iconالعربيةdown icon

المتجر: الأسعار

جزء من قسم المشاريع في رحلة Flutter على Coddy. الدرس 20 من 36.

ينبغي أن يظهر السعر 45.5 بالشكل $45.50، وأن يظهر 1299 بالشكل $1,299.00. كتابة ذلك يدويًا أمر مزعج؛ وتتولى intl وNumberFormat تنسيقه بالطريقة نفسها لكل سعر.

أنشئ منسّقًا واحدًا للتطبيق بأكمله، في أعلى الملف، واستخدمه في كل موضع يُعرض فيه سعر:

import 'package:intl/intl.dart';

final money = NumberFormat.currency(locale: 'en_US', symbol: '\$');

money.format(1299); // $1,299.00

يحدد locale: 'en_US' طريقة كتابة الأرقام: فاصلة بين الآلاف ونقطة قبل أجزاء السنت. في de_DE يُكتب السعر نفسه 1.299,00. تحدد symbol ما يظهر في المقدمة؛ وفي Dart تبدأ $ داخل السلسلة النصية عملية الاستيفاء، لذا اكتبها '\$'.

اعرض السعر المنسّق أسفل كل اسم، بخط عريض وبلون primary الخاص بالسمة، كي تبرز الأسعار عن الأسماء.

challenge icon

التحدي

سهل

اعرض الأسعار:

  • استورد package:intl/intl.dart وأضف مُنسّق money المذكور في الشرح النظري أعلاه MyApp
  • أسفل الاسم على كل بطاقة، وعلى بُعد 4 بكسلات، اعرض money.format(product.price) بخط عريض، وباللون Theme.of(context).colorScheme.primary

جرّب بنفسك

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.orange),
      ),
      home: const ShopScreen(),
    );
  }
}

class Product {
  const Product({
    required this.name,
    required this.price,
    required this.icon,
    required this.color,
    required this.description,
  });

  final String name;
  final double price;
  final IconData icon;
  final MaterialColor color;
  final String description;
}

const List<Product> products = [
  Product(
    name: 'Headphones',
    price: 59,
    icon: Icons.headphones,
    color: Colors.indigo,
    description: 'Wireless headphones with 30 hours of battery.',
  ),
  Product(
    name: 'Watch',
    price: 129,
    icon: Icons.watch,
    color: Colors.teal,
    description: 'A smart watch that counts your steps.',
  ),
  Product(
    name: 'Backpack',
    price: 45.5,
    icon: Icons.backpack,
    color: Colors.orange,
    description: 'Room for a laptop, a lunch and a jacket.',
  ),
  Product(
    name: 'Mug',
    price: 12,
    icon: Icons.coffee,
    color: Colors.brown,
    description: 'Holds a big coffee and keeps it warm.',
  ),
  Product(
    name: 'Camera',
    price: 1299,
    icon: Icons.photo_camera,
    color: Colors.blueGrey,
    description: 'A mirrorless camera for sharp photos.',
  ),
  Product(
    name: 'Sneakers',
    price: 89.99,
    icon: Icons.directions_run,
    color: Colors.red,
    description: 'Light running shoes for every day.',
  ),
];

class ShopScreen extends StatelessWidget {
  const ShopScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Coddy Shop')),
      body: GridView.count(
        crossAxisCount: 2,
        padding: const EdgeInsets.all(16),
        mainAxisSpacing: 12,
        crossAxisSpacing: 12,
        childAspectRatio: 0.8,
        children: [
          for (Product product in products) ProductCard(product: product),
        ],
      ),
    );
  }
}

class ProductCard extends StatelessWidget {
  const ProductCard({super.key, required this.product});

  final Product product;

  @override
  Widget build(BuildContext context) {
    return Card(
      clipBehavior: Clip.antiAlias,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
            child: Container(
              width: double.infinity,
              alignment: Alignment.center,
              color: product.color.shade50,
              child: Icon(product.icon, size: 56, color: product.color),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(12),
            child: Text(
              product.name,
              style: Theme.of(context).textTheme.titleMedium,
            ),
          ),
        ],
      ),
    );
  }
}
quiz iconاختبر نفسك

يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.

جميع دروس المشاريع

تدرّب بنفسك: مترجم Flutter عبر الإنترنت