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 378 forks source link

PanelController must be attached to a SlidingUpPanel #203

Closed omar-hanafy closed 3 years ago

omar-hanafy commented 3 years ago

Describe the bug in my app I am trying to open/close the panel manually by pressing the header of the panel or any button in the body() but I found this error"

Log Message The following assertion was thrown while handling a gesture: PanelController must be attached to a SlidingUpPanel 'package:sliding_up_panel/src/panel.dart': Failed assertion: line 629 pos 12: 'isAttached'

When the exception was thrown, this was the stack:

2 PanelController.open (package:sliding_up_panel/src/panel.dart:629:12)

3 MyHome._body. (package:gym_bar_sales/ui/views/Add/sliding_up_panel_widget.dart:31:21)

4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19)

5 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:1111:38)

6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:183:24)

... Handler: "onTap" Recognizer: TapGestureRecognizer#4971a debugOwner: GestureDetector state: possible won arena finalPosition: Offset(656.5, 105.5) finalLocalPosition: Offset(60.5, 19.5) button: 1 sent tap down ═══════════════════════════════════════════════════════

Expected behavior the panel open or close when calling _pc.open() or .close()

Smartphone

Sample main.dart

import 'package:flutter/material.dart';
import 'package:sliding_up_panel/sliding_up_panel.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Sliding Up Panel",
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  PanelController _pc = PanelController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("SlidingUpPanelExample"),
      ),
      body: SlidingUpPanel(
        controller: _pc,
        panel: Center(
          child: Text("This is the sliding Widget"),
        ),
        body: _body(),
      ),
    );
  }

  Widget _body() {
    return Container(
      child: Column(
        children: <Widget>[
          RaisedButton(
              child: Text("Open"),
              onPressed: () {
                _pc.open();
              }),
          RaisedButton(
            child: Text("Close"),
            onPressed: () => _pc.close(),
          ),
          RaisedButton(
            child: Text("Show"),
            onPressed: () => _pc.show(),
          ),
          RaisedButton(
            child: Text("Hide"),
            onPressed: () => _pc.hide(),
          ),
        ],
      ),
    );
  }
}
omar-hanafy commented 3 years ago

I have fixed this by initializing the PanelController out of the build widget and declare it as final

ex: final PanelController _pc = PanelController();

abel-vs commented 3 years ago

Another cause of this error can be usage of the PanelController outside of the widget in which you attach the PanelController to the Panel. This can be fixed by adding: _pc.isAttached ? [ your code with _pc ] : [ some code not utilizing _pc ( e.g. Container() ) ]

erayerdin commented 3 years ago

This issue is still relevant in case of hot reload. Hot restart seems to resolve this, yet it's annoying to hot restart each time you change some kind of code in your codebase.

polarby commented 3 years ago

A quick solve is also to move "final PanelController _pc = PanelController();" outside of the class.

LarYoungruu commented 3 years ago

@akshathjain I have the same problem. I tried to use a button outside the panel to handle closing the panel and got this error. Has anyone got a solution for this problem?

Ali-Kheiri commented 2 years ago

if you have a StateFulWidget , the best way is in this Example :