import 'package:flutter/material.dart';/nimport 'package:cached_network_image/cached_network_image.dart';/nimport '../models/cart.dart';/nimport '../services/api_service.dart';/n/nclass CartPage extends StatefulWidget {/n @override/n _CartPageState createState() => _CartPageState();/n}/n/nclass _CartPageState extends State {/n List _carts = [];/n/n @override/n void initState() {/n super.initState();/n _loadCarts();/n }/n/n Future _loadCarts() async {/n List carts = await ApiService.getCartList();/n setState(() {/n _carts = carts;/n });/n }/n/n Future _updateCart(int productId, int quantity) async {/n dynamic data = await ApiService.updateCart(productId, quantity);/n setState(() {/n if (data is List) {/n _carts = _carts.map((cart) {/n if (cart.productId == productId) {/n return Cart.fromJson(data[1]);/n } else {/n return cart;/n }/n }).toList();/n }/n });/n }/n/n Future _deleteCart(int productId) async {/n dynamic data = await ApiService.deleteCart(productId);/n setState(() {/n if (data is double) {/n _carts.removeWhere((cart) => cart.productId == productId);/n }/n });/n }/n/n TextStyle _textStyle = TextStyle(fontSize: 16);/n/n @override/n Widget build(BuildContext context) {/n return Scaffold(/n appBar: AppBar(/n title: Text('购物车'),/n centerTitle: true,/n ),/n body: _carts.isNotEmpty/n ? Column(/n children: [/n Expanded(/n child: ListView.builder(/n itemCount: _carts.length,/n itemBuilder: (BuildContext context, int index) {/n final cart = _carts[index];/n return Card(/n margin: EdgeInsets.all(8),/n child: Row(/n children: [/n Expanded(/n child: CachedNetworkImage(/n imageUrl: cart.productImageUrl,/n fit: BoxFit.cover,/n errorWidget: (context, url, error) =>/n Icon(Icons.error),/n ),/n ),/n Expanded(/n flex: 2,/n child: Padding(/n padding: EdgeInsets.all(8),/n child: Column(/n crossAxisAlignment: CrossAxisAlignment.start,/n children: [/n Text(/n cart.productName,/n style: TextStyle(/n fontSize: 18,/n fontWeight: FontWeight.bold,/n ),/n ),/n SizedBox(height: 4),/n Text(/n '价格: ¥${cart.price.toStringAsFixed(2)}',/n style: _textStyle,/n ),/n SizedBox(height: 4),/n Text(/n '数量: ${cart.quantity}',/n style: _textStyle,/n ),/n ],/n ),/n ),/n ),/n Expanded(/n child: IconButton(/n icon: Icon(Icons.add),/n onPressed: () {/n _updateCart(cart.productId, 1);/n },/n ),/n ),/n Expanded(/n child: IconButton(/n icon: Icon(Icons.remove),/n onPressed: () {/n _updateCart(cart.productId, -1);/n },/n ),/n ),/n Expanded(/n child: IconButton(/n icon: Icon(Icons.delete),/n onPressed: () {/n _deleteCart(cart.productId);/n },/n ),/n ),/n ],/n ),/n );/n },/n ),/n ),/n SizedBox(/n height: 50,/n child: Row(/n mainAxisAlignment: MainAxisAlignment.spaceAround,/n children: [/n Text(/n '总价:',/n style: _textStyle,/n ),/n Text(/n '¥${_carts.fold(0, (acc, cart) => acc + cart.price * cart.quantity).toStringAsFixed(2)}',/n style: textStyle,/n ),/n RaisedButton(/n child: Text('结算'),/n onPressed: () {/n showDialog(/n context: context,/n builder: () => AlertDialog(/n title: Text('付款'),/n content: Column(/n mainAxisSize: MainAxisSize.min,/n children: [/n Text('选择地址'),/n SizedBox(height: 8),/n FutureBuilder(/n future: ApiService.getAddressList(),/n builder: (BuildContext context,/n AsyncSnapshot snapshot) {/n if (snapshot.hasData) {/n return Container(/n height: 200,/n child: ListView.builder(/n itemCount: snapshot.data.length,/n itemBuilder: (BuildContext context,/n int index) {/n final address =/n snapshot.data[index];/n return ListTile(/n title: Text(/n '${address.provience}${address.city}${address.district}${address.addressLine}',/n ),/n subtitle: Text(/n address.isDefault == 1/n ? '默认地址'/n : '',/n ),/n trailing: Icon(Icons/n .keyboard_arrow_right),/n );/n },/n ),/n );/n } else {/n return CircularProgressIndicator();/n }/n },/n ),/n SizedBox(height: 8),/n Text('新增地址'),/n SizedBox(height: 8),/n TextFormField(/n decoration: InputDecoration(/n hintText: '收货人姓名',/n ),/n ),/n SizedBox(height: 8),/n TextFormField(/n decoration: InputDecoration(/n hintText: '收货人电话',/n ),/n ),/n SizedBox(height: 8),/n TextFormField(/n decoration: InputDecoration(/n hintText: '省份',/n ),/n ),/n SizedBox(height: 8),/n TextFormField(/n decoration: InputDecoration(/n hintText: '城市',/n ),/n ),/n SizedBox(height: 8),/n TextFormField(/n decoration: InputDecoration(/n hintText: '区县',/n ),/n ),/n SizedBox(height: 8),/n TextFormField(/n decoration: InputDecoration(/n hintText: '详细地址',/n ),/n ),/n SizedBox(height: 8),/n Text('选择支付方式'),/n SizedBox(height: 8),/n Row(/n mainAxisAlignment: MainAxisAlignment.spaceAround,/n children: [/n Column(/n children: [/n Icon(Icons.alipay),/n SizedBox(height: 4),/n Text('支付宝'),/n ],/n ),/n Column(/n children: [/n Icon(Icons.payment),/n SizedBox(height: 4),/n Text('微信'),/n ],/n ),/n ],/n ),/n SizedBox(height: 8),/n RaisedButton(/n child: Text('确认付款'),/n onPressed: () {},/n ),/n ],/n ),/n ),/n );/n },/n ),/n ],/n ),/n ),/n ],/n )/n : Center(/n child: Text('购物车为空'),/n ),/n );/n }/