To change app bar icon dynamically according to sms received

713 views
Skip to first unread message

Abinavan Nagendran

unread,
Jun 29, 2020, 11:26:03 AM6/29/20
to Flutter Development (flutter-dev)
Hi... I'm doing an app in which it needs to change the appbar icon according to the SMS received.
I have used the SMS package to receive SMS and it is working. Now I want to change appbar icon according to received SMS.
For example: if the SMS received is "On", The appbar icon needs to change to toggle_on.
                       if the SMS received is "Off", The appbar icon needs to change to toggle_off.
https://stackoverflow.com/questions/55730390/flutter-change-appbar-icon-when-receiving-notification  I tried this but it is not working. showing error in setState() 
Can you suggest me some reference .
This is my code 
import 'package:flutter/material.dart';
import 'package:sms/sms.dart';


void main() => runApp(QuickBee());

String mTitle=" ";  
IconData _iconData;
class QuickBee extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Imik',
      debugShowCheckedModeBanner: false,
      // Set Raleway as the default app font
      theme: ThemeData(
        fontFamily: 'Roboto',
      ),

      home: MyHomePage(),
    );
  }
}
void receiveMsg() {  
    SmsReceiver receiver = new SmsReceiver();
  receiver.onSmsReceived.listen((SmsMessage msg) =>mTitle=(msg.body));
}

void testAlert(BuildContext context) {
    Widget okButton = FlatButton(  
      child: Text("OK"),  
      onPressed: () {  
        Navigator.of(context).pop();  
      },  
      shape: CircleBorder(side: BorderSide(color: Colors.transparent)),
    );  
    var alert = AlertDialog(      
      contentPadding: EdgeInsets.only(top: 10.0),
      title: Text("The recently received msg is"),
      content: Text("$mTitle"),    
      actions: [  
      okButton,  
     ],  
      
    );
    
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return alert;
        });
}
void onoff(){
  if(mTitle=='On'){                
    _iconData = Icons.notifications;
  }
  else if(mTitle=='Off'){               
  _iconData = Icons.notifications;                
  }

}
class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
          title: new Text("Title"),
          actions: <Widget>[
            
          ],
        ),
      
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Stack(
              alignment: Alignment.center,
              children: <Widget>[
                new Container(
                  height: 60.0,
                  width: 60.0,
                  decoration: new BoxDecoration(
                      borderRadius: new BorderRadius.circular(50.0),
                      color: Color(0xFF45E0EC)),
                  child: new Icon(
                    Icons.build,
                    color: Colors.white,
                  ),
                ),
                new Container(
                  margin: new EdgeInsets.only(right: 50.0, top: 50.0),
                  height: 60.0,
                  width: 60.0,
                  decoration: new BoxDecoration(
                      borderRadius: new BorderRadius.circular(50.0),
                      color: Color(0xFFFC6A7F)),
                  child: new Icon(
                    Icons.device_hub,
                    color: Colors.white,
                  ),
                ),
                new Container(
                  margin: new EdgeInsets.only(left: 30.0, top: 50.0),
                  height: 60.0,
                  width: 60.0,
                  decoration: new BoxDecoration(
                      borderRadius: new BorderRadius.circular(50.0),
                      color: Color(0xFFFFCE56)),
                  child: new Icon(
                    Icons.phonelink,
                    color: Colors.white,
                  ),
                ),
                new Container(
                  margin: new EdgeInsets.only(left: 90.0, top: 40.0),
                  height: 60.0,
                  width: 60.0,
                  decoration: new BoxDecoration(
                      borderRadius: new BorderRadius.circular(50.0),
                      color: Color(0xFF18D191)),
                  child: new Icon(
                    Icons.memory,
                    color: Colors.white,
                  ),
                )
              ],
            ),
            new Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(top: 10.0, bottom: 80.0),
                  child: new Text(
                    "Receive SMS",
                    style: new TextStyle(fontSize: 30.0,fontWeight: FontWeight.bold),
                  ),
                )
              ],
            ),
           
            new Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
              /*Center(
           
                child: RaisedButton(
                  padding: const EdgeInsets.only(left: 20.0, right: 5.0, top: 10.0),
                  color: Colors.black,
                  textColor: Colors.white,
                  onPressed: () {
                    receiveMsg();
                    testAlert(context);             
                  },
                  child: Text("Show notification"),
                ),
              ),*/

                Expanded(
                  child: RaisedButton(                  
                    onPressed: () {
                      receiveMsg();
                      testAlert(context);    
                               
                   
                    },
                    child: new Container(
                        alignment: Alignment.center,
                        height: 60.0,
                        decoration: new BoxDecoration(
                            color: Color(0xFFDF513B),
                            borderRadius: new BorderRadius.circular(9.0)),
                        child: new Text("Show notification",
                            style: new TextStyle(
                                fontSize: 20.0, color: Colors.white))),
                ),
                ),                
              ],
            )
          ],
        ),
      ),
    );
  }
}
Thank You

Souvik Dutta

unread,
Jun 30, 2020, 8:12:33 AM6/30/20
to Abinavan Nagendran, Flutter Development (flutter-dev)
Does your sms come with labels indicating the different types of SMSs?

Souvik flutter dev

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/5493e253-3905-46b5-9e2b-1025e49fafb7o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages