flutter DropDownButton是否删除箭头图标?

q0qdq0h2  于 2023-03-19  发布在  Flutter
关注(0)|答案(4)|浏览(150)

我有一个简单的DropDownButton,想禁用/隐藏它附带的向下箭头,但似乎没有它的选项?
一个非常笨拙的解决方案是设置一个自定义图标,并给予它一个透明的颜色,但这真的感觉不是一个好的解决方案。

h9vpoimq

h9vpoimq1#

iconSize: 0.0添加到您的DropdownButton,如下所示

DropdownButton(
  iconSize: 0.0,
  ...
)
qlzsbp2j

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(),
    );
  }
}
yrdbyhpb

yrdbyhpb3#

最好的方法是定义一个空的Widget作为图标。
SizedBox.shrink()可以设置一个空的Widget,所以需要在DropdownButton参数中添加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(),
    );
  }
}
ttp71kqs

ttp71kqs4#

DropdownSearch<SpeakerAndAuthorModel>(
      dropdownButtonProps: const DropdownButtonProps(isVisible: false ),
  );

相关问题