Flutter change background color using streambuilder

Viewed 295

I'm new in Flutter and bloc architecture and I try some things. Here I'll just try to set my background color, and it works with this code

  final ColorBloc _bloc = ColorBloc();

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<Response<ColorResponse>>(
      stream: _bloc.stream,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          switch (snapshot.data.status) {
            case Status.LOADING:
              return Loading(loadingMessage: snapshot.data.message);
              break;
            case Status.COMPLETED:
              return Scaffold(
                  appBar: AppBar(
                    title: Text('First Route clicked'),
                  ),
                  backgroundColor: snapshot.data.data.color,
                  body: new Center(
                      child: new InkWell(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => Act2()),
                      );
                    }, // Handle your callback
                    child: Ink(height: 100, width: 100, color: Colors.blue),
                  )),
                  floatingActionButton: FloatingActionButton(
                    onPressed: () {
                      _bloc.changeColor(Colors.yellow);
                    },
                    child: Icon(Icons.navigation),
                    backgroundColor: Colors.green,
                  ));
              break;
            case Status.ERROR:
              return Error(
                errorMessage: snapshot.data.message,
                //onRetryPressed: () => _bloc.fetchCategories(),
              );
              break;
          }
        }
    );
  }
}

But I need to rebuild all my page, can I just set the background color with the streamBuilder answer ?

EDIT: Should I use session to store my final color ?

0 Answers
Related