使用下拉菜单时我无法更改文本值 - 颤动

时间:2021-02-10 20:49:07

标签: flutter

我对 flutter 非常陌生,最近我开始使用 PopupMenuButton,我使用它来允许用户选择他们希望使用的语言,然后在容器中呈现所选语言(使用它作为它使它看起来像一个文本字段下拉菜单),

所以基本上当用户选择语言时 selectedlanguage 值实际上会改变并且应用程序的语言也会改变但容器内的文本永远不会改变

import 'package:Terzi/main.dart';
import 'package:flutter/material.dart';

class Testable extends StatefulWidget {
  @override
  _TestableState createState() => _TestableState();
}

class _TestableState extends State<Testable> {

  @override
  Widget build(BuildContext context) {
    final TextEditingController _controller = new TextEditingController();
    var languages = ['English','اللغة العربية','Türkçe'];
    String slectedLanguage="English";
    return  new Scaffold(

        body: new Center(
          child: new Container(
            child: new Column(
              children: [
                new Padding(
                  padding: const EdgeInsets.all(24.0),
                  child: new Row(
                    children: <Widget>[
                      new Expanded(
                          child: Container(
                            decoration: BoxDecoration(
                              color: Colors.blue

                            ),
                            height: 65, width: double.infinity,
                            alignment: Alignment.center,
                            child: Text(slectedLanguage,style: TextStyle(color: Colors.white,fontSize: 16),),
                          ),
                      ),
                      new PopupMenuButton<String>(
                        icon: const Icon(Icons.arrow_drop_down),
                        onSelected: (String value) {
                          setState(() {
                            slectedLanguage= value;
                            switch( slectedLanguage)
                            {
                              case "English" :
                                MyApp.setLocale(context, Locale('en','US'));
                                break;
                              case "اللغة العربية" :
                                MyApp.setLocale(context, Locale('ar','SA'));
                                break;
                              case "Türkçe" :
                                MyApp.setLocale(context, Locale('tr','TR'));
                                break;
                            }
                            print(slectedLanguage);
                          });

                        },
                        itemBuilder: (BuildContext context) {
                          return languages.map<PopupMenuItem<String>>((String value) {
                            return new PopupMenuItem(child: new Text(value), value: value);
                          }).toList();
                        },
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      );

  }
}

1 个答案:

答案 0 :(得分:0)

您的 selectedLanguage 将始终为 'English',因为它是在您的 build 方法开始时初始化的局部变量。相反,将 selectedLanguage 初始化为 State 类 _TestableState 的实例变量:

class Testable extends StatefulWidget {
  @override
  _TestableState createState() => _TestableState();
}

class _TestableState extends State<Testable> {
  String selectedLanguage = 'English';

  @override
  Widget build(BuildContext context) {
    final TextEditingController _controller = new TextEditingController();
    var languages = ['English', 'اللغة العربية', 'Türkçe'];
    return new Scaffold(
      body: new Center(
        child: new Container(
          child: new Column(
            children: [
              new Padding(
                padding: const EdgeInsets.all(24.0),
                child: new Row(
                  children: <Widget>[
                    new Expanded(
                      child: Container(
                        decoration: BoxDecoration(color: Colors.blue),
                        height: 65,
                        width: double.infinity,
                        alignment: Alignment.center,
                        child: Text(
                          selectedLanguage,
                          style: TextStyle(color: Colors.white, fontSize: 16),
                        ),
                      ),
                    ),
                    new PopupMenuButton<String>(
                      icon: const Icon(Icons.arrow_drop_down),
                      onSelected: (String value) {
                        setState(() {
                          selectedLanguage = value;
                          switch (selectedLanguage) {
                            case "English":
                              MyApp.setLocale(context, Locale('en', 'US'));
                              break;
                            case "اللغة العربية":
                              MyApp.setLocale(context, Locale('ar', 'SA'));
                              break;
                            case "Türkçe":
                              MyApp.setLocale(context, Locale('tr', 'TR'));
                              break;
                          }
                          print(selectedLanguage);
                        });
                      },
                      itemBuilder: (BuildContext context) {
                        return languages
                            .map<PopupMenuItem<String>>((String value) {
                          return new PopupMenuItem(
                              child: new Text(value), value: value);
                        }).toList();
                      },
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}