0

I am making a camera app with a button that will be able to turn the flash on and off.
When the flash is not on I would like for the flash_off icon to be displayed.

However, my problem is trying to make the flash turn on and the icon change to flash_auto when the flash icon is pressed. I tried doing something which you can see down below, but I am still new to Flutter and learning the syntax so I do not think it is right.

I also declared the flash variable as a static bool because it would not allow me to declare it as a regular boolean, "static bool flash = false"

  const IconButton(
            padding: EdgeInsets.only(right: 30), // right 30
            onPressed: null,
            icon: Icon(
              flash ? Icons.flash_on : Icons.flash_off,
              color: Colors.white,
            ),
            iconSize: 50,
            onPressed: () {
              setState(() {
                flash = !flash;
                flash ? _cameraController.setFlashMode(FlashMode.off);
              });
             
              //flash?_cameraController.setFlashMode(FlashMode.torch) : 

            }),
          ),
Serenity
  • 3
  • 3

1 Answers1

1

Because you want three options for icons you can't use a simple true/false check, you'll need an array.

import 'package:flutter/material.dart';

class FlashPage extends StatefulWidget {
  const FlashPage({Key? key}) : super(key: key);

  @override
  State<FlashPage> createState() => _FlashPageState();
}

class _FlashPageState extends State<FlashPage> {
  int flashStatus = 0;
  List<Icon> flash = [
    Icon(Icons.flash_on),
    Icon(Icons.flash_off),
    Icon(Icons.flash_auto)
  ];
  
  List<FlashMode> flashMode = [
    FlashMode.always,
    FlashMode.off,
    FlashMode.auto
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flash demo',
      home: Scaffold(
        body: Center(
          child: IconButton(
              icon: flash[flashStatus],
              onPressed: () {
                setState(() {
                  flashStatus = (flashStatus + 1) % 3;
                  _cameraController.setFlashMode(flashMode[flashStatus]);
                });
              }),
        ),
      ),
    );
  }
}
Gerry
  • 1,159
  • 1
  • 14
  • 29
  • I have tried your code, however, I am getting an error due to the "int flashStatus = 0" I think I may be placing it in the wrong location. Is this the right way? Or can you try it and see what I am doing wrong? I have inserted the code down below @Gerry – Serenity Apr 12 '22 at 01:45
  • children: [ int flashStatus = 0, List flash = [Icons.flash_on, Icons.flash_off, Icons.flash_auto]; const IconButton( padding: EdgeInsets.only(right: 30), // right 30 icon: flash[flashStatus], iconSize: 50, onPressed: () { setState(() { flashStatus = (flashStatus + 1) % 3; }); }), ), – Serenity Apr 12 '22 at 01:47
  • @Serenity I have included the whole working code. Please confirm and then mark the answer as correct with the green tick. – Gerry Apr 12 '22 at 03:15
  • hello I have tried your code however, the flash does not work still. – Serenity Apr 13 '22 at 23:19
  • The code is just for the visual aspect of cycling through the different modes. – Gerry Apr 13 '22 at 23:48
  • @Serenity To use the flash just create a list of FlashMode with the various options and pass the flashStatus into the list. I have updated my answer. Let me know if this works. – Gerry Apr 14 '22 at 03:12
  • I will try to implement it now, however, where did you define camera controller? and how did you define it? – Serenity Apr 15 '22 at 22:45
  • @Serenity I am just going based off your code sample. I assumed you have defined it already in the stateful widget this IconButton you want is in. – Gerry Apr 16 '22 at 00:51