I am trying to make ListView.builder as an alertdialog content...but it is showin RENDER errror...and when I do the same with scaffold body..it works ...
I have made a method named Mycontent... If I apply this MyContent to scaffold body, it works but it does not work with alert content....this is what making me confuse...between alert content and scaffold body
here is my code...
import 'package:flutter/material.dart';
class HomeScreen extends StatefulWidget {
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String result = '';
List <String> names=['A','B','C','D'];
@override
void initState() {
// TODO: implement initState
super.initState();
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(result),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
onPressed: () async {
final result = await openDialog();
if (result == null || result.isEmpty) return;
setState(() {
this.result = result;
});
},
child: Text('Select Color'),
),
],
),
),
);
}
Future<String?> openDialog() {
return showDialog<String>(
context: context,
builder: (context) => AlertDialog(
title: Text('Select Your Name'),
content: MyContent(),
actions: [
TextButton(
onPressed: () {
},
child: Text('Submit'))
],
));
}
MyContent() {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: ListView.builder(
itemCount: names.length,
itemBuilder: (context,index){
return Text(names[index]);
}),
)
],
);
}
}
CodePudding user response:
Try to wrap your ListView inside SizedBox or Container and give it to specific heifgt and width, add shrinkWrap: true,
inside ListView.builder()
, dont use RaisedButton
use