【问题标题】:using list of options in textFormField使用 textFormField 中的选项列表
【发布时间】:2021-11-03 03:15:06
【问题描述】:

我正在开发一个flutter应用程序,在应用程序中有一个包含一些textFormfields的注册表格,

如下:

在性别字段中,应该有一个选项列表{男性,女性,其他}用户可以自己选择,我不知道如何创建它。 有什么想法???

【问题讨论】:

  • 在flutter中使用下拉菜单或根据要求自定义。
  • 如何将下拉菜单与 textFormField 连接起来?

标签: flutter flutter-layout


【解决方案1】:

您可以尝试使用 pud.dev 上提供的 dropdownfield 包。这是您的用例的示例。 您可以在表单小部件中使用/不使用其他文本表单字段来使用验证器。

DropDownField(
    textStyle: TextStyle(
        color: Colors.black,
    ),
    hintStyle: TextStyle(
        color: Colors.grey,
    ),
    hintText: "Gender",
    controller: genderController,
    value: genderController.text,
    items: ["Male","Female","Others"],
    onValueChanged: (value) {
        //process logic over here on value change
    },
    setter: (value) {
        //set new value
    },
),

希望这能为您提供解决方案。但对于像性别这样的领域,我建议使用下拉菜单。当有很多选项可供选择时使用这种字段。

【讨论】:

    【解决方案2】:

    您可以为此使用颤振 DropDownButton。这是文档中的代码,您可以自己尝试:

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

    欲了解更多信息,您可以访问here

    【讨论】:

    • 以及如何将其与表单连接起来?
    • 表单有孩子,这将是其中之一。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多