I have GridView.builder
and have ElevatedButton
in my grid, when I press my button, button click event showing for all my grid tiles:
How can I show this loader effect only on the button that is pressed?
My code:
void _onSubmit() {
if (Constants.USER_TOKEN != null && Constants.USER_TOKEN != '') {
print('NO');
} else {
showDialog<void>(
context: context,
builder: (BuildContext dialogContext) {
return SimpleDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0)),
title: Text(
'Пройдите регистрацию, чтобы добавлять в корзину',
textAlign: TextAlign.center,
),
children: <Widget>[
TextButton(
child: Text('Пройти регистрацию'),
onPressed: () {
Navigator.of(dialogContext).pop(); // Dismiss alert dialog
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => MainScreen(currentIndex: 3)),
(route) => false);
},
style: ButtonStyle(alignment: Alignment.center),
),
],
);
},
);
}
setState(() => _isLoading = true);
Future.delayed(
const Duration(seconds: 2),
() => setState(() => _isLoading = false),
);
}
ElevatedButton.icon(
icon: _isLoading
? Container(
width: 24,
height: 24,
padding: const EdgeInsets.all(2.0),
child: const CircularProgressIndicator(
color: Colors.white,
strokeWidth: 3,
),
)
: const Icon(Icons.shopping_basket),
onPressed: () => _onSubmit(),
2
Answers
You need to define new variable like this:
then pass the item index to your item and use it Like this:
also remember to reset the
selectedIndex
after loading finish:Thanks to @Ivo, if you want to have more than one loading at the same time you need this approach:
first define the list of bool like this:
when your items list get ready fill this list with default value like this:
then you need update this list in your Item like this:
then in your
_onSubmit
do this:Another solution that allows having multiple loading items at the same time is to keep track which indexes are loading. Instead of having a boolean
_isLoading_
we define it as a Set like this:Then edit you
_onSubmit
to also pass an index likeand at the end of it
And finally at your button instead of
you write
and also