【问题标题】:Flutter DropdownButtonFormField is expanding but shouldn'tFlutter DropdownButtonFormField 正在扩展但不应该
【发布时间】:2022-12-23 19:02:20
【问题描述】:

我尝试在 Flutter 中构建一个复杂的 From。该应用程序将在 Windows 和 Web 上运行。但是现在我遇到了 DropdownButtonFormField 的问题。我希望它只需要那么宽。但是即使我将 isExpanded 设置为 false 也会扩展。 我哪里错了?

蓝色部分是背景,侧面的灰色只是边框。我希望白框尽可能小。

这里的代码:

class FormOne extends StatefulWidget {
  const FormOne({Key? key}) : super(key: key);

  @override
  State<FormOne> createState() => _FormOneState();
}

class _FormOneState extends State<FormOne> {

  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {Padding(
      padding: EdgeInsets.only(top: 15),
      child: Form(
        key: _formKey,
        child: Column(
          children: [
            Text("Headline"),
            const SizedBox(height: 10),
            Container(
              padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
              color: Colors.white,
              child: DropdownButtonFormField<String>(
                items: [
                  DropdownMenuItem<String>(
                    child: BbnText.paragraphContrast("test 1"),
                    value: "test 1",
                  ),
                  DropdownMenuItem<String>(
                    child: BbnText.paragraphContrast("test 100000"),
                    value: "test 100000",
                  )
                ],
                value: "test 100000",
                onChanged: (String? value) {},
                onSaved: (value) => null,

                isExpanded: false,
                style: context.textTheme.paragraphContrast,
                icon: Icon(Icons.arrow_drop_down, color: Colors.black,),
                iconSize: 25,
                dropdownColor: context.textFieldBackground,

                borderRadius: BorderRadius.circular(5),
                decoration: InputDecoration(
                  floatingLabelBehavior: FloatingLabelBehavior.never,
                  isCollapsed: true,
                  border: InputBorder.none,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  _saveForm(){
    if(_formKey.currentState?.validate() ?? false){
      _formKey.currentState!.save();
    }
  }
}

【问题讨论】:

    标签: flutter forms drop-down-menu


    【解决方案1】:

    问题出在父母Container()

    Container(
                  // Set this -> width: 100,
                  padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                  color: Colors.white,
                  child: DropdownButtonFormField<String>(
                    isDense: true,
                    items: [
                      DropdownMenuItem<String>(
                        child: Text('test 1'),
                        value: "test 1",
                      ),
    

    解决它的一种方法是为Container设置一个固定的width

    【讨论】:

      猜你喜欢
      • 2020-02-06
      • 2022-10-17
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多