【发布时间】:2020-12-16 00:08:51
【问题描述】:
我正在从事电子商务项目,该项目展示了几种产品。因此,我必须创建具有产品主要组件的单独类,以便在列表构建器中多次调用它。 下图显示用户可以按下 + 或 - 按钮来更改内部文本的值,但这不会设置文本值的状态,因为这个单独的类不扩展有状态小部件。 所以我的问题是,如何设置屏幕上文字的状态值?
编辑:这是显示功能的代码。当我按下按钮时,我需要更改内部文本的值
Widget productCard(String img_link, String title, String details, String price) {
int 数量Ordered = 0; 返回容器(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(50),
child: Image( image: NetworkImage(img_link, ),
height: 200,
width: 200,
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(padding: EdgeInsets.only(left: 8, bottom: 5),child: Text(title, style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),)),
Container(
padding: EdgeInsets.only(left: 8, bottom: 5),
width:150,
child: Text(details, style: TextStyle(fontSize: 18), softWrap: true, overflow: TextOverflow.clip,)
),
Container(padding: EdgeInsets.only(left: 8, bottom: 5),child: Text(price+"\$", style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold, color: Colors.red)))
]
)
],
),
// + / - Button
Container(
padding: EdgeInsets.only(bottom: 5),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
),
width: 250,
height: 35,
child: Row(
children: <Widget>[
Expanded(
child: RaisedButton(
child: Text("-", style: TextStyle(fontSize: 20, color: Colors.white),),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.only( bottomLeft: Radius.circular(10),topLeft: Radius.circular(10) )),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
color: Color.fromRGBO(45,182,169, 1),
elevation: 1,
onPressed: (){
quantityOrdered--;
},
)
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
color: Color.fromRGBO(45,182,169, 0.85),
height: double.infinity,
width: 50,
child: Center(
child: Text(
"0",
style: TextStyle(
color: Colors.white,
),
textAlign: TextAlign.center,
),
),
),
],
),
Expanded(
child: RaisedButton(
child: Text("+", style: TextStyle(fontSize: 20, color: Colors.white),),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.only( bottomRight: Radius.circular(10),topRight: Radius.circular(10) )),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
color: Color.fromRGBO(45,182,169, 1),
elevation: 1,
onPressed: (){
quantityOrdered++;
},
)
)
],
),
),
// Order Button
Container(
width: 150,
child: RaisedButton(
child: Text("Add to cart", style: TextStyle(fontSize: 22, color: Colors.white),),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.only( bottomLeft: Radius.circular(10),topLeft: Radius.circular(10),
bottomRight: Radius.circular(10), topRight: Radius.circular(10))),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
color: Color.fromRGBO(45,182,169, 1),
onPressed: (){},
),
)
],
),
); }
【问题讨论】:
-
你能分享你的代码吗?
标签: flutter