Reputation: 257
I am trying to open a floating container on Button click of ListView Items. List View items are in an external class, and I want to pass the int change
value to the main class so that I can call a floating Container in the Screen. I went through the threads here but I could only find answers where they say that I have to make the variable value final. But I can't since the change
value changes to change = 1
in the external class I mentioned above.
Here is the code.
class ListTileClass extends StatefulWidget {
final Map<String, String> jsonObject;
ListTileClass({this.jsonObject});
@override
_ListTileClassState createState() => _ListTileClassState();
}
class _ListTileClassState extends State<ListTileClass> {
int change = 0;
@override
Widget build(BuildContext context) {
return Stack(
children: [
ListTile(
title: Container(
child: Column(
children: [
Row(
children: makeWidgetChildren(widget.jsonObject),
),
],
),
),
),
],
);
}
List<Widget> makeWidgetChildren(jsonObject) {
List<Widget> children = [];
bool _count = true;
jsonObject.keys.forEach(
(key) => {
setState(() {
if (_count == true) {
children.add(
GestureDetector(
onTap: () {
setState(() {
if (change == 0) {
change = 1;
} else {
change = 0;
}
});
},
child: Container(...),
),
);
_count = false;
}
}),
},
);
return children;
}
}
I want to send this Changed change
value to the main class. I highly appreciate your help!
Upvotes: 0
Views: 359
Reputation: 450
use can pass a callback function btw
class ListTileClass extends StatefulWidget {
final function callback;
final Map<String, String> jsonObject;
ListTileClass({this.jsonObject, this.callback});
@override
_ListTileClassState createState() => _ListTileClassState();
}
class _ListTileClassState extends State<ListTileClass> {
@override
Widget build(BuildContext context) {
return Stack(
children: [
ListTile(
title: Container(
child: Column(
children: [
Row(
children: makeWidgetChildren(widget.jsonObject),
),
///this is a sample to use a callback don't mind if it different than your goal
InkWell(
onTap:(){
/// change value to your value, can be anything
callback(value);
}
),
],
),
),
),
],
);
}
class YourOtherClass extends StatefulWidget {
@override
_ListTileClassState createState() => _ListTileClassState();
}
class _ListTileClassState extends State<ListTileClass> {
yourVariable = 0;
yourFunction(int value){
setState((){
yourVariable = value;
});
}
@override
Widget build(BuildContext context) {
return ListTileClass(
jsonObject:yourjsonObject,
callback:yourFunction
);
}
}
btw istead of using foreach to build a widgets. you can use ListViewBuilder take a look at this https://medium.com/@DakshHub/flutter-displaying-dynamic-contents-using-listview-builder-f2cedb1a19fb
Upvotes: 1