【问题标题】:How To Update A Widget from Another Widget with A DropDown in Flutter and Dart?如何在 Flutter 和 Dart 中使用 DropDown 从另一个 Widget 更新一个 Widget?
【发布时间】:2021-01-23 13:19:02
【问题描述】:

我有两个独立的有状态小部件。一个是带有值的下拉菜单,一个是我想更新 DropDown 的onChange 的小部件。我的问题是,无论何时更改下拉列表,第二个小部件都不会更新。以下是我的完整代码。

下拉小部件

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,
    );
  }
}

获取数据的第二个小部件

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),
      ),
    );
  }
}

请有人指出我正确的方向吗? 友好地 克里斯

【问题讨论】:

    标签: android ios flutter dart


    【解决方案1】:

    这里需要添加 didChangeDependencies() 方法,这里如果该变量会改变页面将被编译。

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

    【讨论】:

    • 嘿@Mustafa。我尝试了您的代码,但没有成功。你能更具体地说明你的代码吗?谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-06-14
    • 2021-04-23
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 2021-02-09
    相关资源
    最近更新 更多