简体   繁体   English

Flutter DropdownButton 在 StatefulBuilder 中没有更新

[英]Flutter DropdownButton doesn't update in StatefulBuilder

I am using List items for DropdownButton widget in StatefulBuilder.我在 StatefulBuilder 中为 DropdownButton 小部件使用列表项。 When I select any item on DropdownButton its not updating at this stage.当我 select DropdownButton 上的任何项目在此阶段没有更新时。 But when I close the showModalBottomSheet widget and open again its updating.但是当我关闭 showModalBottomSheet 小部件并再次打开它的更新时。 How can I fix this?我怎样才能解决这个问题? Thanks for help.感谢帮助。

This is my menu code for items这是我的项目菜单代码

List<DropdownMenuItem<String>> tur = [];
void kategoritur(){
tur = [];

tur.add(new DropdownMenuItem(
  child: Row(
    children: <Widget>[
      Container(

        decoration: BoxDecoration(

          image: DecorationImage(
            image: AssetImage(
                'assets/g.png'),
            fit: BoxFit.fitWidth,
          ),
        ),
        height: 50, width: 50,
      ),
      SizedBox(width: 30,),
      new Text("Şırınga"),
    ],
  ),
  value: "Siringa",)
);
tur.add(new DropdownMenuItem(
  child: Row(
    children: <Widget>[
      Container(

        decoration: BoxDecoration(

          image: DecorationImage(
            image: AssetImage(
                'assets/t.png'),
            fit: BoxFit.fitWidth,
          ),
        ),
        height: 50, width: 50,
      ),
      SizedBox(width: 30,),
      new Text("Hap"),
    ],
  ),
  value: "Hap",)
);
tur.add(new DropdownMenuItem(
  child: Row(
    children: <Widget>[
      Container(

        decoration: BoxDecoration(

          image: DecorationImage(
            image: AssetImage(
                'assets/f.png'),
            fit: BoxFit.fitWidth,
          ),
        ),
        height: 50, width: 50,
      ),
      SizedBox(width: 30,),
      new Text("Şişe"),
    ],
  ),
  value: "Sise",)
);
tur.add(new DropdownMenuItem(
  child: Row(
    children: <Widget>[
      Container(

        decoration: BoxDecoration(

          image: DecorationImage(
            image: AssetImage(
                'assets/e.png'),
            fit: BoxFit.fitWidth,
          ),
        ),
        height: 50, width: 50,
      ),
      SizedBox(width: 30,),
      new Text("Damla"),
    ],
  ),
  value: "Damla",)
);

}

This is my main code这是我的主要代码

                                showModalBottomSheet(
                                useRootNavigator: true,
                                context: context,
                                clipBehavior: Clip.antiAlias,
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.vertical(
                                    top: Radius.circular(24),
                                  ),
                                ),


                              builder: (context) {
                                  return SingleChildScrollView(
                                    scrollDirection: Axis.vertical,
                                    child: StatefulBuilder(
                                      builder: (context, setModalState) {
                                        return Container(
                                          padding: const EdgeInsets.all(32),
                                          child: Column(
                                            children: [
                                              FlatButton(
                                                onPressed: () async {
                                                  var selectedTime =
                                                  await showTimePicker(


                                                    builder: (BuildContext context, Widget child) {
                                                      return MediaQuery(
                                                        data: MediaQuery.of(context)
                                                            .copyWith(alwaysUse24HourFormat: false),
                                                        child: Theme(
                                                            data: ThemeData.dark().copyWith(
                                                              colorScheme: ColorScheme.dark(
                                                                primary: Color(0xFF52b788),
                                                                onPrimary: Color(0xFF52b788),
                                                                surface: Colors.white,
                                                                onSurface:  Color(0xFF52b788),
                                                              ),
                                                              dialogBackgroundColor: 
                                                             Color(0xFF52b788),
                                                            ),
                                                            child: child),
                                                      );
                                                    },


                                                    context: context,
                                                    initialTime:
                                                    TimeOfDay.now(),
                                                  );
                                                  if (selectedTime != null) {
                                                    final now = DateTime.now();
                                                    var selectedDateTime =
                                                    DateTime(
                                                        now.year,
                                                        now.month,
                                                        now.day,
                                                        selectedTime.hour,
                                                        selectedTime
                                                            .minute);
                                                    _alarmTime =
                                                        selectedDateTime;
                                                    setModalState(() {
                                                      _alarmTimeString =
                                                          DateFormat('HH:mm')
                                                              .format(
                                                              selectedDateTime);
                                                    });
                                                  }
                                                },
                                                child: Text(
                                                  _alarmTimeString,
                                                  style:
                                                  TextStyle(fontSize: 32),
                                                ),
                                              ),



                          

                                               

                                             


                                              Container(
                                                margin: EdgeInsets.all(10),
                                                decoration: BoxDecoration(
                                                    color: Colors.black38,
                                                    borderRadius: BorderRadius.circular(18)
                                                ),
                                                width: _width,
                                                padding: EdgeInsets.fromLTRB(14, 0, 14, 0),
                                                child: DropdownButton(
                                                  style:  GoogleFonts.montserrat(
                                                      color:  Colors.white,
                                                      fontSize: 16,
                                                      textStyle: TextStyle(fontWeight: 
                                                    FontWeight.w300)),
                                                  dropdownColor: Colors.cyan[700],
                                                  underline: SizedBox.shrink(),

                                                  items: tur,
                                                  onChanged: (value){
                                                    turdata = value;
                                                    setState(() {


                                                    });
                                                  },
                                                  hint: new Text("İlaç Türü", textAlign: 
                                                    TextAlign.center
                                                    ,
                                                    style: GoogleFonts.montserrat(
                                                        color:  Colors.white,
                                                        fontSize: 16,
                                                        textStyle: TextStyle(fontWeight: 
                                                   FontWeight.w300)),
                                                  ),
                                                  value: turdata,
                                                ),
                                              ),


                                              
                                              SizedBox(height: 20,),
                                              FloatingActionButton.extended(
                                                backgroundColor: Color(0xFF52b788),
                                                onPressed: onSaveAlarm,
                                                icon: Icon(Icons.alarm),
                                                label: Text('Save'),
                                              ),
                                            ],
                                          ),
                                        );
                                      },
                                    ),
                                  );
                                },
                              );

