Flutter填充值的恒定值无效



我有一个Widget包裹在一个Padding。我想让rightPadding分别对应于当前设备的width。这是我尝试过的:

child: Padding(
padding: const EdgeInsets.only(
top: 8,
bottom: 8,
right: MediaQuery.of(context).size.width / 30), // -> error
child: Image.asset('assets/images/person.png'),
),

但是我得到错误:

无效常量

为什么会这样?我该如何解决这个问题?

您需要在EdgeInsets之前删除const。为什么?因为如果你使用var(MediaQuery.of(context).size.width),它就不能是常量。

试试这个:

child: Padding(
padding: EdgeInsets.only(
top: 8,
bottom: 8,
right: MediaQuery.of(context).size.width / 30), // -> error
child: Image.asset('assets/images/person.png'),
),

最新更新