flutter listview with radio not showing in alertDialog
AlertDialog uses an IntrinsicWidth
widget that doesn't allow ListView.builder
. You have to give a specific width to your ListView
Example:
return AlertDialog(
title: Text('Dialog'),
content: SizedBox(
width: double.maxFinite,
child: ListView(
children: <Widget>[
//Your content here
],
),
),
);
KUMAR PANDULE
Updated on December 18, 2022Comments
-
KUMAR PANDULE 5 minutes
This is the code. code:
class ThemeChangerWidget extends StatelessWidget { final List<String> string = ['Light', 'Dark', 'Amoled']; @override Widget build(BuildContext context) { final stateData = Provider.of<ThemeNotifier>(context); final ThemeData state = stateData.getTheme(); return Theme( data: state.copyWith(unselectedWidgetColor: state.accentColor), child: AlertDialog( backgroundColor: state.primaryColor, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.0)), title: Text('Select Theme', style: state.textTheme.body1), content: ListView.builder( shrinkWrap: true, itemBuilder: (context, index) { return RadioListTile( value: index, groupValue: themes.indexOf(state), onChanged: (ind) { onThemeChanged(ind, stateData); }, title: Text( string[index], style: state.textTheme.body2, ), ); }, itemCount: string.length, )), ); } }'
errors-The following assertion was thrown during performLayout():
RenderShrinkWrappingViewport does not support returning intrinsic dimensions.
some times throw this error instead of above
LayoutBuilder does not support returning intrinsic dimensions.
Calculating the intrinsic dimensions would require instantiating every child of the viewport, which defeats the point of viewports being lazy. If you are merely trying to shrink-wrap the viewport in the main axis direction, you should be able to achieve that effect by just giving the viewport loose constraints, without needing to measure its intrinsic dimensions.
-
Amir over 2 yearsthis solution also work when you got this error
LayoutBuilder does not support returning intrinsic dimensions.
-
1housand about 1 yearSizedBox(width: double.maxFinite, child: ...) works also and might be better since container has a lot of extra bells and whistles you don't need. i'm happy i found a solution, but why does this work? and is there a more elegant solution?
-
CoderUni about 1 yearThanks for the heads up @1housand .
SizedBox
is a better way of doing this. I wasn't as experienced at that time so I usedContainer
. I've updated the answer. This works becauseListView
takes as much width as it possibly can andIntristicWidth
sizes itself to have the same width as its child. Therefore, you have to constrain the width of the IntristicWidth's child.