我想為我的 Flutter-Dart 電子商務 App 創建一個動態按鈕,樣式需要是這樣的:
添加物品時,可以選擇數量,但是當數量回到0時,“添加物品”按鈕會再次出現,并帶有淡入淡出的效果。
第一個例子

第二個例子

uj5u.com熱心網友回復:
這是一個簡單的示例,我將如何創建此行為。
你可以在 DartPad 上自己測驗這段代碼
class MyWidget extends StatefulWidget {
const MyWidget({Key? key}) : super(key: key);
@override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
int quantity = 0;
@override
Widget build(BuildContext context) {
return Card(
color: Colors.blue,
child: SizedBox(
height: 50,
width: 100,
child: quantity == 0
? TextButton(
child: const Text(
'Add Item',
style: TextStyle(
color: Colors.white,
),
),
onPressed: () {
setState(() {
quantity ;
});
},
)
: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: const Icon(Icons.remove),
onPressed: () {
setState(() {
quantity--;
});
},
),
Text(quantity.toString()),
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
setState(() {
quantity ;
});
},
),
],
),
));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/487470.html
