【问题标题】:How to put border rounded in DropdownButtonFormField? using flutter如何在 DropdownButtonFormField 中设置圆角边框?使用颤振
【发布时间】:2021-03-03 13:03:02
【问题描述】:

如何在 DropdownButtonFormField 中设置圆角边框?以及如何在下拉图标中制作背景颜色?请看下图

            Container(
                padding: EdgeInsets.only(top: 10),
                width: (globals.screenWidth * 0.8),
                height: (globals.screenHeight * 0.08),
                color: Colors.white,
         
                child: DropdownButtonFormField(
                  style: TextStyle(
                    fontSize: globals.fontsize_18,
                    color: Colors.black,
                    fontWeight: FontWeight.normal,
                  ),
                  value: _selectedLocation,
                  onChanged: (newValue) {
                    setState(() {
                      _selectedLocation = newValue;
                    });
                  },
                  items: _locations.map((location) {
                    return DropdownMenuItem(
                      child: new Text(location),
                      value: location,
                    );
                  }).toList(),
                )
              )

结果

这是我想要的结果

【问题讨论】:

标签: flutter dart


【解决方案1】:

这是一个带有PopupMenuButton 的解决方案,它也允许配置菜单项。

完整源代码:

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: HomePage(),
    ),
  );
}

class HomePage extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final _selected = useState(locations[0]);
    return Scaffold(
      backgroundColor: Color(0xff1b2052),
      body: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.all(16.0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              'Where do you want to go today?',
              style: TextStyle(
                fontSize: 32.0,
                color: Colors.white54,
              ),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 48.0),
            MyDropdown<String>(
              items: locations,
              selected: _selected.value,
              onChanged: (newLocation) => _selected.value = newLocation,
            ),
          ],
        ),
      ),
    );
  }
}

class MyDropdown<T> extends StatelessWidget {
  final List<T> items;
  final T selected;
  final ValueChanged<T> onChanged;

  const MyDropdown({Key key, this.items, this.selected, this.onChanged})
      : super(key: key);
  @override
  Widget build(BuildContext context) {
    return PopupMenuButton(
        onSelected: onChanged,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10),
        ),
        itemBuilder: (context) {
          return items
              .map(
                (value) => PopupMenuItem(
                  value: value,
                  child: Text(value.toString()),
                ),
              )
              .toList();
        },
        offset: Offset(1, 1),
        child: Container(
          decoration: BoxDecoration(
            color: Colors.white,
            border: Border.all(color: Colors.brown),
            borderRadius: BorderRadius.all(Radius.circular(8.0)),
          ),
          clipBehavior: Clip.antiAlias,
          child: IntrinsicHeight(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text('Location: ${selected}'),
                ),
                Container(
                  padding: const EdgeInsets.all(4.0),
                  color: Colors.black54,
                  child: Icon(Icons.expand_more, color: Colors.white54),
                ),
              ],
            ),
          ),
        ));
  }
}

final List<String> locations = [
  'Earth',
  'Jupiter',
  'Mars',
  'Mercury',
  'Neptune',
  'Saturn',
  'Uranus',
  'Venus',
];

【讨论】:

    【解决方案2】:

    应该这样做!

    Container(
                    padding: EdgeInsets.only(top: 10),
                    width: (globals.screenWidth * 0.8),
                    height: (globals.screenHeight * 0.08),
                    decoration: BoxDecoration(
                       borderRadius: BorderRadius.circular(10),
                    ),
                    child: DropdownButtonFormField(
                      style: TextStyle(
                        fontSize: globals.fontsize_18,
                        color: Colors.black,
                        fontWeight: FontWeight.normal,
                      ),
                      value: _selectedLocation,
                      onChanged: (newValue) {
                        setState(() {
                          _selectedLocation = newValue;
                        });
                      },
                      items: _locations.map((location) {
                        return DropdownMenuItem(
                          child: new Text(location),
                          value: location,
                        );
                      }).toList(),
                    )
                  )
    

    将此添加到 DropdownButtonFormField

    DropdownButtonFormField(
                    icon: Container(
                      color: Colors.black,
                      child: Icon(Icons.keyboard_arrow_down, color: Colors.white,),
                    ),
                ),
    

    【讨论】:

    • 嗯我现在得到了边框,但是下拉图标呢?我怎样才能让它变黑?
    • @Rene 尝试在 DropdownButtonFormField 中添加 iconEnabledColor:
    • iconEnabledColor 更改图标颜色,而不是其背景
    • @TDN 你看到我的i want result 了吗?图片?
    • 哦,对不起,用容器包装图标,我更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 2012-08-27
    • 2021-03-01
    • 2013-04-30
    相关资源
    最近更新 更多