flutter DropDownButton 删除箭头图标?

Ano*_*der 12 flutter flutter-layout

我有一个简单的 DropDownButton,想禁用/隐藏附加到它的向下箭头,但似乎没有选项?

一个非常hacky的解决方法是设置一个自定义图标并给它一个透明的颜色,但这确实感觉不是一个好的解决方案。

Apb*_*Apb 28

像这样添加iconSize: 0.0到你的DropdownButton

DropdownButton(
  iconSize: 0.0,
  ...
)
Run Code Online (Sandbox Code Playgroud)


小智 10

像这样使用可见性小部件 -

icon: Visibility (visible:false, child: Icon(Icons.arrow_downward)),
Run Code Online (Sandbox Code Playgroud)

请参阅下面的完整代码:

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(),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


Jou*_*uby 8

最好的方法是定义一个空的 Widget 作为图标。

可以使用 设置空的 Widget SizedBox.shrink(),因此您需要添加icon: SizedBox.shrink(),到 DropdownButton 参数。

这是一个简单的例子:

  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(),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)