I want to make multiple Text Button in one code. I make this code but it requires a widget. How can i resolve this problem?
Initialize Block
imageButtonInit(int index) {
AppImage appImage;
for (int i = 0; i < index; i++) {
if (i < index) {
appImage = images[i];
imageButtonMaker(appImage);
} else {
break;
}
}
}
imageButtonMaker(AppImage appImage) {
return Container(
child: TextButton(
onPressed: () {},
child: Column(children: [
Image.asset(
appImage.image.toString(),
cacheHeight: 100,
cacheWidth: 100,
),
Text(
appImage.title.toString(),
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
)
])),
);
}
Main Block
Column menuBuilder(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Menu 1",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
imageButtonInit(3), //ERROR HERE it wants to widgets but mine is null type
],
),
),
.
.
.
Changing initialize block to List TextButton but still same problem in Main block.
2
Answers
It’s better you use declarative programming instead of imperative programming.
It’s possible you did not use
StatelessWidget
orStatefulWidget
.I think that zex_rectooor’s proposal is the most suitable, but still if you don’t want to change your code too much you can try the following:
With this solution you will return widgets, so the error should disappear, and the imageButtonInit method will no longer be needed so you can remove it.
Hope it helps-