gpt4 book ai didi

flutter - 按下按钮后添加卡片小部件

转载 作者:行者123 更新时间:2023-12-03 03:30:16 30 4
gpt4 key购买 nike

我试图在按下按钮后在应用程序中连续动态添加卡。
我尝试了不同的方法,但似乎没有任何正常工作,现在我到达了这一点:

    cardList = [];
setState(() {
cardList.add(new DynamicCard());
});
}
这是我用来添加新卡的方法,在下面的alertDialog中被调用:
return    Alert(
context: context,
title: "Add activity",
content: Column(
children: <Widget>[
DropdownButton(
hint: Text('Select your activity'),
icon: Icon(Icons.arrow_drop_down),
value: selectedActivity,
onChanged: (value){
setState(() {
value = selectedActivity;
print(value);
});
},
//value: selectedActivity,
items: activityList.map((value) {
return DropdownMenuItem(
value: value,
child: Text(value));
}).toList()
),
TextField(
decoration: InputDecoration(
labelText: 'Where',
),
),
],
),
buttons: [
DialogButton(
onPressed: () {
addCard();
Navigator.pop(context);
},
child: Text(
"Add Activity",
style: TextStyle(color: Colors.white, fontSize: 20),
),
)
]).show();
这是我要在按下按钮后添加的卡片:
import 'package:flutter/material.dart';
import 'package:wildnature/widgets/sizeConfig.dart';

class DynamicCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SizedBox(
height: 250,
width: 350,
child: Card(
elevation: 6,
clipBehavior: Clip.antiAlias,
child: Column(
children: [
ListTile(
title: Text('Last Activity:'),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'Test123',
style:
TextStyle(fontSize: 5 * SizeConfig.blockSizeHorizontal),
)),
Container(
width: 200,
height: 160,
child: Image.asset('assets/camping.png', fit: BoxFit.fill),
)
],
),
),
);
}
}
另外,如何在所需的确切位置渲染此小部件?

最佳答案

解:

  • 创建一个新变量:
  •  bool addWidget = false;
  • 创建了一个新的小部件:

  • Widget createActivityCard(String activy, String activityDesc){
    return SizedBox(
    height: 250,
    width: 350,
    child: Card(
    elevation: 6,
    clipBehavior: Clip.antiAlias,
    child: Column(
    children: [
    ListTile(
    title: Text(activy),
    ),
    Padding(
    padding: EdgeInsets.all(8.0),
    child: Text(
    activityDesc,
    style:
    TextStyle(fontSize: 5 * SizeConfig.blockSizeHorizontal),
    )),
    Container(
    width: 200,
    height: 160,
    child: Image.asset('assets/camping.png', fit: BoxFit.fill),
    )
    ],
    ),
    ),
    );
    }
  • 按下按钮时将addWidget的值设置为“true”
  • 添加了一个简单的if语句,条件是addWidget:
  • if(addWidget)
    Container(
    child: createActivityCard('Added by user', 'WE DID IT'))
    ```

    关于flutter - 按下按钮后添加卡片小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64451026/

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