Flutter发布模式下DropdownSearch与CustomTextField不显示求助
Flutter Release模式下DropdownSearch和CustomTextField不显示的解决方法
问题根源
你的代码中在ListView的子组件里直接使用了Expanded,这会导致Release模式下布局高度计算异常,控件被压缩至不可见。Debug模式下Flutter的布局检查机制相对宽松,所以能正常渲染,但Release模式会严格遵循布局约束,最终导致控件无法显示。
修复方案
移除DropdownSearch和CustomTextField外层的Expanded组件即可。如果需要控制控件高度,可直接使用SizedBox包裹并指定高度,或者调整控件自身的高度属性。
修改后的代码示例
body: ListView( shrinkWrap: true, padding: const EdgeInsets.all(15.0), children: <Widget>[ DropdownSearch<String>( popupProps: const PopupProps.menu( showSelectedItems: true, ), items: [ 'Option 1', 'Option 2', 'Option 3' ], dropdownDecoratorProps: const DropDownDecoratorProps( dropdownSearchDecoration: InputDecoration( labelText: 'Choose', hintText: 'Choose', ), ), onChanged: print, selectedItem: 'Option 1', ), SizedBox( height: 20, ), CustomTextField( controller: num1controller, showCancelIcon: true, keyboardType: TextInputType.number, decoration: InputDecoration( labelText: 'Enter Number', hintText: 'Enter Number', ), ), SizedBox( height: 30, ), ElevatedButton( child: const Text('Calculate'), onPressed: () { setState(() { double sum = double.tryParse(num1controller.text) ?? 1; final sum1 = sum / calculation; result = sum1.toStringAsFixed(3); }); }, ), SizedBox( height: 30, ), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[ Text( 'Result', style: TextStyle(fontSize: 20), ), Container( color: Colors.black12, child: Text( '$result', style: Theme.of(context).textTheme.headline1!.copyWith( fontSize: 25, color: Theme.of(context).colorScheme.primary, fontWeight: FontWeight.w600, ), ), ) ], ), ])
补充说明
DropdownSearch和CustomTextField默认会自动填充父容器的宽度,无需额外使用Expanded来拉伸宽度。如果需要调整控件高度,可以在控件外层包裹SizedBox(height: 50)这类指定高度的组件来实现。
内容的提问来源于stack exchange,提问作者Zee
相关产品推荐
相关产品推荐

