Flutter ListView.builder not working in alert dialog - flutter

I want to display item list in alert dialog, for payment confirmation.
but it's not working, i tried many solutions.
AlertDialog(
titlePadding: EdgeInsets.all(0),
contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
title: Text("Heading"),
content: ListView.builder(
shrinkWrap: true,
itemCount: items.length,
itemBuilder: (ctx, i) {
return getRowWidget(i);
}),
);

This will solve your problem, Add Container with width.
showDialog(
context: context,
builder: (context) => AlertDialog(
titlePadding: EdgeInsets.all(0),
contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
title: Text("Heading"),
content: Container(
width: double.maxFinite,
child: ListView.builder(
shrinkWrap: true,
itemCount: items.length,
itemBuilder: (ctx, i) {
return Text(items[i].toString());
}),
),
),
);

Use this code.
showDialog(
context: context,
builder: (context) => AlertDialog(
titlePadding: EdgeInsets.all(0),
contentPadding:
EdgeInsets.symmetric(horizontal: 10, vertical: 0),
title: Text("Heading"),
content: Container(
height: 100,
child: ListView.builder(
shrinkWrap: true,
itemCount: 1,
itemBuilder: (ctx, i) {
return Text("text");
}),
),
),
);

Related

SingleChildScrollView with ListView in a showModalBottomSheet [duplicate]

This question already has answers here:
How to Move bottomsheet along with keyboard which has textfield(autofocused is true)?
(24 answers)
Closed 6 months ago.
I am using a SingleChildScrollView with a ListView in a showModalBottomSheet but the keyboard is hidding the field when the user is filling it.
showModalBottomSheet(
context: context,
isScrollControlled: true,
enableDrag: true,
isDismissible: false,
builder: (context) {
return SafeArea(
child: Container(
decoration: ...
child:Column(
children:[
Text("...),
Expanded(
child: SingleChildScrollView(
child: ListView.separated(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: list.length,
itemBuilder: (BuildContext context, int index) {
return TextField(...)
]);
},
separatorBuilder: (BuildContext context, int index) => const Divider(),
),
),
),
Button(...),
]),
));
}
Wrap your modal builder with SingleChildScrollView and add padding to it like this:
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom),
full example:
showModalBottomSheet(
context: context,
isScrollControlled: true,
enableDrag: true,
isDismissible: false,
builder: (context) {
return SingleChildScrollView(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom),
child: Container(
decoration: BoxDecoration(
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(8.0),
topRight: const Radius.circular(8.0)),
),
padding: EdgeInsets.only(left: 16, right: 16, bottom: 16),
child: Column(
children: [
ListView.separated(
// padding: EdgeInsets.zero,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: list.length,
itemBuilder: (BuildContext context, int index) {
return TextField(
decoration: InputDecoration(hintText: 'text'),
);
},
separatorBuilder: (BuildContext context, int index) =>
const Divider(),
),
ElevatedButton(
onPressed: () {
_showBottom(context);
},
child: Text('button')),
],
),
),
);
})
when keyboard is close:
when it is open:
You can add the SingleChildScrollView up right after the safearea widget.
Now, you will get an error because the children does not have a boundary height.
You can use the height of the screen to set the container height.
Then add padding to the SingleChildScrollView matching the keyboard height
Showing lines with significant changes below
showModalBottomSheet(
context: context,
isScrollControlled: true,
enableDrag: true,
isDismissible: false,
builder: (context) {
return SafeArea(
child: SingleChildScrollView(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom),
child: Container(
height: MediaQuery.of(context).size.height,
child: Column(children: [
TextField(),
Expanded(
child: ListView.separated(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: 10,
itemBuilder: (BuildContext context, int index) {
return TextField();
},
separatorBuilder:
(BuildContext context, int index) =>
const Divider(),
),
),
TextField(),
]),
),
),
);
});

How to set null case in Gridview builder

I am trying to create a container and write no data found in case the data coming from the API is null.
Although I deleted the link of the API, the container does not appear.
body: Obx(
() => fGames2SecScreenLoad.isLoading.value
? const Center(
child: LoadingWidget(),
)
: GridView.builder(
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio: (itemWidth / itemHeight),
crossAxisCount: 1,
),
scrollDirection: Axis.vertical,
itemCount: freeGameTypeController.freeGames.length,
itemBuilder: (BuildContext context, int index) {
var freeGames = freeGameTypeController.freeGames[index];
if(freeGamesController.freeGamesList.isEmpty){
return Container(chid: Text("No data found");}
else{
return Padding(
padding: EdgeInsets.symmetric(
horizontal: 7.w,
vertical: 8.h,
),
child: GestureDetector(
onTap: () {
Get.to(
const FreeGamesIndex(),
transition: Transition.native,
duration: const Duration(milliseconds: 200),
arguments: freeGames,
);
},
child: FreeGamesListCard(
fImage: freeGames.image,
fTitle: freeGames.title,
fPlatforms: freeGames.platforms,
fWorth: freeGames.worth,
),
),
);
}}),
),
Try this:
body: Obx(
() => fGames2SecScreenLoad.isLoading.value
? const Center(
child: LoadingWidget(),
)
: Builder(
builder: (_){
if(freeGamesController.freeGamesList.isEmpty){
return Container(chid: Text("No data found");
}
return GridView.builder(
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio: (itemWidth / itemHeight),
crossAxisCount: 1,
),
scrollDirection: Axis.vertical,
itemCount: freeGameTypeController.freeGames.length,
itemBuilder: (BuildContext context, int index) {
var freeGames = freeGameTypeController.freeGames[index];
return Padding(
padding: EdgeInsets.symmetric(
horizontal: 7.w,
vertical: 8.h,
),
child: GestureDetector(
onTap: () {
Get.to(
const FreeGamesIndex(),
transition: Transition.native,
duration: const Duration(milliseconds: 200),
arguments: freeGames,
);
},
child: FreeGamesListCard(
fImage: freeGames.image,
fTitle: freeGames.title,
fPlatforms: freeGames.platforms,
fWorth: freeGames.worth,
),
),
);
}),
),
}),
),

