flutter 获取父组件大小并布局容器LayoutBuilder

LayoutBuilder(
    builder: (context,constraints){
      	context为父级上下文
      	constraints.biggest.height;  获取组件在父组件所能设置的最大高度
      	contraints.maxWidth;  获取父组件宽度,高度同理
      	
        return 组件
     } 
  )

代码示例:

LayoutBuilder(
    builder: (context,constraints){
      final endHeight=constraints.biggest.height;
      
        return GestureDetector(
          onVerticalDragDown: (text){ //当点击时会获取点击坐标
              print(endHeight);
              print(constraints.maxHeight);
              print(constraints.maxWidth);
          },
          onVerticalDragEnd: (text){
            print(text);
          },
          onVerticalDragCancel: (){
            print("取消");
          },
          child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
              Icon(Icons.search,size: 15,),
            GestureDetector(
              child:Text('A') ,
              onTap: (){
                scroll.animateTo(312, duration: Duration(milliseconds: 200), curve: Curves.easeIn);
              },
            ),
            GestureDetector(
              child:Text('B') ,
              onTap: (){
                scroll.animateTo(478, duration: Duration(milliseconds: 200), curve: Curves.easeIn);
              },
            ),
            GestureDetector(
              child:Text('C') ,
              onTap: (){
                scroll.animateTo(575, duration: Duration(milliseconds: 200), curve: Curves.easeIn);
              },
            ),
            GestureDetector(
              child:Text('D') ,
              onTap: (){
                scroll.animateTo(741, duration: Duration(milliseconds: 200), curve: Curves.easeIn);
              },
            ),
              Text('E'),
              Text('F'),
              Text('G'),
              Text('H'),
              Text('I'),
              Text('J'),
              Text('K'),
              Text('L'),  
              Text('M'),
              Text('N'),
              Text('O'),
              Text('P'),
              Text('Q'),
              Text('R'),
              Text('S'),
              Text('T'),
              Text('U'),
              Text('V'),
              Text('W'),
              Text('X'),
              Text("Y"),
              Text("#")                                   
            ],
          ),
        );
    },
                )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值