I want a text button when on click shows a simpleDialog vith a listView.builder but I don't know how to code it. I always have an error. Can you help me?
Here is my code:
TextButton(
child: const Text('Selet instruments needed'),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) => SimpleDialog(
contentPadding: const EdgeInsets.all(15),
title: const Text('Select instruments needed'),
children: [
ListView.builder(
shrinkWrap: true,
itemCount: 2,
itemBuilder: ((context, index) {
return ListTile(
title: instrumentType[index]['name'],
onTap: () {});
}))
]));
})
CodePudding user response:
You can wrap your ListView
with SizedBox
widget and using LayoutBuilder
help to get the constraints
TextButton(
child: const Text('Selet instruments needed'),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) => LayoutBuilder(
builder: (context, constraints) => SimpleDialog(
contentPadding: const EdgeInsets.all(15),
title: const Text('Select instruments needed'),
children: [
SizedBox(
height: constraints.maxHeight * .7, // 70% height
width: constraints.maxWidth * .9,
child: ListView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: 44,
itemBuilder: ((context, index) {
return ListTile(onTap: () {});
}),
),
)
],
),
),
);
})
CodePudding user response:
Try below code :
void _show(BuildContext ctx) {
showDialog(
context: ctx,
builder: (_) {
return SimpleDialog(
title: const Text('The Title'),
children: [
SimpleDialogOption(
child: const Text('Option 1'),
onPressed: () {
// Do something
print('You have selected the option 1');
Navigator.of(ctx).pop();
},
),
SimpleDialogOption(
child: const Text('Option 2'),
onPressed: () {
// Do something
print('You have selected the option 2');
Navigator.of(ctx).pop();
},
)
],
);
},
);
}
Call Dialog function :
TextButton(
child: const Text('Show The Simple Dialog'),
onPressed: () => _show(context),
),