is there a way to make a switch (on, off) and save it to firebase and getting it and displaying an offline screen and online Screen?

Viewed 14

i have a switch in the app bar (on, off)

i want it to be saved in firebase and save it in the app itself so when the user enters the app offline an offline screen will be displayed and if online an online screen will be displayed

the method of saving to firebase is working fine,

but i have an issue in displaying the offline screen and the online screen when the switch changed

the app bar widget

  bool switched = false;

  setWorkState(bool value) async {
    await setWorkStatePref(value);
 }

  getWorkState() async {
    bool switched = await getWorkStatePref();
    switched != null ? await setWorkState(switched) : await setWorkState(false);
  }

  @override
  void initState() {
      getWorkState();
      super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<ServineraProvider>(context);
    return AppBar(
      title: Row(
        children: [
          Text("Servinera Provider"),
          Row(
            children: [
             Switch(
                activeColor: Colors.white,
                value: switched,
                onChanged: (value) {
                  setState(()=>switched = value);
                  getWorkState();
                  if(value == true){
                    DatabaseService(pid: provider.pid).updateProviderStatus('On');
                  }
                  else{
                    DatabaseService(pid: provider.pid).updateProviderStatus('Off');
                  }
                },
              ),
              switched ? Text("Online") : Text("Offline")
            ]
          )
        ],
      ),

the main screen

 bool switched;
  int _selectedIndex = 0;

  List<Widget> _widgetOptions;

  @override
  void initState() {
    super.initState();
    getWorkState();
    _widgetOptions = <Widget>[
      switched ? OrderList() : OfflineWidget(),
      Account()
    ];
  }

  getWorkState() async {
    bool switched = await getWorkStatePref();
    setState((){
      this.switched = switched;
    });
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<ServineraProvider>(context);
    return Scaffold(
        appBar: CustomAppBar(),
        bottomNavigationBar: BottomNavigationBar(
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: "Home",
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.person),
              label: "Account"
            ),
          ],
          currentIndex: _selectedIndex,
          selectedItemColor: Colors.blue,
          onTap: _onItemTapped,
        ),
  }
}

can any one help

0 Answers
Related