当页面滚动时,所选的下拉值将在 flutter 中更改

When the page is scrolling the selected dropdown value will change in flutter

提问人:Mari Selvan 提问时间:11/4/2022 最后编辑:James ZMari Selvan 更新时间:9/19/2023 访问量:244

问:

我将更改一个下拉列表,然后滚动屏幕。所选的下拉值将被更改。1lakhRs. 6000

enter image description here

请参阅我的下拉自定义小部件 我会将所有细节传递给小部件。title、DropdownMenuItems、回调函数,并给出一个初始选择的值。

import 'package:flutter/material.dart';
import 'package:insurance/widgets/common/colors.dart';

class DropdownWidget extends StatefulWidget {
  const DropdownWidget(
      {super.key,
      required this.title,
      required this.dropdownList,
      required this.selectedValue,
      required this.callback});
  final String title;
  final List<String> dropdownList;
  final String selectedValue;
  final Function callback;

  @override
  State<DropdownWidget> createState() => _DropdownWidgetState();
}

class _DropdownWidgetState extends State<DropdownWidget> {
  late String title = widget.title, selectedValue = widget.selectedValue;
  late List<String> dropdownList = widget.dropdownList;
  late Function callback = widget.callback;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 50,
      width: double.infinity,
      child: ListTile(
        title: SizedBox(
          width: MediaQuery.of(context).size.width * 0.5,
          child: Row(
            children: [
              Text(
                title,
                style: const TextStyle(
                    fontSize: 14,
                    fontFamily: 'Montserrat',
                    fontWeight: FontWeight.w600),
                overflow: TextOverflow.ellipsis,
              ),
              Visibility(
                  child: title == 'Seating Capacity'
                      ? Text(
                          ' *',
                          style: TextStyle(color: AppColor.red),
                        )
                      : const Text(''))
            ],
          ),
        ),
        trailing: Container(
          width: MediaQuery.of(context).size.width * 0.40,
          height: 50,
          padding: const EdgeInsets.only(left: 10),
          decoration: BoxDecoration(
              color: AppColor.white,
              borderRadius: const BorderRadius.all(Radius.circular(7))),
          child: DropdownButtonHideUnderline(
            child: DropdownButton(
              value: selectedValue,
              borderRadius: const BorderRadius.all(Radius.circular(7)),
              items: dropdownList.map((String value) {
                return DropdownMenuItem(
                  value: value,
                  child: SizedBox(
                    width: MediaQuery.of(context).size.width * 0.30,
                    child: Text(
                      value.toString(),
                      style: TextStyle(
                          fontSize: 14,
                          fontFamily: 'Montserrat',
                          color: AppColor.black,
                          fontWeight: FontWeight.w500),
                      overflow: TextOverflow.clip,
                    ),
                  ),
                );
              }).toList(),
              onChanged: (String? selectedData) {
                print('hello');
                callback(selectedData);

                setState(() {
                  selectedValue = selectedData!;
                });
              },
            ),
          ),
        ),
      ),
    );
  }
}

以及颤振开关按钮上的相同问题 我该怎么办?

Flutter 下拉菜单 dynamic-ui

评论

0赞 Gwhyyy 11/4/2022
你能告诉我们到底是什么问题吗
0赞 Mari Selvan 11/4/2022
确切的问题是。当我更改值然后滚动屏幕时,使用自定义下拉小部件最初效果很好。所选值将被更改
1赞 Md. Yeasin Sheikh 11/4/2022
尝试使用AutomaticKeepAliveClientMixin mixin

答:

0赞 Mandip Shrestha 9/19/2023 #1

如果您直接在 ListView 小部件中直接使用自定义 Dropdown 小部件,请替换为 SingleChildScrollview(子项:Column( 子项:[ DropdownWidget]),