I want to achieve PersistentBottomSheet but doesn't want to use setState() . want to achieve throw streamBuilder

I have tried the below code with setState it works but used StreamBuilder it doesn't gave the result as expected

 VoidCallback _showBottomSheetCallback;

  @override
  void initState() {
    super.initState();
    _showBottomSheetCallback = _showBottomSheet;
  }

void _showBottomSheet() {
    setState(() { // disable the button
      _showBottomSheetCallback = null;
    });
    _scaffoldKey.currentState.showBottomSheet<void>((BuildContext context) {
      final ThemeData themeData = Theme.of(context);
      return Container(
        decoration: BoxDecoration(
          border: Border(top: BorderSide(color: themeData.disabledColor))
        ),
        child: Padding(
          padding: const EdgeInsets.all(32.0),
          child: Text('This is a Material persistent bottom sheet. Drag downwards to dismiss it.',
            textAlign: TextAlign.center,
            style: TextStyle(
              color: themeData.accentColor,
              fontSize: 24.0,
            ),
          ),
        ),
      );
    })
    .closed.whenComplete(() {
      if (mounted) {
        setState(() { // re-enable the button
          _showBottomSheetCallback = _showBottomSheet;
        });
      }
    });
  }

It is working perfect but as soon i wrap it inside streamBuilder doesn't give expected output I created a stream which will take some input on click of raised button and it will notify stream builder about it but its't not woking. Inside Raised button onpress i have onPressed: _showBottomSheetCallback,

1 Answers

0
sarvesh chavan On

I achieve it using streams and stream builder as follow

created a stream controller and stream and a function to add into stream in my bloc class as

//controller
final _bottomSheetSubject = BehaviorSubject<String>();

//stream
Observable<String> get bottomSheetEvent => _bottomSheetSubject.stream;

//add value in a stream
Function(String) get addEventBottomSheet => _bottomSheetSubject.sink.add;

In my ui part did the following

so initial data in the snapShot will be string "show" and once the user press the button the data which will go in a stream will be "null" that time the stream builder will be called again since it is listening to the stream now the data it will compare and it will return null for the on press method and once the bottom sheet is close will again add "show" in stream and again eventually the stream builder will call with the data as "show".

 Widget filterBtn() {
    return StreamBuilder(
      stream: homeBloc.bottomSheetEvent,
      initialData: 'show',
      builder: (BuildContext context, AsyncSnapshot<String> snapShot) {
        return RaisedButton(
          onPressed: () {
            if (snapShot.data == 'show') {
              homeBloc.addEventBottomSheet('null');
              _scaffoldKey.currentState
                  .showBottomSheet<void>((BuildContext context) {
                    final ThemeData themeData = Theme.of(context);
                    return Container(
                      decoration: BoxDecoration(
                          border: Border(
                              top: BorderSide(color: themeData.disabledColor))),
                      child: Padding(
                        padding: const EdgeInsets.all(32.0),
                        child: Text(
                          'This is a Material persistent bottom sheet. Drag downwards to dismiss it.',
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            color: themeData.accentColor,
                            fontSize: 24.0,
                          ),
                        ),
                      ),
                    );
                  })
                  .closed
                  .whenComplete(() {
                    if (mounted) {
                      homeBloc.addEventBottomSheet('show');
                    }
                  });
            }
            return null;
          },
        );
      },
    );
  }