Flutter 如何在ListView Builder中添加加号按钮从Flatter中的SQLite获取数据
我有一个Flutter 如何在ListView Builder中添加加号按钮从Flatter中的SQLite获取数据,flutter,listview,sqflite,Flutter,Listview,Sqflite,我有一个列表视图生成器,其中有一个有状态小部件。我想在列表视图项中添加一个加号按钮作为小部件。我正在做的是,在创建数据库时,我在SQLite中添加了许多记录,并且在数据库中添加了一个加号图标和一个空的as记录/行。所以我从那里获取数据。加号按钮(小部件)按我的预期显示,但当将数据更新到数据库中时,按钮未显示在预期位置,因为我希望它显示在所有项目的末尾 class CategoriesScreen extends StatefulWidget { @override
列表视图生成器
,其中有一个有状态小部件
。我想在列表视图项
中添加一个加号按钮
作为小部件
。我正在做的是,在创建数据库时,我在SQLite中添加了许多记录,并且在数据库中添加了一个加号图标和一个空的as记录/行。所以我从那里获取数据。加号按钮(小部件)按我的预期显示,但当将数据更新到数据库中时,按钮
未显示在预期位置,因为我希望它显示在所有项目的末尾
class CategoriesScreen extends StatefulWidget {
@override
_CategoriesScreenState createState() => _CategoriesScreenState();
}
class _CategoriesScreenState extends State<CategoriesScreen> {
List<Map<String,dynamic>> spendingCategoriesList=[] ;
DatabaseHelper helper=DatabaseHelper();
@override
Widget build(BuildContext context) {
getData();
return Scaffold(
backgroundColor: Color(0xFF343641),
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height-100,
margin: EdgeInsets.only(top: 40.0),
child: Column(
children: <Widget>[
Container(
width:MediaQuery.of(context).size.width ,
height: 30.0,
margin: EdgeInsets.only(left: 20.0),
child: Row(
children: [
GestureDetector(
onTap:(){Navigator.pop(context);},
child: Icon(
Icons.close,
color: Color(0xFFE44663),
size: 24.0,
),
),
Padding(
padding: EdgeInsets.only(left:MediaQuery.of(context).size.width*0.3),
child: Text('Categories',style: TextStyle(color: Colors.white,fontWeight: FontWeight.normal,fontSize: 15.0,),),
),
],
),
),
Expanded(
child: GridView.builder(
itemCount: spendingCategoriesList.length,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3), itemBuilder: (context,index){
AddTransactionsModel transactionModel= new AddTransactionsModel(spendingCategoriesList[index]['CategoryImage'],spendingCategoriesList[index]['categoryName']);
return AddTransactionItem(transactionModel);
}),
),
],
),
),
);
}
void getData() async{
spendingCategoriesList=await helper.getNoteMapList();
setState(() {
spendingCategoriesList=spendingCategoriesList;
});
}
class CategoriesScreen扩展StatefulWidget{
@凌驾
_CategoriesScreenState createState()=>\u CategoriesScreenState();
}
类_CategoriesScreenState扩展状态{
List spendingCategoriesList=[];
DatabaseHelper=DatabaseHelper();
@凌驾
小部件构建(构建上下文){
getData();
返回脚手架(
背景颜色:颜色(0xFF343641),
主体:容器(
宽度:MediaQuery.of(context).size.width,
高度:MediaQuery.of(context).size.height-100,
边距:仅限边缘集(顶部:40.0),
子:列(
儿童:[
容器(
宽度:MediaQuery.of(context).size.width,
身高:30.0,
边距:仅限边缘设置(左:20.0),
孩子:排(
儿童:[
手势检测器(
onTap:(){Navigator.pop(上下文);},
子:图标(
图标。关闭,
颜色:颜色(0xFFE44663),
尺寸:24.0,
),
),
填充物(
填充:EdgeInsets.only(左:MediaQuery.of(context.size.width*0.3),
子项:文本('Categories',样式:TextStyle(颜色:Colors.white,fontWeight:fontWeight.normal,fontSize:15.0,),),
),
],
),
),
扩大(
子项:GridView.builder(
itemCount:spendingCategoriesList.length,
gridDelegate:new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:3),itemBuilder:(上下文,索引){
AddTransactionModel transactionModel=新的AddTransactionModel(spendingCategoriesList[index]['CategoryImage'],spendingCategoriesList[index]['categoryName']);
return AddTransactionItem(transactionModel);
}),
),
],
),
),
);
}
void getData()异步{
spendingCategoriesList=Wait helper.getNoteMapList();
设置状态(){
spendingCategoriesList=spendingCategoriesList;
});
}
这里是AddTransactionGridItem
class _AddTransactionItemState extends State<AddTransactionItem> {
var imageSelectIndex;
String categoryName;
String categoryImage;
@override
Widget build(BuildContext context) {
print(
'Hello dear ! icon Number ${widget.transactionModel.iconNumber} and here is Text ${widget.transactionModel.categoryNumber} ');
return Container(
child: Column(
children: <Widget>[
InkWell(
onTap: widget.transactionModel.categoryNumber == ''
? () {
showAddCategoryDialogue();
}
: () {
setState(() {
categoryName=widget.transactionModel.categoryNumber;
categoryImage=widget.transactionModel.iconNumber;
print('categoryName $categoryName And categoryImage $categoryImage');
String cate='$categoryName&$categoryImage';
Navigator.pop(context,cate);
});
},
child: Container(
width: 72.0,
height: 72.0,
decoration: BoxDecoration(
color: Color(0xffC4C4C4).withOpacity(0.2),
borderRadius: BorderRadius.all(Radius.circular(12.0)),
border: widget.transactionModel.categoryNumber == ''
? Border.all(width: 0.3, color: Colors.white)
: Border.all(width: 0.0),
),
child: Center(
child: Image.asset('${widget.transactionModel.iconNumber}')),
),
),
SizedBox(
height: 10.0,
),
Text(
widget.transactionModel.categoryNumber,
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.w600,
fontFamily: 'Rajdhani',
fontStyle: FontStyle.normal),
)
],
),
);
class\u AddTransactionItemState扩展状态{
var指数;
字符串类别名称;
字符串分类图像;
@凌驾
小部件构建(构建上下文){
印刷品(
'您好,亲爱的!图标编号${widget.transactionModel.iconNumber},这里是文本${widget.transactionModel.categoryNumber}';
返回容器(
子:列(
儿童:[
墨水池(
onTap:widget.transactionModel.categoryNumber==“”
? () {
showAddCategoryDialogue();
}
: () {
设置状态(){
categoryName=widget.transactionModel.categoryNumber;
categoryImage=widget.transactionModel.iconNumber;
打印('categoryName$categoryName和categoryImage$categoryImage');
字符串cate='$categoryName&$categoryImage';
pop(上下文,cate);
});
},
子:容器(
宽度:72.0,
身高:72.0,
装饰:盒子装饰(
颜色:颜色(0xFFC4C4)。不透明度(0.2),
borderRadius:borderRadius.all(半径.圆形(12.0)),
边框:widget.transactionModel.categoryNumber==“”
?边框。全部(宽度:0.3,颜色:颜色。白色)
:边框。全部(宽度:0.0),
),
儿童:中心(
子项:Image.asset(“${widget.transactionModel.iconNumber}”),
),
),
大小盒子(
身高:10.0,
),
正文(
widget.transactionModel.categoryNumber,
样式:TextStyle(
颜色:颜色,白色,
字体大小:16.0,
fontWeight:fontWeight.w600,
fontFamily:“Rajdhani”,
fontStyle:fontStyle.normal),
)
],
),
);
}
容器上方的
是ListView.Builder()Item
如果有人能帮助我,我将不胜感激。您能分享一个关于如何填充ListView小部件并在列表末尾添加“加号”按钮的代码片段吗?共享的代码片段仅用于ListView项目的容器。我很好奇ListView是如何填充的。您是否尝试过填充ListView wid获取静态数据并查看是否显示了“加号”按钮?如果有效,则使用SQLite数据填充ListView小部件应该不会有问题,除非有其他原因导致此行为。我已编辑了我的问题