How to pass parameter in callback function in flutter

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AddUserPage extends StatefulWidget {
@override
_AddUserPageState createState() => _AddUserPageState();
}

class ListItem {
int value;
String name;

ListItem(this.value, this.name);
}

class Users {
int id;
String name;
Users(this.id, this.name);
}

class _AddUserPageState extends State<AddUserPage> {
List<Users> _listUsers = List<Users>();
Users _users1, _users2, _users3, _users4, _users5;
TextEditingController _nameCtrler = new TextEditingController();
TextEditingController _customerCtrlr = new TextEditingController();

@override
void initState() {
super.initState();
_users1 = Users(1, "Mark");
_users2 = Users(2, "Alice");
_users3 = Users(3, "John");
_users4 = Users(4, "Rachel");
_users5 = Users(5, "Michel");
_listUsers.add(_users1);
_listUsers.add(_users2);
_listUsers.add(_users3);
_listUsers.add(_users4);
_listUsers.add(_users5);
}

Future<String> _showDialogUsers() async {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: SingleChildScrollView(
child: new MyDialog(
listInDialog: _listUsers,
setData: (Users listSearch) {
print("-----setdata" + listSearch.name.toString());
_nameCtrler.text = listSearch.name;
setState(() {});
}),
));
});
}

@override
Widget build(BuildContext context) {
return new WillPopScope(
child: Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
backgroundColor: Colors.indigoAccent,
title: Text(
"Add User Page",
),
),
body: SingleChildScrollView(
child: Container(
color: Colors.white,
padding: EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
child: Container(
padding:
EdgeInsets.only(left:10.0,right:10.0),
decoration: BoxDecoration(
borderRadius:BorderRadius.circular(10.0),
border: Border.all()),
child: InkWell(
child: TextField(
decoration: InputDecoration(
suffixIcon: IconButton(
icon: Icon(
Icons.arrow_drop_down,
color: Colors.grey,
),
),
labelText: 'Select User',
),
controller: _customerCtrlr,
enabled: false,
),
onTap: () {
if (_listUsers != null &&
_listUsers.length > 0) {
print('----length of customers---' +
_listUsers.length.toString());
_showDialogUsers();
}
},
)),
),
Container(
margin: EdgeInsets.only(left: 10, right: 10),
child: Column(
children: [
SizedBox(
height: 16,
),
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Name',
),
controller: _nameCtrler,
)
],
),
)
],
),
),
)));
}
}
class MyDialog extends StatefulWidget {
MyDialog({
Key key,
this.listInDialog,
this.setData,
}) : super(key: key);

final List<Users> listInDialog;
final Function setData;

@override
_MyDialogState createState() => new _MyDialogState();
}

class _MyDialogState extends State<MyDialog> {
int _selectedIndex = 0;
final TextEditingController _searchQuery = TextEditingController();
TextEditingController searchController = TextEditingController();
String filter;

@override
void initState() {
super.initState();
searchController.addListener(() {
setState(() {
filter = searchController.text;
});
});
}

_getContent() {
if (widget.listInDialog.length == 0) {
return new Container();
}

return Container(
width: double.maxFinite,
child: Column(
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: TextField(
controller: searchController,
decoration: InputDecoration(
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0)),
labelText: "Search User",
contentPadding: EdgeInsets.all(10),
),
),
),
Container(
constraints: BoxConstraints(minHeight: 400,
minWidth: double.infinity, maxHeight: 400),
child: ListView.builder(
shrinkWrap: true,
itemCount: widget.listInDialog.length,
itemBuilder: (context, index) {
// if filter is null or empty returns all data
return filter == null || filter == ""
? Container(
margin: EdgeInsets.only(left: 10, right:10),
child: ListTile(
onTap: () => _onTapItem(context,
widget.listInDialog[index]),
title: Text(
widget.listInDialog[index]
.name.toString(),
)),
)
: widget.listInDialog[index].name
.toString()
.toLowerCase()
.contains(filter.toLowerCase())
? Container(
margin:EdgeInsets.only(left:10,right:10),
child: ListTile(
onTap: () => _onTapItem(context,
widget.listInDialog[index]),
title: Text(widget.listInDialog[index]
.name.toString(),
),
),
)
: Container();
},
),
)
],
),
);
}

@override
Widget build(BuildContext context) {
return _getContent();
}

_onTapItem(BuildContext context, Users listSearch) {
print('listSearch----------- ' + listSearch.name);
widget.setData(listSearch);
Navigator.pop(context);
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store