【问题标题】:Making a resizable dropdown widget with touch in flutter在颤动中制作一个可调整大小的下拉小部件
【发布时间】:2019-07-18 00:25:31
【问题描述】:

我想创建一个如下图所示的下拉菜单,通过触摸和拖动打开,通过触摸外部关闭。

before dragging

after dragging

Scaffold(
  appBar: AppBar(
    automaticallyImplyLeading: false
  ),
  body: Stack(
    children: <Widget>[
      Container(
        height: 200,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.vertical(bottom: Radius.circular(20))
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: <Widget>[
            Align(
              alignment: Alignment.bottomCenter,
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 40),
                    child: Divider(
                      color: Colors.blueGrey[500],
                      height: 10,
                      indent: 5,
                    ),
                  ),
                  Icon(FontAwesomeIcons.angleDoubleDown,size: 15,color: Colors.blueGrey[500],)
                ],
              ),
            )
          ],
        ),
      ),
      Center(child: Text('List View'),)
    ],
  )
)

我想改变高度,但是遇到溢出错误! 制作这个小部件的最佳方法是什么? 我可以在 AppBar 中执行此操作吗?

【问题讨论】:

    标签: flutter dropdownbox flutter-animation


    【解决方案1】:

    您可以通过某些方式执行此操作,但立即想到的一种方法是在 Stack 顶部使用带有您自己的 CustomPainterCustomPaint 小部件,这样您实际上可以将其他小部件保留在下方滚动条。

    我已尝试复制您在图片上显示的内容,但请随时根据您的需要进行调整。

    const kMinScrollBarHeight = 20.0;
    
    class MyScreen extends StatefulWidget {
      _MyScreenState createState() => _MyScreenState();
    }
    
    class _MyScreenState extends State<MyScreen> {
      double _scrollBarOffset = 0.0;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: const Color.fromRGBO(13, 23, 35, 1.0),
          appBar: AppBar(
            backgroundColor: const Color.fromRGBO(255, 72, 18, 1.0),
          ),
          body: Stack(children: <Widget>[
            GestureDetector(
              onVerticalDragUpdate: (tapDetails) => setState(() => _scrollBarOffset = tapDetails.globalPosition.dy),
              child: Stack(
                children: <Widget>[
                  Center(
                    child: Text(
                      'My screen widgets',
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                  Stack(
                    children: <Widget>[
                      Positioned(
                        bottom: MediaQuery.of(context).size.height -
                            max(_scrollBarOffset,
                                MediaQuery.of(context).padding.top + kToolbarHeight + kMinScrollBarHeight),
                        child: CustomPaint(
                          painter: MyDraggable(),
                          child: Container(
                            height: MediaQuery.of(context).size.height,
                            width: double.infinity,
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                FlutterLogo(
                                  size: 100.0,
                                ),
                                Text('Flutter is awesome'),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ]),
        );
      }
    }
    
    class MyDraggable extends CustomPainter {
      @override
      void paint(Canvas canvas, Size size) {
        final Paint paint = Paint()..color = Colors.white;
        final Radius cornerRadius = Radius.circular(20.0);
        final double lineMargin = 30.0;
    
        // Draw slider
        canvas.drawRRect(
            RRect.fromLTRBAndCorners(0.0, 0.0, size.width, size.height,
                bottomLeft: cornerRadius, bottomRight: cornerRadius),
            paint);
        paint.color = Colors.black.withAlpha(64);
        paint.strokeWidth = 1.5;
    
        // Draw triangle
        canvas.drawPoints(
            PointMode.polygon,
            [
              Offset((size.width / 2) - 5.0, size.height - 10.0),
              Offset((size.width / 2) + 5.0, size.height - 10.0),
              Offset((size.width / 2), size.height - 5.0),
              Offset((size.width / 2) - 5.0, size.height - 10.0),
            ],
            paint);
    
        // Draw line
        canvas.drawLine(Offset(lineMargin, size.height - kMinScrollBarHeight),
            Offset(size.width - lineMargin, size.height - kMinScrollBarHeight), paint);
      }
    
      @override
      bool shouldRepaint(CustomPainter oldDelegate) => true;
    }
    

    【讨论】:

    • 非常感谢,但是如何在向上或降低高度时将小部件放入其中而不会溢出错误?
    • 哦,对了,如果你想在其中放置一些小部件,你需要注意它的约束。无论如何,我已经通过一些改动编辑了我的答案。基本上你有一个Stack,它根据Positioned 小部件的偏移量在屏幕上上下滑动。我仍然保留CustomPainter 来绘制滑块本身。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 2014-09-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 2016-10-15
    相关资源
    最近更新 更多