最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - Why didn't the constrain from parent widget apply to a child widget of different kind? - Stack Overflow

programmeradmin1浏览0评论

I have a parent widget GeneralBtn:

class GeneralBtn extends StatelessWidget {
  final VoidCallback onPressed;
  final Widget? child;
  final bool compact

  GeneralBtn({
    thispact = false,
    required this.onPressed,
    this.child,
  });
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        padding: compact 
                ? const EdgeInsets.symmetric(horizontal: 12, vertical: 0) // Reduced padding
                : const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
        visualDensity: compact
                ? VisualDensitypact 
                : VisualDensity.standard,
      ),
      onPressed: onPressed,
      child: child ?? Text(""),
    );
  }
}

The padding and visualDensity are set in GeneralBtn.

I also have a child widget that inherits GeneralBtn, but more importantly, it builds a GeneralBtn that encloses another widget ListTile. Thus ListTile in this case is more like a child of GeneralBtn.

class RecentCallBtn extends GeneralBtn {
// constructor ...
// build function
return  GeneralBtn(
      text: text, 
      onPressed: onPhoneServiceCall, 
      child: child??
        ListTile(
          contentPadding: compactItem 
                          ? const EdgeInsets.symmetric(horizontal: 12, vertical: 0) // Reduced padding
                          : const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
          visualDensity: compactItem 
                          ? VisualDensitypact 
                          : VisualDensity.standard,
      // other stuffs
 );
} //end build function
}// end RecentCallBtn

I see that the constrain padding and visualDensity is not applied if I put them on GeneralBtn build function. I thought that:

Constraints go down. Sizes go up. Parent sets position

as explained in /ui/layout/constraints

The following is the output if I comment out the constrain the build function of RecentCallBtn or put the constrain in the build function of GeneralBtn:

On the other hand, if I put the constrain of padding and visualDensity in RecentCallBtn build function and remove them from GeneralBtn, then the compact applies: (it looks more compact)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论