import 'package:flutter/material.dart'; import 'package:search_choices/search_choices.dart'; import 'package:video_book_ecommerce/compoment/ProductCard.dart'; import 'package:video_book_ecommerce/compoment/swipper.dart'; import 'package:video_book_ecommerce/model/product.dart'; import 'package:video_book_ecommerce/service/GetProduct.dart';

class Home extends StatefulWidget { @override _HomeState createState() => _HomeState(); }

class _HomeState extends State { var _productList = []; int _currentPage = 1; int _pageSize = 20; bool _isLoading = false;

@override void initState() { super.initState(); _getProducts(); }

void _getProducts() async { setState(() { _isLoading = true; }); List productList = await GetProduct.getProducts(_currentPage, _pageSize); setState(() { _productList.addAll(productList); _isLoading = false; }); }

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('商品列表'), ), body: NotificationListener( onNotification: (ScrollNotification notification) { if (!_isLoading && notification.metrics.pixels == notification.metrics.maxScrollExtent) { _currentPage++; _getProducts(); return true; } return false; }, child: ListView( children: [ Container( height: 200, child: MySwipper(), ), SizedBox(height: 10), _isLoading ? Center(child: CircularProgressIndicator()) : SizedBox.shrink(), GridView.builder( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2, childAspectRatio: 0.6, // 调整商品卡片宽高比 ), itemCount: _productList.length, shrinkWrap: true, physics: NeverScrollableScrollPhysics(), itemBuilder: (BuildContext context, int index) { product products = _productList[index]; return ProductCard( imageUrl: products.imageUrl, title: products.name, price: products.price.toString(), ); }, ), ], ), ), bottomNavigationBar: BottomNavigationBar( currentIndex: 0, items: [ BottomNavigationBarItem( icon: Icon(Icons.home), label: '首页', ), BottomNavigationBarItem( icon: Icon(Icons.shopping_cart), label: '购物车', ), BottomNavigationBarItem( icon: Icon(Icons.person), label: '我的', ), ], ), ); } }

Flutter 商品列表页面开发教程 - 首页、购物车、我的

原文地址: https://www.cveoy.top/t/topic/nJqi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录