How to Create Listview Inside Dialog

Flutter: ListView in a SimpleDialog

Just wrap ListView.builder in a Container with a specific height and width.

Widget setupAlertDialoadContainer() {
return Container(
height: 300.0, // Change as per your requirement
width: 300.0, // Change as per your requirement
child: ListView.builder(
shrinkWrap: true,
itemCount: 5,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('Gujarat, India'),
);
},
),
);
}

And call the above method in showDialog.

showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Country List'),
content: setupAlertDialoadContainer(),
);
});

EDITED:

You can go with @Rap's comment too.

is it possible to create listview inside dialog?

You don't really have to extend listActivity in order to use listviews.

Extending listActivity will give you some functionality for free, such as getListView() (if I recall the method name correctly), but that can just as well be done manually with findViewById() just as any other view.

How can I display a list view in an Android Alert Dialog?

Used below code to display custom list in AlertDialog

AlertDialog.Builder builderSingle = new AlertDialog.Builder(DialogActivity.this);
builderSingle.setIcon(R.drawable.ic_launcher);
builderSingle.setTitle("Select One Name:-");

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(DialogActivity.this, android.R.layout.select_dialog_singlechoice);
arrayAdapter.add("Hardik");
arrayAdapter.add("Archit");
arrayAdapter.add("Jignesh");
arrayAdapter.add("Umang");
arrayAdapter.add("Gatti");

builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String strName = arrayAdapter.getItem(which);
AlertDialog.Builder builderInner = new AlertDialog.Builder(DialogActivity.this);
builderInner.setMessage(strName);
builderInner.setTitle("Your Selected Item is");
builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss();
}
});
builderInner.show();
}
});
builderSingle.show();

Flutter AlertDialog with ListView and bottom TextField

You can copy paste run full code below

You can in content use SingleChildScrollView

code snippet

 content: SingleChildScrollView(
child: Container(
width: double.maxFinite,

working demo

Sample Image

full code

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class Exercise {
String name;
Exercise({this.name});
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
List<Exercise> exercises = [
Exercise(name: 'A'),
Exercise(name: 'B'),
Exercise(name: 'C'),
Exercise(name: 'D'),
Exercise(name: 'E'),
Exercise(name: 'F'),
Exercise(name: 'G')
];
int _selected;
void _incrementCounter() {
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(widget.title),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20))),
actions: <Widget>[
FlatButton(
child: const Text('CANCEL'),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
textColor: Theme.of(context).accentColor,
onPressed: () {
//widget.onCancel();
},
),
FlatButton(
child: const Text('OK'),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
textColor: Theme.of(context).accentColor,
onPressed: () {
//widget.onOk();
},
),
],
content: SingleChildScrollView(
child: Container(
width: double.maxFinite,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Divider(),
ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.of(context).size.height * 0.4,
),
child: ListView.builder(
shrinkWrap: true,
itemCount: exercises.length,
itemBuilder: (BuildContext context, int index) {
return RadioListTile(
title: Text(exercises[index].name),
value: index,
groupValue: _selected,
onChanged: (value) {
setState(() {
_selected = index;
});
});
}),
),
Divider(),
TextField(
autofocus: false,
maxLines: 1,
style: TextStyle(fontSize: 18),
decoration: new InputDecoration(
border: InputBorder.none,
hintText: "hint",
),
),
],
),
),
),
);
}
}

ListView.builder doesn't work in alertDialog

As can be seen from error it is assertion error of alert dialog. Element in alert dialog must be specific width, which you can see in log. it change based on device.

So to solve your error you have to provide specific width of container, which is above list view. To be more specific it is because your listview is horizontal and set width to infinity and that’s why it is throwing assertion error. If is was vertical listview then you have to provide height of container.

@override
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width*0.75 // here i set width of container to 75% of screen
child: ListView.builder(
scrollDirection: Axis.horizontal,

Add item to ListView inside SimpleDialog in Flutter

demo

SimpleDialog is already built so when you change the list/map it does change the values of variable but bot refected. Trying adding print to observe the values.
Basically to redraw the SimpleDialog just pop SimpleDialog call again. Like this

class TagScreen extends StatefulWidget {
@override
_TagScreenState createState() => _TagScreenState();
}

class _TagScreenState extends State<TagScreen> {
Map<IconData, Color> tagMap = {
Icons.work: Colors.blue[900],
Icons.beach_access: Colors.lightGreen,
Icons.local_dining: Colors.lightGreen,
Icons.supervised_user_circle: Colors.green,
Icons.people: Colors.green,
Icons.favorite_border: Colors.green,
Icons.directions_run: Colors.deepOrange,
Icons.directions_car: Colors.blueGrey,
Icons.hotel: Colors.deepPurpleAccent,
};
var tagIconList = [];
var tagColorList = [];

int _selectedIndex = 0;

@override
void initState() {
super.initState();
initData();
}

initData() {
tagIconList = tagMap.keys.toList();
tagColorList = tagMap.values.toList();
}

List<String> tagNames = [
'Work',
'Relax',
'Eating',
'Family',
'Friends',
'Relationship',
'Sport',
'Transport',
'Sleep'
];

_addTagOption() async {
setState(() {
tagMap.putIfAbsent(Icons.laptop, () => Colors.deepOrange);
tagNames.add('Challenge');
initData();
});
Navigator.of(context).pop();
_showDialog();
}

void _showDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10.0))),
children: <Widget>[
Container(
width: double.maxFinite,
child: ListView.builder(
itemCount: tagMap.length,
shrinkWrap: true,
itemBuilder: (BuildContext context, int itemCount) {
return SimpleDialogOption(
onPressed: () {
setState(() {
_selectedIndex = itemCount;
});
Navigator.of(context).pop();
},
child: Row(
children: <Widget>[
Icon(
tagIconList[itemCount],
color: tagColorList[itemCount],
),
Padding(padding: EdgeInsets.only(left: 25), child: Text(tagNames[itemCount]))
],
));
})),
SimpleDialogOption(
onPressed: _addTagOption,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.add,
size: 25,
color: Color(0x8A000000),
),
Padding(
padding: EdgeInsets.only(left: 5),
child: Text(
'ADD NEW',
style: TextStyle(color: Color(0x8A000000)),
))
],
),
)
]);
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"TAGS",
style: TextStyle(color: Colors.white),
),
),
body: Column(
children: <Widget>[
Text("Selected Tag"),
SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
tagIconList[_selectedIndex],
color: tagColorList[_selectedIndex],
),
Padding(padding: EdgeInsets.only(left: 25), child: Text(tagNames[_selectedIndex]))
],
),
SizedBox(
height: 20,
),
RaisedButton(
onPressed: _showDialog,
child: Text("Select TAG"),
)
],
),
);
}
}

Hope it helps :)



Related Topics



Leave a reply



Submit