如何解决如何在不刷新列表的情况下将多个小部件添加到列表
所以从本质上讲,我正在尝试创建一个喜欢的购物车。我可以添加一个鞋子,也可以添加多个鞋子,但是一旦按下后退按钮并尝试添加另一个鞋子,我最终只会添加该鞋子,而以前尝试添加的其他鞋子不存在。
import 'package:Flutter/material.dart';
import 'package:sample/model/products.dart';
import 'package:sample/pages/details_page.dart';
import 'package:sample/widgets/shoe_card.dart';
class discover extends StatelessWidget {
discover();
//padding: const EdgeInsets.all(8),@override
Widget build(BuildContext context) {
return Container(
child: GridView.builder(
padding: EdgeInsets.all(10),itemCount: products.length,gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisspacing: 10,mainAxisspacing: 10,crossAxisCount: 2,),itemBuilder: (context,index) => ShoeCard(
product: products[index],press: () {
Navigator.push(
context,MaterialPageRoute(
builder: (context) => Details(index)));
}),);
}
}
Details()
import 'package:Flutter/material.dart';
import 'package:sample/model/products.dart';
import 'package:sample/widgets/cart.dart';
class Details extends StatefulWidget {
int newIndex;
Details(this.newIndex);
@override
_Detailsstate createState() => _Detailsstate();
}
class _Detailsstate extends State<Details> {
List<Product> _list = new List<Product>();
void _addList() {
setState(() {
print(widget.newIndex);
var item = products[widget.newIndex];
_list.add(item);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
IconButton(
onpressed: () {
for (int i = 0; i < _list.length; i++) {
print(_list[widget.newIndex].name);
}
Navigator.push(context,MaterialPageRoute(builder: (context) => Cart(_list)));
},icon: Icon(Icons.shopping_cart)),],elevation: 0,body: Container(
margin: const EdgeInsets.fromLTRB(10,10,0),color: Colors.white,child: Padding(
padding: const EdgeInsets.fromLTRB(2,2,child: Column(
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.start,children: <Widget>[
Divider(
color: Colors.black,indent: 50,endindent: 40,Container(
child: Text(products[widget.newIndex].name),Divider(
color: Colors.black,Container(
height: 190,child: Image(
image: Assetimage(products[widget.newIndex].assetName),Container(
child: Text('Product Detail'),alignment: Alignment.topLeft,Container(
child: Divider(
endindent: 240,Container(
height: 110,child: Text(
products[widget.newIndex].descript,style: TextStyle(fontSize: 10),textAlign: TextAlign.start,Container(
padding: const EdgeInsets.only(top: 58),alignment: Alignment.center,child: Stack(
children: <Widget>[
Container(
height: 50,decoration: Boxdecoration(
color: Colors.black,borderRadius: BorderRadius.only(
topLeft: Radius.circular(24),topRight: Radius.circular(24))),Container(
alignment: Alignment.center,child: Row(
children: <Widget>[
Container(
child: IconButton(
onpressed: () {
print('tapped');
},icon: Icon(Icons.favorite),Flexible(
fit: FlexFit.tight,child: Container(
padding: EdgeInsets.only(left: 100),child: Text(
products[widget.newIndex].price,style: TextStyle(
color: Colors.white,letterSpacing: 2),Container(
padding: EdgeInsets.only(left: 70),child: IconButton(
onpressed: _addList,icon: Icon(Icons.add_shopping_cart),)
],)
],);
}
}
Cart
import 'package:Flutter/material.dart';
import 'package:sample/model/products.dart';
import 'package:sample/widgets/shoe_card.dart';
// ignore: must_be_immutable
class Cart extends StatelessWidget {
final List<Product> _cart;
Cart(this._cart);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,body: Container(
child: GridView.builder(
padding: EdgeInsets.all(10),itemCount: _cart.length,gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisspacing: 10,index) => ShoeCard(
product: _cart[index],press: () {},));
}
}
然后,当我尝试在购物车中添加多个鞋子时,在尝试添加第二个鞋子时出现此错误:
Exception caught by gesture ═══════════════════════════════════════════
The following RangeError was thrown while handling a gesture:
RangeError (index): Invalid value: Only valid value is 0: 1
When the exception was thrown,this was the stack
#0 List.[] (dart:core-patch/growable_array.dart:146:60)
#1 _Detailsstate.build.<anonymous closure>
package:sample/pages/details_page.dart:32
#2 _InkResponseState._handleTap
#3 _InkResponseState.build.<anonymous closure>
package:Flutter/…/material/ink_well.dart:862
#4 GestureRecognizer.invokeCallback
package:Flutter/…/gestures/recognizer.dart:182
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#a453e
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(332.3,51.7)
finalLocalPosition: Offset(20.3,27.7)
button: 1
sent tap down
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。