答案 0 :(得分:1)
尝试以下代码:
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
int i = 0;
@override
Widget build(BuildContext context) {
return Row(children: [
Visibility(
visible: i > 0,
replacement: RaisedButton(
color: Colors.orangeAccent,
child: new Text("Add"),
onPressed: () => setState(() { ++i;}),
),
child: Row(
children: [
RaisedButton(
color: Colors.orangeAccent,
child: new Text("-"),
onPressed: () => setState(() { --i;}),
),
Container(
width: 100,
child: Text(
i.toString(),
textAlign: TextAlign.center,
),
),
],
),
),
RaisedButton(
color: Colors.orangeAccent,
child: new Text("+"),
onPressed: () => setState(() { ++i;}),
)
]);
}
}