0

I need my 'ENCENDER' and 'APAGAR' buttons to change their color after I hit them and send some data through bluetooth, but they change only after I make a hot reload. Don't know if the mistake is on the ternary condition, I have almost the same logic on the previous widget and it's working just fine to change the button when I hit 'SINCRONIZAR'

Here's my code:

import 'package:douglas_app/controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:loading_indicator/loading_indicator.dart';



// ignore: use_key_in_widget_constructors
class SyncDouglas extends StatelessWidget {


  /* 

COLOR FONDO:

Color.fromRGBO(22, 22, 35, 1),

COLOR BORDE NARANJA:

Color.fromRGBO(225, 139, 0, 1),

COLOR FUENTE:

Color(0xffCCCCCC)



 */

  @override
  Widget build(BuildContext context) {

    
    return GetBuilder<PruebaBlue>(
        init: PruebaBlue(''),
        builder: (_) => Scaffold(
              backgroundColor: const Color.fromRGBO(22, 22, 35, 1),
              body: Center(
                child: AnimatedSwitcher(
                  duration: const Duration(milliseconds: 500),
                  child: !_.isScanningForDevices
                      ? GestureDetector(
                          onTap: () => _.scanForDevices(),
                          child: Container(
                            width: 150,
                            height: 150,
                            child: const Center(
                              child: Text("Sincronizar",
                                  textScaleFactor: 1.4,
                                  textAlign: TextAlign.center,
                                  style: TextStyle(color: Colors.black)),
                            ),
                            decoration: BoxDecoration(
                                shape: BoxShape.circle,
                                border: Border.all(color: const Color.fromRGBO(22, 22, 35, 1)),
                                color: Colors.yellow),
                          ),
                        )
                      : AnimatedSwitcher(
                          duration: const Duration(milliseconds: 500),
                          child: _.isScanningForDevices &&
                                  !_.isConnectedToCharacteristic
                              ? SizedBox(
                                  width:
                                      MediaQuery.of(context).size.width * 0.9,
                                  child: Stack(
                                    children: [
                                      Center(
                                        child: LoadingIndicator(
                                          indicatorType:
                                              Indicator.ballScaleMultiple,
                      ),
                                      ),
                                      const Center(
                                          child: Text(
                                        "Sincronizando",
                                        textScaleFactor: 1.4,
                                        style: TextStyle(color: Colors.white),
                                      ))
                                    ],
                                  ),
                                )
                              :  Column(
                                mainAxisAlignment:
                                    MainAxisAlignment.center,
                                children: [
                                  Container(
                                    width: 150,
                                    height: 150,
                                    child: const Center(
                                      child: Text("Sincronizado",
                                          
                                          textScaleFactor: 1.4,
                                          textAlign: TextAlign.center,
                                          style: TextStyle(
                                              color: Colors.black)),
                                    ),
                                    decoration: const BoxDecoration(
                                        shape: BoxShape.circle,
                                        color: Color.fromRGBO(248, 243,43, 1),),
                                  ),

                                  
                                    const SizedBox(height:10),
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      children: [
                                        GestureDetector(
                                          onTap: () async {
                                            await _.sendDataToDevice('e');
                                          },
                                            child: Container(
                                              width: MediaQuery.of(context)
                                                      .size
                                                      .width *
                                                  0.35,
                                              decoration: BoxDecoration(
                                                  color: !_.isOn
                                                      ? Colors.green
                                                      : Colors.grey,
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          10)),
                                              margin:
                                                  const EdgeInsets.all(8.0),
                                              padding:
                                                  const EdgeInsets.all(12.0),
                                              child: const Text("Encender",
                                                  textScaleFactor: 1.4,
                                                  textAlign: TextAlign.center,
                                                  style: TextStyle(
                                                      color: Colors.white)))
                                          
                                        ),
                                        GestureDetector(
                                      onTap: () async{
                                          await  _.sendDataToDevice('a');
                                          },
                                      child: Container(
                                        width: MediaQuery.of(context)
                                                .size
                                                .width *
                                            0.35,
                                        decoration: BoxDecoration(
                                            color:
                                                _.isOn
                                                    ? Colors.red
                                                    : Colors.grey,
                                            borderRadius:
                                                BorderRadius.circular(
                                                    10)),
                                        padding:
                                            const EdgeInsets.all(10.0),
                                        margin:
                                            const EdgeInsets.all(12.0),
                                        child: const Text("Apagar",
                                            textAlign: TextAlign.center,
                                            textScaleFactor: 1.4,
                                            style: TextStyle(
                                                color: Colors.white)),
                                      )),
                                      ],
                                    ),


                                    
                                  
                                ],
                              ),
                          
                               
                        ),
                ),
                
              ),
            ));
  }
}

Here's my SendData code:

sendDataToDevice(String? dataToSend) async {
    //Encoding the string
    if (dataToSend == 'e') {
      this.isOn = true;    
    } else {
      this.isOn = false;
    }
    dev.log("Send Data: " + dataToSend!);
    List<int> data = utf8.encode(dataToSend);
    await bluetoothCharacteristicTx.write(data, withoutResponse: false);
    
/*     print('RX');
    await bluetoothCharacteristicRx.read(); */
    
    /* print('TX');
    List<int> recibido = await bluetoothCharacteristicTx.read();
    print(utf8.decode(recibido));
    print('FIN'); */
  }

  double getDistance(int rssi) {
    /*
  distance_in_meters = 10 ^ ((-69 - (<RSSI_VALUE>))/(10 * 2))
  where: -69 is a reference powerMeasured from 1 meter
  */
    double n = 2; // calibration factor range: 2-4
    return pow(10, (-69 - (rssi)) / (10 * n)).toDouble();

    
  }
croxx5f
  • 5,163
  • 2
  • 15
  • 36
  • 1
    1. Use Stateful Widget instead of Stateless. 2. Keep your sendDataToDevice(){} inside the Stateful widget before or after @override Widget build(BuildContext context) {}. 3. Enclose this.isOn = true: and this.isOn = false in setState function. – Ashiq Ullah Sep 12 '21 at 01:45

1 Answers1

0

First off, is sendDataToDevice part of your SyncDouglas widget? if so you need to convert your widget to a stateful widget, update the isOn property within a setState function call.

Otherwise, if isOn and sendDataToDevice, is part of a parent widget of SyncDouglas, then you'd need to call setState there to rebuild SyncDouglas

Scott
  • 552
  • 3
  • 7