我有一个简单的DropDownButton,想禁用/隐藏它附带的向下箭头,但似乎没有它的选项?一个非常笨拙的解决方案是设置一个自定义图标,并给予它一个透明的颜色,但这真的感觉不是一个好的解决方案。
h9vpoimq1#
将iconSize: 0.0添加到您的DropdownButton,如下所示
iconSize: 0.0
DropdownButton
DropdownButton( iconSize: 0.0, ... )
qlzsbp2j2#
像这样使用可见性小部件-
icon: Visibility (visible:false, child: Icon(Icons.arrow_downward)),
请参阅下面的完整代码:
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); /// This is the main application widget. class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); static const String _title = 'Flutter Code Sample'; @override Widget build(BuildContext context) { return MaterialApp( title: _title, home: Scaffold( appBar: AppBar(title: const Text(_title)), body: const Center( child: MyStatefulWidget(), ), ), ); } } /// This is the stateful widget that the main application instantiates. class MyStatefulWidget extends StatefulWidget { const MyStatefulWidget({Key? key}) : super(key: key); @override State<MyStatefulWidget> createState() => _MyStatefulWidgetState(); } /// This is the private State class that goes with MyStatefulWidget. class _MyStatefulWidgetState extends State<MyStatefulWidget> { String dropdownValue = 'One'; @override Widget build(BuildContext context) { return DropdownButton<String>( value: dropdownValue, icon: Visibility (visible:false, child: Icon(Icons.arrow_downward)), iconSize: 24, elevation: 16, style: const TextStyle(color: Colors.deepPurple), underline: Container( height: 2, color: Colors.deepPurpleAccent, ), onChanged: (String? newValue) { setState(() { dropdownValue = newValue!; }); }, items: <String>['One', 'Two', 'Free', 'Four'] .map<DropdownMenuItem<String>>((String value) { return DropdownMenuItem<String>( value: value, child: Text(value), ); }).toList(), ); } }
yrdbyhpb3#
最好的方法是定义一个空的Widget作为图标。SizedBox.shrink()可以设置一个空的Widget,所以需要在DropdownButton参数中添加icon: SizedBox.shrink(),。下面是一个简单的例子:
SizedBox.shrink()
icon: SizedBox.shrink(),
Widget build(BuildContext context) { return DropdownButton<String>( value: dropdownValue, elevation: 16, icon: SizedBox.shrink(), style: const TextStyle(color: Colors.deepPurple), underline: Container( height: 2, color: Colors.deepPurpleAccent, ), onChanged: (String? newValue) { setState(() { dropdownValue = newValue!; }); }, items: <String>['One', 'Two', 'Free', 'Four'] .map<DropdownMenuItem<String>>((String value) { return DropdownMenuItem<String>( value: value, child: Text(value), ); }).toList(), ); } }
ttp71kqs4#
DropdownSearch<SpeakerAndAuthorModel>( dropdownButtonProps: const DropdownButtonProps(isVisible: false ), );
4条答案
按热度按时间h9vpoimq1#
将
iconSize: 0.0
添加到您的DropdownButton
,如下所示qlzsbp2j2#
像这样使用可见性小部件-
请参阅下面的完整代码:
yrdbyhpb3#
最好的方法是定义一个空的Widget作为图标。
SizedBox.shrink()
可以设置一个空的Widget,所以需要在DropdownButton参数中添加icon: SizedBox.shrink(),
。下面是一个简单的例子:
ttp71kqs4#