gpt4 book ai didi

if-statement - 如何使图标子项和文本子项仅在项目等于 1 或更大时出现,如果它们为 0 则使它们不可见

转载 作者:IT王子 更新时间:2023-10-29 07:16:32 26 4
gpt4 key购买 nike

如何使图标子项和文本子项仅在项目等于或大于 1 时出现

如果总数等于 0,我试图隐藏减号按钮和 0(显示项目数)。因此,仅当减号图标和总项目数为 1 或更大时才显示它们,如果它们不可见则使它们不可见为 0。

import 'package:flutter/material.dart';

class ClassName extends StatefulWidget {
@override
_ClassNameState createState() => _ClassNameState();
}

class _ClassNameState extends State<ClassName> {

int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
});
}

void _decrementCounter() {
setState(() {
if (_counter != 0)
_counter--;
});
}

@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 7.0, top: 7.0),
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
image: DecorationImage(fit: BoxFit.fill,image:
AssetImage("image.png"))
),
),
),
Text("Black t-shirt", style:
TextStyle(fontWeight: FontWeight.bold),),
Spacer(),
Padding(
padding: const EdgeInsets.only(top: 52.0),
child: Column(
children: <Widget>[
Text("£17.99", style: TextStyle(fontWeight:
FontWeight.bold),),
SizedBox(height: 5.0,),
Row(
children: <Widget>[
IconButton(icon:
Icon(Icons.remove_circle_outline, color: Colors.blue,), onPressed:
_decrementCounter),
Text('$_counter'),
IconButton(icon: Icon(Icons.control_point,
color: Colors.blue,), onPressed: _incrementCounter),
],
)
],
),
),
],

最佳答案

使用可见性小部件:

Visibility(
visible: totalItems > 0,
child: Row(
children: [
Icon(...),
Text(...),
],
),
)

关于if-statement - 如何使图标子项和文本子项仅在项目等于 1 或更大时出现,如果它们为 0 则使它们不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57208758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com