【问题标题】:Why doesent changes the displayed Text, if the String in the Text widget changes?为什么不改变显示的Text,Text小部件中的String发生变化?
【发布时间】:2019-08-30 18:06:12
【问题描述】:

我正在尝试构建一个 Switch Widget 来更改显示的文本。因此,我有带有 Switch 小部件的类:

class SwitchWidget extends StatelessWidget{

  static bool switchOn = false;

  void _onSwitchChanged(bool value) {   
      switchOn = false; 
    }

  @override
  Widget build(BuildContext context) {
    return  Switch( 
              onChanged: _onSwitchChanged,  
              value: switchOn,  
            );
        }
}

switchOn 表示开关是否打开。

然后我让 Widget 女巫将我的字符串文本的文本设置为“ON”,如果 switchOn 为真,则设置为“OFF”:

class SwichTextWidget extends StatelessWidget{
  static String text = "OFF";

  @override
  Widget build(BuildContext context){
    if (SwitchWidget.switchOn == true){
      text = "ON";
    }
    else{
      text = "OFF";
    }
  }  
}

在另一个类中,我现在使用我的小部件:

class MatrixPageOne extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
    ...
    Row( children:[
            Text("  Clock Mode"),
            SwitchWidget(),
            Text(SwichTextWidget.text),
            ]//Row children
          )

但如果我使用我的 Switch,文本将保持在关闭状态。我预计它不会像这样工作,但是让它工作的最简单方法是什么? 感谢您的每一个帮助!

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    Flutter 的做法是这样的:

    class MyStatefulWidget extends StatefulWidget {
      MyStatefulWidget({Key key}) : super(key: key);
    
      @override
      _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
    }
    
    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      bool switch = false;
    
      @override
      Widget build(BuildContext context) {
        return Center(
          child: SwitchListTile(
            title: Text('Clock Mode'),
            value: switch,
            onChanged: (bool value) {
              setState(() {
                switch = value;
              });
            },
            secondary: Icon(Icons.access_time), // or Text(switch ? "ON" : "OFF") instead of an Icon
          ),
        );
      }
    }
    

    【讨论】:

    • 嗯,如果我这样尝试,我的模拟器而不是应用程序中会出现错误。它说“没有找到材料小部件。ListTile 小部件需要一个材料小部件祖先。”第二个问题是,对于这个 State 类,我不知道如何将它带入我的 MatrixPageOne 类
    • 您可以像添加普通的无状态小部件一样添加此类。假设您在 Scaffold 主体中有一个 Column 小部件。 Column(children:[MyStatefullWidget]) 用 Scaffold 包裹它也应该修复材质错误。
    • 嗯,如果我像这样添加它:Row( children:[ Text(" Clock Mode"), _SwitchState, ] ) ), 它说“元素类型'Type'不能分配给列表类型'Widget'”
    • 哦,我想我弄错了,我的页面中需要 Widget-Class 而不是 State-Class。发现后听起来有点明显^^
    • ok 问题出在容器里,因为容器空间太大,现在一切正常,谢谢!
    【解决方案2】:

    SwitchWidget_onSwitchChanged 的值设置为false,与传递的值无关:

    void _onSwitchChanged(bool value) {   
          switchOn = false; 
        }
    

    改成:

    void _onSwitchChanged(bool value) {   
          switchOn = value; 
        }
    

    而且文字会改变。

    【讨论】:

    • 啊是的,改变是必要的,但它仍然没有改变文本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2020-02-04
    • 2019-09-16
    相关资源
    最近更新 更多