ListView.builder() builds all items in an ExpansionTile

In a list of expansion tile widgets, I'm trying to build a list (200+ items) when any expansion tile is opened. When i open the expansion tile it builds all the 200 items at once, giving a jank. I'm using Listview.builder so I'm not expecting this behavior. Here's a code snippet.
ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: 100,
itemBuilder: (context, i) {
final _expansionTileKey = GlobalKey();
return ExpansionTile(
initiallyExpanded: false,
textColor: Colors.black,
key: _expansionTileKey,
onExpansionChanged: (value) =>
widget.onExpansionChanged(_expansionTileKey),
trailing: Icon(
Icons.keyboard_arrow_down,
),
subtitle: Text(
"$i - $i",
),
title: Text(
'$i',
),
children: <Widget>[
StreamBuilder<List<T>>(
stream: _bloc.stream,
builder: (context, snapshot) {
return ListView.separated(
shrinkWrap: true,
itemCount: _bloc.length,
itemBuilder: (_, index) {
return Container(
color: Colors.white,
child: Text("$index"),
);
},
separatorBuilder: (_, i) => Padding(
padding: const EdgeInsets.only(left: 8, right: 8),
child: const Divider(
height: 1,
),
),
);
},
)
],
);
},
),

Flutter: How to delete the Listview item onClick on icon

I am displaying the records on ListView using ListView.builder from api response data, i want to delete the list item when user click on icon, here is the code
Container(
child:SingleChildScrollView(
scrollDirection: Axis.vertical,
child: FutureBuilder(
future: _getRecord(), //getting data from this method
builder: (BuildContext context,
AsyncSnapshot<List<History>> snapshot) {
if (snapshot.hasData) {
if(historyList.length!=0){
return Container(
padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
child:Expanded(
child: ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return SingleChildScrollView(
scrollDirection: Axis.vertical,
child:Stack(
overflow: Overflow.visible,
children: <Widget>[
ListTile(
leading: CircleAvatar(
radius: 25,
backgroundColor:Color(int.parse(annualboxcolor))
child: Container(
padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
child:Column(children: <Widget>[
Text(dateObj.day.toString()),
]
),
)),
title: Text(snapshot.data[index].username),
isThreeLine: true,
subtitle:
IconButton(icon:Icon(Icons.check_box,color: Colors.green,size: 30,),onPressed: (){})]) //i want to delete item on click on this icon
please help how to do this.
You can use below line in onTap event of ListTile Widget.
setState(() {
snapshot.data.removeAt(index);
});

clip padding while scrolling on list view in flutter

I have used clipToPadding in android to clip the padding while scrolling horizontally and vertically. Is there any specific attribute like clipToPadding in flutter? The behavior would look alike:
ListView.separated(
padding: EdgeInsets.all(16),
scrollDirection: Axis.horizontal,
separatorBuilder: (context, index) => SizedBox(
width: 8,
),
itemBuilder: (BuildContext context, int index) {
return Text(data[index].name);
},
itemCount: data.length,
)
I'm not a native android dev, but if I did understand right the "clipToPadding" option, you just want to remove padding from the ListView and add padding around it.
Container(
height: 100.0,
padding: EdgeInsets.symmetric(horizontal: 16.0),
child: ListView.separated(
scrollDirection: Axis.horizontal,
separatorBuilder: (context, index) => SizedBox(
width: 8,
),
itemBuilder: (BuildContext context, int index) {
return AspectRatio(
aspectRatio: 1.0,
child: Container(
color: Colors.grey[300],
child: Center(child: Text(index.toString())),
),
);
},
itemCount: 20,
),
),
OR using Container (it's just to set max height for the listview) + separate Padding:
Container(
height: 100.0,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 16.0),
child: ListView.separated(
scrollDirection: Axis.horizontal,
separatorBuilder: (context, index) => SizedBox(
width: 8,
),
itemBuilder: (BuildContext context, int index) {
return AspectRatio(
aspectRatio: 1.0,
child: Container(
color: Colors.grey[300],
child: Center(child: Text(index.toString())),
),
);
},
itemCount: 20,
),
),
),