【问题标题】:How do I call a sliding up panel in Flutter?如何在 Flutter 中调用上滑面板?
【发布时间】:2020-04-14 20:10:03
【问题描述】:

我正在使用https://pub.dev/packages/sliding_up_panel,我想在现有面板上按下按钮后触发新面板的打开。

    @override
    Widget build(BuildContext context) {
      BorderRadiusGeometry radius = BorderRadius.only(
        topLeft: Radius.circular(24.0),
        topRight: Radius.circular(24.0),
      );

      return Scaffold(
        appBar: AppBar(
          title: Text("SlidingUpPanelExample"),
        ),
        body: SlidingUpPanel(
          panel: Center(
            child: RaisedButton(
                  child: Text('Show new panel'),
                  onPressed: () {

                    **///want to add code 
here to trigger the callback of the new panel.** 

                  },
             ),
          ),

          collapsed: Container(
            decoration: BoxDecoration(
              color: Colors.blueGrey,
              borderRadius: radius
            ),
            child: Center(
              child: Text(
                "This is the collapsed Widget",
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),

          body: Center(
            child: Text("This is the Widget behind the sliding panel"),
          ),

          borderRadius: radius,
        ),
      );
    }

此依赖项能够在控制器的帮助下调用 open() 方法,但我无法理解如何使用它调用新的向上滑动面板(并且显然关闭现有面板)。 这是 open() 操作的代码:

PanelController _pc = new PanelController();
...
  RaisedButton(
          child: Text("Open"),
          onPressed: () => _pc.open(),
        ),

【问题讨论】:

    标签: user-interface flutter dart


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    您可以使用Column 并将这两个SlidingUpPanel 包装成Visibility 并将maintainStatemaintainAnimation 设置为true
    工作演示显示SlidingUpPanel 1 和 2 之间的切换

    代码sn-p

    body: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Visibility(
                maintainState: true,
                maintainAnimation: true,
                visible: _visible,
                child: SlidingUpPanel(
                  controller: _pc1,
                  panel: Center(
                    child: RaisedButton(
                      child: Text('Show new panel 2'),
                      onPressed: () {
                        _pc1.close();
                        _visible = false;
                        setState(() {});
                        _pc2.open();
                      },
    

    工作演示

    完整代码

    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: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      PanelController _pc1 = new PanelController();
      PanelController _pc2 = new PanelController();
      bool _visible = true;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        BorderRadiusGeometry radius = BorderRadius.only(
          topLeft: Radius.circular(24.0),
          topRight: Radius.circular(24.0),
        );
    
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Visibility(
                maintainState: true,
                maintainAnimation: true,
                visible: _visible,
                child: SlidingUpPanel(
                  controller: _pc1,
                  panel: Center(
                    child: RaisedButton(
                      child: Text('Show new panel 2'),
                      onPressed: () {
                        _pc1.close();
                        _visible = false;
                        setState(() {});
                        _pc2.open();
                      },
                    ),
                  ),
                  collapsed: Container(
                    decoration:
                        BoxDecoration(color: Colors.blueGrey, borderRadius: radius),
                    child: Center(
                      child: Text(
                        "Panel 1",
                        style: TextStyle(color: Colors.orange),
                      ),
                    ),
                  ),
                  body: Center(
                      child: Text(
                          "Panel 1 This is the Widget behind the sliding panel")),
                  borderRadius: radius,
                ),
              ),
              Visibility(
                maintainState: true,
                maintainAnimation: true,
                visible: !_visible,
                child: SlidingUpPanel(
                  controller: _pc2,
                  panel: Center(
                    child: RaisedButton(
                      child: Text('Show new panel 1'),
                      onPressed: () {
                        _pc2.close();
                        _visible = true;
                        setState(() {});
                        _pc1.open();
                      },
                    ),
                  ),
                  collapsed: Container(
                    decoration:
                        BoxDecoration(color: Colors.blueGrey, borderRadius: radius),
                    child: Center(
                      child: Text(
                        "Panel 2 ",
                        style: TextStyle(color: Colors.red),
                      ),
                    ),
                  ),
                  body: Center(
                    child:
                        Text("Panel 2 This is the Widget behind the sliding panel"),
                  ),
                  borderRadius: radius,
                ),
              ),
            ],
          ),
        );
      }
    }
    

    【讨论】:

    • 非常感谢!只是想知道是否有办法为新面板的关闭和打开添加动画?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 2013-10-03
    • 1970-01-01
    • 2021-06-23
    • 2015-09-06
    相关资源
    最近更新 更多