i made a custom class of dropdown you can customize this in your own way我制作了一个自定义的 class 下拉菜单,您可以按照自己的方式进行自定义

    import 'package:flutter/material.dart';
    import 'package:rio/Utils/utils.dart';
    import 'package:rio/widgets/widgets.dart';
    import 'package:rio/generated/l10n.dart';
    import 'package:responsive_flutter/responsive_flutter.dart';
    import 'color.dart';
    
    // ignore: must_be_immutable
    class DropDownClass extends StatelessWidget {
      var _hint;
      var _val;
      double _fontSize;
      List _list = new List();
      bool _icon;
      bool _border;
      Color _underLineColor, _dropDownColor, _textColor;
    
      List get list => _list;
      dynamic Function(dynamic) _listener;
    
      DropDownClass({List list,
        var hint,
        Color underLineColor,
        Color dropDownColor,
        Color textColor,
        double fontSize,
        bool icon,
        var val,
        int type,
        bool border = true,
        dynamic Function(dynamic) listener,})
          : _list = list,
            _hint = hint,
            _underLineColor = underLineColor,
            _dropDownColor = dropDownColor,
            _textColor = textColor,
            _icon = icon,
            _val = val,
            _fontSize = fontSize,
            _border = border,
            _listener = listener;
    
      @override
      Widget build(BuildContext context) {
        return InkWell(
          onTap: () {
            Utils.selectedValue = null;
          },
          child: DropdownButtonHideUnderline(
            child: DropdownButtonFormField<String>(
              value: _val,
              dropdownColor: _dropDownColor ?? CustomColors.white,
              decoration:_border == true?  InputDecoration(
                  enabledBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                      color: _underLineColor ?? Theme
                          .of(context)
                          .hintColor,
                      width: 1.0,
                    ),
                  )
              ):InputDecoration(
                border: InputBorder.none,
                fillColor: Theme.of(context).cardColor,
                filled: true),
              style: robotoStyle(context,
                  color: _textColor ??
                      Theme
                          .of(context)
                          .primaryTextTheme
                          .bodyText1
                          .color,
                  fontWeight: FontWeight.w400,
                  fontSize: _icon == null || _icon == false
                      ? ResponsiveFlutter.of(context).fontSize(_fontSize??1.5)
                      : ResponsiveFlutter.of(context).fontSize(_fontSize??1.5)),
              isExpanded: true,
              icon: _icon == null || _icon == false
                  ? null
                  : Icon(
                Icons.keyboard_arrow_down,
                color: CustomColors.appBarTextColor,
              ),
              hint: Text(_hint,
                  style: robotoStyle(context,
                      color: _icon == null || _icon == false
                          ? Theme
                          .of(context)
                          .hintColor
                          : Theme
                          .of(context)
                          .textSelectionColor,
                      fontWeight: FontWeight.w400,
                      fontSize: _icon == null || _icon == false
                          ? ResponsiveFlutter.of(context)
                          .fontSize(_fontSize??1.75)
                          : ResponsiveFlutter.of(context).fontSize(_fontSize??1.5))),
              items: list.map((item) {
                return DropdownMenuItem<String>(
                  value: item,
                  child: new Text(item,
                      style: robotoStyle(context,
                          color: _textColor ?? CustomColors.dropDownTextColor,
                          fontWeight: FontWeight.w400,
                          fontSize: _icon == null || _icon == false
                              ? ResponsiveFlutter.of(context)
                              .fontSize(_fontSize??1.75)
                              : ResponsiveFlutter.of(context).fontSize(_fontSize??1.5))),
                );
              }).toList(),
              onChanged: (value) {
                _val = value;
    
                if (_listener != null) _listener.call(value);
                if (_icon == true) {
                  Utils.selectedValue = value;
                  S.load(Locale(value));
                  Utils.preferences.setString("lang", value);
    
    
    
                }
                //   return val;
              },
            ),
          ),
        );
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM