Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

android - 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. 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

question from:https://stackoverflow.com/questions/65859652/how-to-update-a-widget-from-another-widget-with-a-dropdown-in-flutter-and-dart

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to add didChangeDependencies() method here, here if that variable will change page will be compiled.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...