简体   繁体   English

如何使用 Flutter 和 Dart 中的下拉菜单从另一个小部件更新小部件?

[英]How To Update A Widget from Another Widget with A DropDown in Flutter and Dart?

I have two separate stateful widgets.我有两个独立的有状态小部件。 One is a dropdown with values and one is a widget I would like to update onChange of the DropDown .一个是带有值的下拉列表,一个是我想更新DropDownonChange的小部件。 My issue is that the second widget does not update whenever the dropdown is changed.我的问题是,无论何时更改下拉列表,第二个小部件都不会更新。 Below is my full code.下面是我的完整代码。

Dropdown Widget下拉小部件

import 'package:flutter/material.dart';

class DropDownList extends StatefulWidget {
  List<String> values;
  String select;

  DropDownList({
    Key key,
    this.values,
    this.select,
  }) : super(key: key);

  @override
  _DropDownListState createState() => _DropDownListState();
}

class _DropDownListState extends State<DropDownList> {
  List<String> dropdownValues = [
    "2021",
    "2020",
    "2019",
    "2018",
  ];
  var selectedValue = '2021';
  @override
  void initState() {
    setState(() {
      widget.values = dropdownValues;
      widget.select = selectedValue;
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 20.0),
      child: DropdownButton<String>(
        value: selectedValue,
        icon: SizedBox.shrink(),
        // iconSize: 24,
        elevation: 0,
        style: TextStyle(color: Colors.white),
        selectedItemBuilder: (BuildContext context) {
          return dropdownValues.map((String value) {
            return Text(
              value,
              style: TextStyle(
                color: Colors.white,
                fontWeight: FontWeight.bold,
              ),
            );
          }).toList();
        },
        underline: SizedBox.shrink(),
        onChanged: (String newValue) {
          setState(() {
            selectedValue = newValue;
          });
          showJojo(selectedValue);
          print(selectedValue);
        },
        items: widget.values.map<DropdownMenuItem<String>>((String value) {
          return DropdownMenuItem<String>(
            value: value,
            child: Text(
              '${value}',
              style: TextStyle(
                color: Colors.deepPurple,
              ),
            ),
          );
        }).toList(),
      ),
    );
  }

  static showJojo(String select) {
    return Jojo(
      select: select,
    );
  }
}

Second Widget To get the data第二个小部件获取数据

class _JojoState extends State<Jojo> {
  String select;
  _JojoState(this.select);
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: new Text(
        '${this.select}',
        style: TextStyle(color: Colors.white),
      ),
    );
  }
}

Can someone point me in the right direction, please?有人可以指出我正确的方向吗? Amicably Chris友好的克里斯

You need to add didChangeDependencies() method here, here if that variable will change page will be compiled.您需要在此处添加 didChangeDependencies() 方法,如果该变量将更改页面将被编译。

  void didChangeDependencies() {
    super.didChangeDependencies();
    setState(() {
      if (selectedValue != null) return;
    });
  }```

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

相关问题 如何从另一个应用程序更新小部件? - How to update widget from another application? 从 Flutter 中的另一个 dart 文件在有状态小部件 class 下使用 SetState 方法调用 void - Call void with SetState method under stateful widget class from another dart file in Flutter 如何将下拉菜单值传递给另一个函数或小部件? 颤振/飞镖 - How to pass Drop down menu value to another func or widget? Flutter/Dart 如何在 Flutter 中将事件从一个有状态小部件广播到另一个小部件 - How To Broadcast An Event From One Stateful Widget To Another In Flutter Flutter 如何从另一个小部件访问 AnimationController 方法 - Flutter how to access AnimationController methods from another widget 如何在 Flutter 中从另一个有状态小部件更改一个有状态小部件的状态? - How to change state of one Stateful Widget from another Stateful Widget in Flutter? 从原生 Android 主屏幕小部件调用 Flutter (Dart) 代码 - Invoke Flutter (Dart) code from native Android home screen widget 从另一个小部件 Flutter 更改文本字段的 textSize - Change textSize of textfield from another widget Flutter 如何在 Flutter 中更新 showmodalsheet 中的小部件颜色? - How to update the widget color in showmodalsheet in Flutter? 如何将其他 dart 文件导入 flutter 中的有状态小部件? - How can I import other dart file to stateful widget in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM