CÔNG NGHỆ THÔNG TIN >> BÀI VIẾT CHỌN LỌC

Flutter cơ bản-Thiết kế màn hình hiển thị sản phẩm giống trang Shopee

Đăng lúc: 07:58 PM - 24/06/2024 bởi Charles Chung - 133

Trong bài viết này tôi sẽ hướng dẫn các bạn thiết kế màn hình hiển thị sản phẩm giống trang Shopee

Yêu cầu

  • Cho một List<Product>, trong đó mỗi Product gồm các thông tin: Mã số, Tên, Ảnh, Giá, Số sao đánh giá tối đa 5 sao, số lượng xem, hoa hồng extra(true, false)
  • Thiết kế giao diện trong Flutter và hiển thị danh sách sản phẩm như mô tả bên trên.

Các bước thực hiện

- Chuẩn bị thông tin về 10 sản phẩm( lên shopee xem)

- Tạo Flutter Project với tên "demoshopee"

- Tạo thư mục assets/products rồi copy ảnh sản phẩm vào đó

- Cấu hình thư mục assets/products trong file pubspec.yaml

- Cấu hình thêm dependency  intl: ^0.19.0 trong tệp pubspec.yaml để format number

- Tạo thư mục models trong lib và tạo tệp tin product.dart 

- Cấu trúc widget trên màn hình lúc chạy tham khảo hình bên dưới

alt text

- Code cho tệp product.dart theo gợi ý

class Product {
  int id;
  String name;
  double price;
  String picture;
  int views;
  double stars;
  bool extra;

  Product(this.id, this.name, this.price, this.picture, this.views, this.stars,this.extra);
}

- Code cho tệp main.dart theo gợi ý

import 'package:ex02/models/product.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Exercise05-lab09",
      home: Exercise06(),
      debugShowCheckedModeBanner: false,
    );
  }
}
class Exercise06 extends StatefulWidget
{
  @override
  State<StatefulWidget> createState() =>_Exercise06State();

}
class _Exercise06State extends State<Exercise06> {
  List<Product> products = [
    Product(1, 'Ví nam mini đựng thẻ VS22 chất da Saffiano bền đẹp chống nước',
        255000, '1.jpg', 12, 4.0, true),
    Product(
        2,
        'Túi đeo chéo LEACAT polyester chống thấm nước thời trang công sở đa năng dành cho nam',
        315000,
        '2.jpg',
        1322,
        5.0,
        false),
    Product(3, 'Phin cafe Trung Nguyên - Phin nhôm cá nhân cao cấp', 28000,
        '3.jpg', 12200, 4.5, true),
    Product(4, 'Ví da cầm tay mềm mại cỡ lớn thiết kế thời trang cho nam',
        610000, '4.jpg', 56, 5.0, true),
    Product(5, 'Dép nữ đế xuồng bản quai trơn đế độn 6cm lên chân sang chảnh',
        52000, '5.jpg', 200, 5.0, true),
    Product(
        6,
        'DTai nghe không dây âm thanh nổi M10 Tws Tai nghe nhét tai cho điện thoại thông minh Android',
        52000,
        '6.jpg',
        1230,
        4.6,
        false),
  ];
  @override
  Widget build(BuildContext context) {

    var f = NumberFormat.currency(locale: "vi_VN");
    return Scaffold(
       appBar: AppBar(title: Text("Danh sách sản phẩm"),),
      body: GridView.count(
        padding: const EdgeInsets.all(3),
        crossAxisCount: 2,
        childAspectRatio: 1 / 1.5,
        shrinkWrap: true,
        children: List.generate(
            products.length,
            (index) => Container(
                  margin: const EdgeInsets.all(3),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Image.asset('assets/products/${products[index].picture}'),
                      Text(
                        products[index].name,
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                      Row(
                        children: [
                          (products[index].extra)
                              ? Image.asset('assets/images/extra.jpg')
                              : const SizedBox(),
                          const Icon(
                            Icons.star,
                            color: Colors.amber,
                          ),
                          Text(products[index].stars.toString())
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text( f.format(products[index].price),
                            textAlign: TextAlign.left,
                            style: const TextStyle(color: Colors.red),
                          ),
                          Text(
                              '${(products[index].views > 1000.0 ? '${(products[index].views / 1000.0).toStringAsFixed(1)}k' : products[index].views)} views')
                        ],
                      )
                    ],
                  ),
                )),
      ),
    );
  }
}

- Run Android Emulator và chạy ứng dụng

Mở rộng bài tập

Kích vào từng sản phẩm thì hiển thị trang chi tiết sản phẩm với đầy đủ thông tin

thay lời cảm ơn!

QUẢNG CÁO - TIẾP THỊ