我是 Flutter 的新手。我的查询是如何在单击按钮时在列表视图中动态添加/删除项目。我试过了,但我得到了
错误:RenderFlex 子级具有非零 flex 但传入的高度约束是无界的
我的代码
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 10),
child: SizedBox(
width: double.infinity,
height: 52,
child: RaisedButton(
//onPressed: ()
=>_onAlertWithCustomContentPressed(context),
onPressed:() =>call(),
child: Text(
"Add Article",
style: TextStyle(color: Colors.white, fontSize: 18),
),
color: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6))),
),
),
),
new Expanded(
child: new ListView.builder
(
itemCount: lItems.length,
itemBuilder: (BuildContext ctxt, int Index) {
return new Text(lItems[Index]);
}
)
)
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助解决这个问题。或者任何人都可以建议哪种方法最适合这项任务
提前致谢
萨蒂什
我知道为时已晚,但也许其他人正在寻找相同的答案,以下代码可以帮助他们。
使用 StatefullWidget 以便稍后更改状态,并使用 setState((){} //您的代码在这里
)
import 'package:flutter/material.dart';
class MyListView extends StatefulWidget {
@override
_MyListViewState createState() => _MyListViewState();
}
class _MyListViewState extends State<MyListView> {
final countries = [
'Pakistan',
'France',
'Spain',
'KSA',
'Brasil',
'Australia',
'UAE',
'USA',
'UK',
'India',
'Afghanistan',
'Bangladsh',
'Egypt',
'Pakistan',
'France',
'Spain',
'KSA',
'Brasil',
'Australia',
'UAE',
'USA',
'UK',
'India',
'Afghanistan',
'Bangladsh',
'Egypt'
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView.separated(
itemCount: countries.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(countries[index]),
);
},
separatorBuilder: (context, index){
return Divider();
},
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: addItem,
),
);
}
void addItem(){
setState(() {
countries.add(countries[0]);
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6613 次 |
| 最近记录: |