Home > Mobile >  Flutter App is freezing after few button press
Flutter App is freezing after few button press

Time:12-16

I'm building a While of Fortune with the help of the flutter_fortune_wheel package. The value where the wheel stops are predecided as the animation starts. I'm generating a random number between 0 - 9 and to avoid the number 1 & 8 I'm using a while loop. But after a few spins the app freezes. I think while loops are the reason because when I comment while loop the app doesn't freeze. I'm not 100% sure

/// Instances
final _shopController = Get.find<ShopController>();

class WheelOfFortune extends StatefulWidget {
  @override
  _WheelOfFortuneState createState() => _WheelOfFortuneState();
}

class _WheelOfFortuneState extends State<WheelOfFortune> {
  //
  StreamController<int> _controller = StreamController<int>();

  // Spins Available
  int _availableSpins = 10;

  // SelectedIndex
  int _winningIndex = 0;

  // Disable Spin Button
  bool _disableSpinButton = false;

  // Reward Value List
  final _rewardList = [
    // #0 1k FC
    {
      'prize': 'FC',
      'value': 1000,
    },

    // #1 100 GB JACKPOT
    {
      'prize': 'GB',
      'value': 100,
    },

    // #2 1k FC
    {
      'prize': 'FC',
      'value': 5000,
    },

    // #3 2 GB
    {
      'prize': 'GB',
      'value': 2,
    },

    // #4 1 SPIN
    {
      'prize': 'Spin',
      'value': 1,
    },

    // #5 15k FC
    {
      'prize': 'FC',
      'value': 15000,
    },

    // #6 AVOCADO
    {
      'prize': 'Avocado',
      'value': 1000,
    },

    // #7 10 GB
    {
      'prize': 'GB',
      'value': 10,
    },

    // #8 100k FC JACKPOT
    {
      'prize': 'FC',
      'value': 100000,
    },

    // #9 5 GB
    {
      'prize': 'GB',
      'value': 5,
    },
  ];

  //
  @override
  void dispose() {
    _controller.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 300.0,
      color: Colors.grey.shade200,
      child: Column(
        children: [
          // Fortune Wheel
          Expanded(
            child: FortuneWheel(
              selected: _controller.stream,
              animateFirst: false,
              onAnimationStart: () {
                //
                int value = Random().nextInt(9);
                //
                do {
                  _controller.add(value);
                } while (value == 1 || value == 8);

                //
                setState(() {
                  _winningIndex = value;
                  _disableSpinButton = true;
                });

                print('This is WI $_winningIndex');
              },
              onAnimationEnd: () {
                final _priceName =
                    _rewardList[_winningIndex]['prize'].toString();
                final _priceValue =
                    _rewardList[_winningIndex]['value'].hashCode;
                //
                wofRewardDialogFN(
                  priceName: _priceName,
                  priceValue: _priceValue.hashCode,
                  context: context,
                );

                //
                if (_priceName == 'FC') {
                  _shopController.updateFootCoinsFN(fc: _priceValue);
                } else if (_priceName == 'GB') {
                  _shopController.updateGoldenBallsFN(gb: _priceValue);
                } else if (_priceName == 'Spin') {
                  setState(() {
                    _availableSpins = _availableSpins   1;
                  });
                } else {
                  print('Avocado');
                }

                //
                setState(() {
                  _disableSpinButton = false;
                });
              },
              indicators: [
                FortuneIndicator(
                  alignment: Alignment.center,
                  child: TriangleIndicator(color: Colors.white),
                ),
              ],
              items: [
                // #0 15k FC
                FortuneItem(
                  child: WheelStrips(
                    svgImage: 'assets/images/footcoin.svg',
                    prizeText: ' 15k',
                  ),
                  style: FortuneItemStyle(
                    color: Colors.purpleAccent,
                    borderColor: Colors.black38,
                  ),
                ),

                // #6 Avocado
                FortuneItem(
                  child: WheelStrips(
                    svgImage: '',
                    prizeText: '              
  • Related