akshathjain / sliding_up_panel

A draggable Flutter widget that makes implementing a SlidingUpPanel much easier!
https://pub.dartlang.org/packages/sliding_up_panel
Other
1.36k stars 379 forks source link

Return Value From Panel After Closing #280

Open benha0898 opened 2 years ago

benha0898 commented 2 years ago

Bug / Suggestion / Question

Is there a way to return something from the panel after closing it? Something similar to Navigator.of(context).pop(value).

My Use Case

I'm using the panel with the structure outlined below. So in MyPage I want to open up the panel to perform some action, which I might need to return a value after the action is completed and/or if the panel is closed. I was hoping it could be something as straightforward as how Dialogs work: result = showDialog<T>(). Maybe there is already an obvious way that I'm not aware of.

My Code

--------------------------------------------------------------------------------------------
class CustomSlidingUpPanel extends StatefulWidget {
  final Widget body;
  ..
}

class CustomSlidingUpPanelState extends State {
  final PanelController _panelController = PanelController();
  ScrollController? _scrollController;
  Widget _panelContent = Container();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SlidingUpPanel(
        controller: _panelController,
        body: widget.body,
        onPanelClosed: () {
          setState(() {
            _panelContent = Container();
          });
        },
        panelBuilder: (ScrollController controller) {
          if (_scrollController == null) {
            _scrollController = controller;
          }
          return Scaffold(
            body: ClipRRect(
              borderRadius: BorderRadius.vertical(top: Radius.circular(30)),
              child: _panelContent,
            ),
          );
        },
      ),
    );
  }

  Future getBool() async {
    setState(() {
      _panelContent = Column(
        children: [
          ElevatedButton(child: Text("True"), onPressed: () => Navigator.of(context).pop(true)),
          ElevatedButton(child: Text("False"), onPressed: () => Navigator.of(context).pop(false)),
        ],
      );
    });
    openPanel();
  }

  void openPanel() {
    _panelController.open();
  }

  void closePanel() {
    _panelController.close();
  }

--------------------------------------------------------------------------------------------

class _MyPageState extends State {
  GlobalKey _panelGlobalKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return CustomSlidingUpPanel(
      key: _panelGlobalKey,
      body: Scaffold(
        body: Center(
          child: ElevatedButton(child: Text("Get bool from panel"), onPressed: _getBoolFromPanel),
        ),
      ),
    );
  }

  Future _getBoolFromPanel() async {
    bool? result = await _panelGlobalKey.currentState?.getBool();
  }

}

Desired Behavior

Dependencies

Thank you!