DropdownMenu 小部件问题 = Flutter

问题描述 投票:0回答:1

我正在尝试为 DropDownButton 创建一个自定义小部件。看起来效果很好。当我在控制台打印更改后的值时, onChanged() 函数也可以工作。 但是,我无法在 Onchanged() 函数之后查看框中的文本。

有人可以让我知道同样的事情可能有什么问题吗? 我的小部件代码如下:

 import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    
    class InputScreen2 extends StatefulWidget {
      final List<double> sizes;
      const InputScreen2({Key? key, required this.sizes}) : super(key: key);
    
      @override
      State<InputScreen2> createState() => _InputScreen2State();
    }
    
    class _InputScreen2State extends State<InputScreen2> {
      Future? future;
      double screenwd = 0, screenht = 0;
      List<String> listsex = ['Male', 'Female'];
      String? sex;
    
      @override
      void initState() {
        // TODO: implement initState
        future = _future();
        super.initState();
      }
    
      Future<int> _future() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        screenwd = widget.sizes[0];
        screenht = widget.sizes[1];
    
        return 0;
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            backgroundColor: Colors.orange,
            body: FutureBuilder(
                future: future,
                builder: (context, snapshot) {
                  if (snapshot.hasError) {
                    return Center(
                        child: Text(
                      'Data Error...Please try again',
                      style: Theme.of(context).textTheme.titleSmall,
                    ));
                  } else if (snapshot.connectionState == ConnectionState.waiting) {
                    return Text(
                      'Waiting',
                      style: Theme.of(context).textTheme.titleSmall,
                    );
                  } else if (!snapshot.hasData) {
                    return Text(
                      'No Data',
                      style: Theme.of(context).textTheme.titleSmall,
                    );
                  } else if (snapshot.hasData) {
                    //Block I====================================================
                    return SafeArea(
                      child: Center(
                        child: Container(
                          padding: const EdgeInsets.all(20.0),
                          decoration: BoxDecoration(
                              border: Border.all(width: 2.0), color: Colors.blue),
                          width: screenwd * .8,
                          height: screenht * .75,
                          child: SingleChildScrollView(
                            child: Column(
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    const Text(
                                      'Personal Details',
                                    ),
                                    const SizedBox(
                                      width: 23,
                                    ),
                                    dropDownMenu('Sex', sex, listsex),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    );
                  }
                  return const Text('No DATA');
                }));
      }
    
      Widget dropDownMenu(String title, String? inputTxt, List<String> stringList) {
        return Container(
          margin: const EdgeInsets.only(top: 10.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                title,
              ),
              Container(
                width: 80,
                height: 35,
                decoration: BoxDecoration(
                    border: Border.all(color: Colors.white, width: 1.0),
                    borderRadius: const BorderRadius.all(Radius.circular(5.0))),
                child: DropdownButton<String>(
                    value: inputTxt,
                    dropdownColor: Colors.amber,
                    items: stringList.map<DropdownMenuItem<String>>((String value) {
                      return DropdownMenuItem<String>(
                          value: value,
                          child: Text(
                            value,
                            style: const TextStyle(color: Colors.white),
                          ));
                    }).toList(),
                    onChanged: (String? value) {
                      setState(() {
                        inputTxt = value!;
                      });
                    }),
              ),
            ],
          ),
        );
      }
    }
flutter widget dropdown dropdownbutton
1个回答
0
投票

我在 Dartpad 中尝试了你的代码:你必须用这个替换 dropDownMenu

onChanged

 onChanged: (String? value) {
                  setState(() {
                    sex = value!;
                  });
                }),

我想你只是把

inputTxt
sex
搞错了。也许您根本不需要
dropDownMenu
中的 inputTxt:只需将其替换为
sex
中的
_InputScreen2State
字段,如下所示:

Widget dropDownMenu(String title, List<String> stringList) {
    return Container(
      margin: const EdgeInsets.only(top: 10.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(title),
          Container(
            width: 80,
            height: 35,
            decoration: BoxDecoration(
                border: Border.all(color: Colors.white, width: 1.0),
                borderRadius: const BorderRadius.all(Radius.circular(5.0))),
            child: DropdownButton<String>(
                value: sex,
                dropdownColor: Colors.amber,
                items: stringList.map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                      value: value,
                      child: Text(
                        value,
                        style: const TextStyle(color: Colors.white),
                      ));
                }).toList(),
                onChanged: (String? value) {
                  setState(() {
                    sex = value!;
                  });
                }),
          ),
        ],
      ),
    );
  }

我在同一个 Dartpad 中测试了它,它有效。

© www.soinside.com 2019 - 2024. All rights reserved.