Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
how to add loader when on the toggle button

I want to display a circular loader when user is going to on the toggle button, then after few secs toggle button will active.

here is my code


InkWell(
                      onTap: () {
                        Navigator.of(context).push(MaterialPageRoute(
                          builder: (context) => ProductProfile(),
                        ));
                      },
                      child: Container(
                         decoration: BoxDecoration(
                       color: _selectedProducts.contains(book.id) ? Colors.grey[200] :Colors.white,
                        borderRadius: BorderRadius.all(
                          Radius.circular(10),
                        ),
                      ),
                       
                        child: ListTile(
                          dense: true,
                          trailing: Switch(
                            value: _selectedProducts.contains(book.id),
                            onChanged: (bool? selected) {
                              if (selected != null) {
                                setState(() {
                                  _onProductSelected(selected, book.id);
                                });
                              }
                            },
                            activeTrackColor: HexColor("#b8c2cc"),
                            activeColor: HexColor("#7367f0"),
                          ),
                          title: Text(
                            book.title,),
                              Divider()
                          
                        ),
                      ),
                    ),
                    SizedBox10(),
                  ],
                );

please help how to do this

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

To achieve that, you need bool _isLoading and a timer. Steps I would do:

  • Declare _isLoading: bool _isLoading = false;

  • Change _isLoading value using a timer:

    void timer() {
      int _time = 10;
    
      Timer timer = new Timer.periodic(
        Duration(seconds: 1),
        (Timer timer) async {
          if (_time == 0) {
            _isLoading = true;
            timer.cancel();
          } else {
            setState(() {
              _time--;
            });
          }
        },
      );
    }
    
  • Use _isLoading on your build method (for example):

    @override
    Widget build(BuildContext context) {
      return _isLoading ? CircularProgressIndicator() : Container();
    }
    
  • Or to hide your button:

    @override
    Widget build(BuildContext context) {
      return _isLoading ? Container() : YourToggleButton;
    }
    
  • Also remember to dispose your timer!

    @override
    void dispose() {
      timer.cancel();
    }
    
over 4 years ago · Santiago Trujillo Relatório

0

So, If you are on Flutter web, there is a widget called MouseRegion which has onHover, onEnter & onExit.

You can assign a new bool for instance bool showLoader=false which you will toggle to true with setState (inside the onHover, where you could also start the Timer and when finished reset the showLoader to false).

Now you can show your button with a ternary operator : showLoader ? CircularProgressIndicator() : YourButton()

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda