Search code examples
flutterdartflutter-alertdialogflutter-text

Dart/Flutter Add an Image to AlertDialog Text Field


I have an Application where I want Users to be able to buy Power Ups inside an Alert Dialog.

For the actual buying, the user simply presses the IconButton, which already works.

Is there a way to display the Images also in the Text above, instead of the actual text?

So for example the TNT image used in the Icon Button to also display in the text instead of the actual word "TNT"?

AlertDialog(
              title: const Text('Buy power up?'),
              content: Text(
                  'Do you want to buy \nTNT for $tntPrice\$ \nMine for $minePrice\$ \nWrapped for $wrappedPrice\$?'),
              elevation: 24,
              shape: const RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(16))),
              actions: <Widget>[
                IconButton(
                    icon: Image.asset('assets/images/bombs/tnt.png'),
                    onPressed: () => buyPowerUp("tnt", tntPrice, coinBloc,
                        reportingBloc, gameBloc, context)),
                IconButton(
                    icon: Image.asset('assets/images/bombs/mine.png'),
                    onPressed: () => buyPowerUp("mine", minePrice, coinBloc,
                        reportingBloc, gameBloc, context)),
                IconButton(
                    icon: Image.asset('assets/images/bombs/multi_color.png'),
                    onPressed: () => buyPowerUp("wrapped", wrappedPrice,
                        coinBloc, reportingBloc, gameBloc, context)),
                TextButton(
                  onPressed: () => buyPowerUp(
                      "nothing", 0, coinBloc, reportingBloc, gameBloc, context),
                  child: const Text('Start Game'),
                )
              ],
            ));

enter image description here

So basically that it looks like this(Sry for bad image, just used paint to make it clear)

enter image description here


Solution

  • Try this code:

      AlertDialog(
                  title: const Text('Buy power up?'),
                  content: Wrap(
                children: [
                  Text('Do you want to buy'),
                  Row(
                    children: [
                      Image.asset('assets/images/bombs/tnt.png'),
                      SizedBox(width: 5),
                      Text(
                          ' for $tntPrice\$'),
                    ],
                  ),
                  Row(
                    children: [
                      Image.asset('assets/images/bombs/mine.png'),
                      SizedBox(width: 5),
                      Text(
                          ' for $minePrice\$'),
                    ],
                  ),
                  Row(
                    children: [
                      Image.asset('assets/images/bombs/multi_color.png'),
                      SizedBox(width: 5),
                      Text(
                          ' for $wrappedPrice\$?'),
                    ],
                  ),
                ],
              )
                      elevation: 24,
                      shape: const RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(16))),
                      actions: <Widget>[
                        IconButton(
                            icon: Image.asset('assets/images/bombs/tnt.png'),
                            onPressed: () => buyPowerUp("tnt", tntPrice, coinBloc,
                                reportingBloc, gameBloc, context)),
                        IconButton(
                            icon: Image.asset('assets/images/bombs/mine.png'),
                            onPressed: () => buyPowerUp("mine", minePrice, coinBloc,
                                reportingBloc, gameBloc, context)),
                        IconButton(
                            icon: Image.asset('assets/images/bombs/multi_color.png'),
                            onPressed: () => buyPowerUp("wrapped", wrappedPrice,
                                coinBloc, reportingBloc, gameBloc, context)),
                        TextButton(
                          onPressed: () => buyPowerUp(
                              "nothing", 0, coinBloc, reportingBloc, gameBloc, context),
                          child: const Text('Start Game'),
                        )
                      ],
                    ));