美文网首页
flutter自定义控件

flutter自定义控件

作者: AndyDufres_0e9c | 来源:发表于2019-06-06 10:53 被阅读0次

    自定义可跟随父Widget弹出显示的弹窗效果。支持自定义事件,默认 转发、收藏、删除。有需要的小伙伴可以直接拿去使用。

    效果图
    class CustomRoute extends PopupRoute {
    Widget child;
    
    CustomRoute({@required this.child});
    
    @override
    Color get barrierColor => null;
      
    @override
    bool get barrierDismissible => true;
    
    @override
    String get barrierLabel => null;
    
    @override
    Widget buildPage(BuildContext context, Animation<double> animation,
    Animation<double> secondaryAnimation) {
        return child;
    }
    
    @override
    Duration get transitionDuration => Duration(milliseconds: 200);
    }
    
    • 参考flutter 的toolTip实现弹窗的位置绑定
    ///参考tooltip的控件绑定
    class _PositionDelegate extends SingleChildLayoutDelegate {
    _PositionDelegate({
      @required this.target,
      @required this.verticalOffset,
      @required this.preferBelow,
    })  : assert(target != null),
          assert(verticalOffset != null),
          assert(preferBelow != null);
    
     final Offset target;
    
    final double verticalOffset;
    
    final bool preferBelow;
    
    @override
    BoxConstraints getConstraintsForChild(BoxConstraints constraints) =>
      constraints.loosen();
    
    @override
    Offset getPositionForChild(Size size, Size childSize) {
    return positionDependentBox(
      size: size,
      childSize: childSize,
      target: target,
      verticalOffset: verticalOffset,
      preferBelow: preferBelow,
    );
    }
    
    @override
    bool shouldRelayout(_PositionDelegate oldDelegate) {
    return target != oldDelegate.target ||
        verticalOffset != oldDelegate.verticalOffset ||
        preferBelow != oldDelegate.preferBelow;
    }
    }
    
     ///弹窗 widget
     Widget _buildChild(BuildContext context, double verOffset, bool isPrefer,
     List<Widget> items) {
     final RenderBox box = context.findRenderObject();
     final Offset target = box.localToGlobal(box.size.topCenter(Offset.zero));
    
     /* 
      *
      *const CustomSingleChildLayout({
        Key key,
        @required this.delegate,//控制子项布局的委托。
        Widget child,
      }) : assert(delegate != null),
           super(key: key, child: child);*/
     return CustomSingleChildLayout(
    //位置绑定
     delegate: _PositionDelegate(
        target: target,
        verticalOffset: verOffset ?? 0.0,
        preferBelow: isPrefer ?? false,
     ),
      child: GestureDetector(
        child: Container(
          width: (items == null || items.isEmpty)
              ? 171.0
              : ((items.length) / 2) * 57.0,
          height: 45.0,
           child: Column(
             children: <Widget>[
               Container(
                 height: 36.0,
                 alignment: Alignment.center,
                 decoration: BoxDecoration(
                   shape: BoxShape.rectangle,
                   color: Color(0xe6060606),
                   borderRadius: BorderRadius.circular(5.0),
                 ),
                 child: Row(
                   mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                   crossAxisAlignment: CrossAxisAlignment.center,
                   children: items ??
                       <Widget>[
                         GestureDetector(
                           child: Text(
                             '转发',
                             style: TextStyle(color: Colors.white),
                           ),
                           onTap: onDefaultForward,
                           onTapCancel: () => Navigator.of(context).pop(),
                         ),
                         Container(
                           width: 0.5,
                           color: Colors.white,
                         ),
                         GestureDetector(
                           onTap: onDefaultCollection,
                           child:
                               Text('收藏', style: TextStyle(color: Colors.white)),
                         ),
                         Container(
                           width: 0.5,
                           color: Colors.white,
                         ),
                         GestureDetector(
                           onTap: onDefaultDelete,
                           child:
                               Text('删除', style: TextStyle(color: Colors.white)),
                         )
                       ],
                 ),
               ),
               Image.asset('assets/images/icon_black_arrow_down.png')
             ],
           ),
           ),
         ),
       );
       }
      }
    
    • CustomRoute的使用请参考我的 PopupRoute文章
      完整代码已上传我的 github

    相关文章

      网友评论

          本文标题:flutter自定义控件

          本文链接:https://www.haomeiwen.com/subject/zydyxctx.html