Flutter修改popupMenuTheme的fontWeight继承其余配置后菜单变为白容器问题
问题根因
你当前的写法直接创建了全新的PopupMenuThemeData实例,完全覆盖了全局已配置的弹出菜单主题属性,仅保留了你手动声明的字重配置,原本定义的背景色、阴影、字体家族、字体大小等属性全部丢失,所以才会出现白色容器的异常表现。
正确实现代码
Theme( data: Theme.of(context).copyWith( // 基于原有popupMenuTheme配置修改,不新建空实例 popupMenuTheme: Theme.of(context).popupMenuTheme.copyWith( // 同样基于原有文本样式修改,仅覆盖字重属性 textStyle: Theme.of(context).popupMenuTheme.textStyle?.copyWith( fontWeight: FontWeight.w900, ), ), ), child: PopupMenuButton( itemBuilder: (context) => const [ PopupMenuItem(child: Text("更新")), PopupMenuItem(child: Text("删除")), ], icon: const Icon( Icons.more_vert, color: Color(accentColor), ), ), )
注意事项
- 不要为自定义的
PopupMenuThemeData添加const修饰,全局主题是运行时动态获取的,常量声明会导致继承逻辑失效。 copyWith方法只会覆盖你主动传入的属性,其余参数完全继承原有配置,符合你只调整字重、其余属性不变的需求。
内容的提问来源于stack exchange,提问作者Mariam Albarghouti
相关产品推荐
相关产品推荐

