dart 在下拉列表中显示List< Map< String,Map< String,String>并使用嵌套值更新其他字段

wgx48brx  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(92)

我有表单字段“服务器名称”、“服务器IP”、“数据库”和“端口”。我需要的服务器名称字段是一个下拉列表,其值是从下面的List<Map<String, Map<String, String>>>serverName1serverName2。然后,当用户选择其中一个时,其他字段应使用适当的嵌套值(服务器IP、数据库、端口)进行更新。我该怎么做?

class GlobalsProvider {

List<Map<String, Map<String, String>>> getServers() {
  final serverList = [{
    'serverName1': {
      'Server IP': 'ip1',
      'Database': 'db1',
      'Port': 'port1'
    },
    'serverName2': {
      'Server IP': 'ip2',
      'Database': 'db2',
      'Port': 'port2'
    }
  }];

  return serverList;
}

}

@override
  Widget build(BuildContext context) {
    return Consumer<GlobalsProvider>(
        builder: (context, value, child) => Scaffold(
            appBar: AppBar(
              title: const Text('Login'),
              centerTitle: true,
              actions: [
                IconButton(
                    onPressed: () {},
                    icon: const Icon(Icons.add)
                ),
              ],
            ),
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                    flex: 2,
                    child: Visibility(
                      visible: true, 
                      child: Row(
                        children: [
                          const Expanded(child: Text('Server Name:')),
                          DropdownButton(
                            //? Not sure how to do this part
                          )
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: Visibility(
                      visible: true, 
                      child: Row(
                        children: [
                          const Expanded(child: Text('Server IP:')),
                          Expanded(
                            child: TextField(
                              controller: serverIPController,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: Visibility(
                      visible: true, 
                      child: Row(
                        children: [
                          const Expanded(child: Text('Database:')),
                          Expanded(
                            child: TextField(
                              controller: dbController,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: Visibility(
                      visible: true, 
                      child: Row(
                        children: [
                          const Expanded(child: Text('Port:')),
                          Expanded(
                            child: TextField(
                              controller: portController,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                 //...
bvjveswy

bvjveswy1#

您有一个列表项和两个服务器。在这种情况下,它将是

String? selectedServer;

  final TextEditingController serverIPController = TextEditingController();
  void onServerChanged(String? value) {
    setState(() {
      selectedServer = value; 
                                /// having single item on the list, so index is 0
      serverIPController.text = serverList[0][selectedServer]?['Server IP'] ?? "";
    });
  }
Row(
  children: [
    Text('Server'),
    Expanded(
      child: DropdownButton<String>(
        value: selectedServer,
        hint: const Text('Select Server'),
        items: serverList[0].keys.map((String value) {
          return DropdownMenuItem<String>(
            value: value,
            child: Text(value),
          );
        }).toList(),
        onChanged: onServerChanged,
      ),
    ),
  ],
),
Row(
  children: [
    const Expanded(child: Text('Server IP:')),
    Expanded(child: TextField(controller: serverIPController)),
  ],
),

相关问题