'passing data immediately without using setstate
Is there a way to pass data when from alert dialogue box to the same screen immediately without using setstate?
Widget setupShadeColorContainer(
List<ShadeColorDatabase> allShadeData, BuildContext context) {
return SizedBox(
height: 300.0, // Change as per your requirement
width: 300.0, // Change as per your requirement
child: GridView.builder(
shrinkWrap: true,
itemCount: allShadeData.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4, crossAxisSpacing: 10, mainAxisSpacing: 10),
itemBuilder: (ctx, i) {
return GestureDetector(
onTap: () {
rPassedChooseColor = allShadeData[i].rValue;
gPassedChooseColor = allShadeData[i].gValue;
bPassedChooseColor = allShadeData[i].bValue;
setState(() {
Navigator.pop(context, [
rPassedChooseColor,
gPassedChooseColor,
bPassedChooseColor
]);
});
},
child: Container(
child: Stack(
children: [
Container(
color: Color.fromRGBO(
allShadeData[i].rValue!.toInt(),
allShadeData[i].gValue!.toInt(),
allShadeData[i].bValue!.toInt(),
1),
),
Padding(
padding: const EdgeInsets.only(top: 45, left: 5),
child: Text("${allShadeData[i].colorCode}"),
)
],
),
),
);
}),
);
}
showAllColors(
List<ShadeColorDatabase> shadeData, BuildContext context) async {
final size = MediaQuery.of(context).size;
final GlobalKey<FormState> _form = GlobalKey<FormState>();
TextEditingController searchController = TextEditingController();
showDialog(
barrierDismissible: true,
context: context,
builder: (ctx) {
return WillPopScope(
onWillPop: () async => false,
child: AlertDialog(
title: Center(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Fashion's Color",
style: TextStyle(
color: ChooseColor(0).appBarColor1, fontSize: 14),
),
IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: const Icon(Icons.clear))
],
),
Form(
key: _form,
child: Column(
children: [
TextFormField(
decoration: InputDecoration(
border: const OutlineInputBorder(
borderSide: BorderSide.none,
),
contentPadding: EdgeInsets.symmetric(
vertical: size.height * 0.001,
horizontal: size.width * 0.030),
errorBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.red, width: 1),
borderRadius: BorderRadius.circular(5)),
// labelText: 'Phone Number',
fillColor: const Color(0xffF6F9FA),
filled: true,
hintText: 'Search Color',
prefixIcon: const Icon(Icons.search),
hintStyle: TextStyle(
fontSize: size.height * 0.012 +
size.width * 0.012,
color: Colors.black26),
),
controller: searchController,
),
SizedBox(height: size.height * 0.035),
],
)),
],
),
),
content: setupShadeColorContainer(shadeData, context),
),
);
});
This is my dialogue box this dilogue box open over a screen and i want to pass data from this dilogue box to the same page immediately without using setstate is there any way i can achieve that?. Thanks
Solution 1:[1]
showDialog() can return future, and you can return data(myData) on closing dialog using
Navigator.of(context).pop(myData);
showAllColors() async {
final data = await showDialog(context: context, builder: (c){
// on closing dialog
Navigator.of(context).pop(passData);
return data;
}
When you use showAllColors try putting await on async method and also make sure to handle null data.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yeasin Sheikh |
