Toko: Harga
Termasuk bagian Proyek dalam perjalanan Flutter di Coddy. Pelajaran 20 dari 36.
Harga 45.5 seharusnya ditampilkan sebagai $45.50, dan 1299 seharusnya ditampilkan sebagai $1,299.00. Menulisnya secara manual cukup merepotkan; NumberFormat dari intl akan melakukannya dengan cara yang sama untuk setiap harga.
Buat satu formatter untuk seluruh aplikasi di bagian atas file, lalu gunakan di setiap tempat harga ditampilkan:
import 'package:intl/intl.dart';
final money = NumberFormat.currency(locale: 'en_US', symbol: '\$');
money.format(1299); // $1,299.00locale: 'en_US' menentukan format penulisan angka: koma sebagai pemisah ribuan dan titik sebelum sen. Dalam de_DE, harga yang sama ditulis 1.299,00. symbol menentukan simbol yang ditampilkan di depan; di Dart, $ dalam string memulai interpolasi, jadi tulislah sebagai '\$'.
Di bawah setiap nama, tampilkan harga yang sudah diformat dengan huruf tebal dan warna primary dari tema, supaya harga terlihat menonjol dibandingkan nama.
Tantangan
MudahTampilkan harga:
- impor
package:intl/intl.dartdan tambahkan pemformatmoneydari teori di atasMyApp - di bawah nama pada setiap kartu, dengan jarak 4 piksel, tampilkan
money.format(product.price)dengan huruf tebal, menggunakanTheme.of(context).colorScheme.primary
Coba sendiri
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,
),
),
],
),
);
}
}
Pelajaran ini berisi kuis singkat. Mulai pelajarannya untuk menjawab kuis dan melacak progresmu.
Semua pelajaran di Proyek
1Proyek: Daftar Tugas
Daftar Tugas: Daftar TugasDaftar Tugas: Tambahkan Tugas4Proyek: Pelacak Kebiasaan
Pelacak Kebiasaan: KebiasaanmuPelacak Kebiasaan: Pekan IniLatihan sendiri: Compiler Flutter online