Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振:如何做好正确的状态管理_Flutter_Dart_State Management - Fatal编程技术网

Flutter 颤振:如何做好正确的状态管理

Flutter 颤振:如何做好正确的状态管理,flutter,dart,state-management,Flutter,Dart,State Management,我是一个初学者,我对颤振中的状态管理有一个问题,我想做的是,当我在一个有状态的小部件类中按下一个按钮时,另一个在另一个文件中的类只是一个包含字符串的简单类,该字符串应该被更改,我不会这样做,请看这两门课 这是必须更改其数据的class Values.dart class Values { String equation = '2+2'; String result = ''; double equationFontSize = 30; double resultFontSiz

我是一个初学者,我对颤振中的状态管理有一个问题,我想做的是,当我在一个有状态的小部件类中按下一个按钮时,另一个在另一个文件中的类只是一个包含字符串的简单类,该字符串应该被更改,我不会这样做,请看这两门课

这是必须更改其数据的class Values.dart

    class Values {
  String equation = '2+2';
  String result = '';
 double equationFontSize = 30;
 double resultFontSize = 25;

  }
这是Functions.dart,它包含有状态小部件

`
import 'package:calculator/values.dart';
import 'package:flutter/material.dart';


class Functions extends StatefulWidget {
  @override
  _FunctionsState createState() => _FunctionsState();
}

class _FunctionsState extends State<Functions> {
  @override
  Widget build(BuildContext context) {
    Color bgColors = Colors.black87;
    return Row(
      children: <Widget>[
        Container(
          width: MediaQuery.of(context).size.width * 0.75,
          child: Table(
            border: TableBorder.all(
              color: Colors.white,
              width: .1,
            ),
            children: [
              TableRow(
                children: [
                  buildButton(bgColors: bgColors,color: Colors.white,text: 'C', height: 1.0,),
                  buildButton(bgColors:bgColors,color:Colors.white, height:1.0,text:'+/-'),
                  buildButton(bgColors:bgColors,color:Colors.white, height:1.0,text:'%'),
                ]
              ),

              TableRow(
                  children: [
                    buildButton(bgColors:bgColors,color:Colors.white,height: 1.0,text:'7'),
                    buildButton(bgColors:bgColors,color:Colors.white, height:1.0,text:'8'),
                    buildButton(bgColors:bgColors,color:Colors.white,height: 1.0,text:'9'),
                  ]
              ),
              TableRow(
                  children: [
                    buildButton(bgColors:bgColors,color:Colors.white, height:1.0,text:'4'),
                    buildButton(bgColors:bgColors,color:Colors.white, height:1.0,text:'5'),
                    buildButton(bgColors:bgColors,color:Colors.white, height:1.0,text:'6'),
                  ]
              ),
              TableRow(
                  children: [
                    buildButton(bgColors:bgColors,color:Colors.white, height:1.0,text:'1'),
                    buildButton(bgColors:bgColors,color:Colors.white, height:1.0,text:'2'),
                    buildButton(bgColors:bgColors,color:Colors.white, height:1.0,text:'3'),
                  ]
              ),
              TableRow(
                  children: [
                    buildButton(bgColors:bgColors,color:Colors.white, height:1.0,text:'0'),
                    buildButton(bgColors:bgColors,color:Colors.white, height:1.0,text:'.'),
                    buildButton(bgColors:bgColors,color:Colors.white, height:1.0,text:'del'),
                  ]
              ),
            ],
          ),
        ),

        Container(
          width: MediaQuery.of(context).size.width * 0.25,

         color:Colors.red,
         child: Table(
           border: TableBorder.all(
             color: Colors.white,
             width: .2,
           ),
           children: [
             TableRow(
                 children:[

                   buildButton(bgColors:Colors.deepPurpleAccent,color:Colors.black,height :0.8, text:'÷'),
                 ]
             ),
             TableRow(
                 children:[

                   buildButton(bgColors:Colors.deepPurpleAccent,color:Colors.black,height: 0.9, text:'×'),
                 ]
             ),
             TableRow(
                 children:[

                   buildButton(bgColors:Colors.deepPurpleAccent,color:Colors.black,height: 0.9, text:'-'),
                 ]
             ),
             TableRow(
                 children:[

                   buildButton(bgColors:Colors.deepPurpleAccent,color:Colors.black,height: 0.9,text: '+'),
                 ]
             ),
             TableRow(
               children:[

                 buildButton(bgColors:Colors.orange[300],color:Colors.black,height: 1.5, text:'='),
               ]
             ),

           ],
         ),
        )
      ],
    );
  }

}


  Values values = new Values();


class buildButton extends StatefulWidget {


  Color bgColors;
  Color color;
  String text;
double height;
buildButton({this.color,this.height,this.text,this.bgColors});
  @override

  _buildButtonState createState() => _buildButtonState();
}

class _buildButtonState extends State<buildButton> {


  @override



  Widget build(BuildContext context) {
    return  Container(
      color:widget.bgColors,
      height: MediaQuery.of(context).size.height * 0.1 * widget.height,
      child: FlatButton(


        onPressed: (){
       setState(() {
          values.result = 'hello functions';
          print(values.result);
          print('pressed');
       });
        },
        child: Text(
          widget.text,
          style: TextStyle(
            fontSize: 25,
            color:widget.color,
            fontWeight: FontWeight.w300,
          ),
        ),
      ),
    );
  }
}



`
`
导入“package:calculator/values.dart”;
进口“包装:颤振/材料.省道”;
类函数扩展StatefulWidget{
@凌驾
_函数状态createState()=>_函数状态();
}
类_函数状态扩展状态{
@凌驾
小部件构建(构建上下文){
颜色bgColors=Colors.black87;
返回行(
儿童:[
容器(
宽度:MediaQuery.of(context).size.width*0.75,
孩子:桌子(
边界:TableBorder.all(
颜色:颜色,白色,
宽度:.1,
),
儿童:[
桌椅(
儿童:[
buildButton(bgColors:bgColors,颜色:Colors.white,文本:“C”,高度:1.0,),
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:“+/-”),
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:“”),
]
),
桌椅(
儿童:[
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:'7'),
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:'8'),
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:'9'),
]
),
桌椅(
儿童:[
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:'4'),
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:'5'),
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:'6'),
]
),
桌椅(
儿童:[
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:'1'),
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:'2'),
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:'3'),
]
),
桌椅(
儿童:[
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:“0”),
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:'.'),
buildButton(bgColors:bgColors,颜色:Colors.white,高度:1.0,文本:'del'),
]
),
],
),
),
容器(
宽度:MediaQuery.of(context).size.width*0.25,
颜色:颜色,红色,
孩子:桌子(
边界:TableBorder.all(
颜色:颜色,白色,
宽度:.2,
),
儿童:[
桌椅(
儿童:[
buildButton(bgColors:Colors.deeppurpleCent,color:Colors.black,高度:0.8,文本:“÷”),
]
),
桌椅(
儿童:[
buildButton(bgColors:Colors.deeppurpleCent,color:Colors.black,高度:0.9,文本:“×”),
]
),
桌椅(
儿童:[
buildButton(bgColors:Colors.DeepPurpleCent,颜色:Colors.black,高度:0.9,文本:'-'),
]
),
桌椅(
儿童:[
buildButton(bgColors:Colors.deeppurpleCent,color:Colors.black,高度:0.9,文本:“+”),
]
),
桌椅(
儿童:[
buildButton(bgColors:Colors.orange[300],Colors:Colors.black,高度:1.5,文本:'='),
]
),
],
),
)
],
);
}
}
值=新值();
类buildButton扩展StatefulWidget{
颜色;
颜色;
字符串文本;
双高;
buildButton({this.color,this.height,this.text,this.bgColors});
@凌驾
_buildButtonState createState()=>\u buildButtonState();
}
类_buildButtonState扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器(
颜色:widget.bgColors,
高度:MediaQuery.of(context).size.height*0.1*widget.height,
孩子:扁平按钮(
已按下:(){
设置状态(){
values.result='hello functions';
打印(值、结果);
打印(“按下”);
});
},
子:文本(
widget.text,
样式:TextStyle(
尺寸:25,
颜色:widget.color,
fontWeight:fontWeight.w300,
),
),
),
);
}
}
`

您可以复制粘贴运行下面的完整代码
您可以在父窗口小部件(
MyHomePage
)中定义
回调(刷新)

回调(刷新)
函数将执行
setState

然后您可以将此
回调
传递给子小部件
函数
构建按钮

代码片段

class buildButton extends StatefulWidget {
      ...
      Function callback;

      buildButton(
          {this.color, this.height, this.text, this.bgColors, this.callback});

...       
buildButton(
                      bgColors: bgColors,
                      color: Colors.white,
                      text: 'C',
                      height: 1.0,
                      callback: widget.callback,
                    )
...                 
onPressed: () {
              setState(() {
                values.result = 'hello functions';
                print(values.result);
                print('pressed');
              });
              widget.callback();
            }                   

//MyHomePage
refresh() {
        setState(() {});
      }                 
...
Expanded(
                    child: Functions(
                  callback: refresh,
                ))                  
工作演示

完整代码

import 'package:flutter/material.dart';

class Values {
  String equation = '2+2';
  String result = '';
  double equationFontSize = 30;
  double resultFontSize = 25;
}

class Functions extends StatefulWidget {
  VoidCallback callback;
  Functions({this.callback});

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

class _FunctionsState extends State<Functions> {
  @override
  Widget build(BuildContext context) {
    Color bgColors = Colors.black87;
    return Row(
      children: <Widget>[
        Container(
          width: MediaQuery.of(context).size.width * 0.75,
          child: Table(
            border: TableBorder.all(
              color: Colors.white,
              width: .1,
            ),
            children: [
              TableRow(children: [
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  text: 'C',
                  height: 1.0,
                  callback: widget.callback,
                ),
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '+/-',
                  callback: widget.callback,
                ),
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '%',
                  callback: widget.callback,
                ),
              ]),
              TableRow(children: [
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '7',
                  callback: widget.callback,
                ),
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '8',
                  callback: widget.callback,
                ),
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '9',
                  callback: widget.callback,
                ),
              ]),
              TableRow(children: [
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '4',
                  callback: widget.callback,
                ),
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '5',
                  callback: widget.callback,
                ),
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '6',
                  callback: widget.callback,
                ),
              ]),
              TableRow(children: [
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '1',
                  callback: widget.callback,
                ),
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '2',
                  callback: widget.callback,
                ),
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '3',
                  callback: widget.callback,
                ),
              ]),
              TableRow(children: [
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '0',
                  callback: widget.callback,
                ),
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: '.',
                  callback: widget.callback,
                ),
                buildButton(
                  bgColors: bgColors,
                  color: Colors.white,
                  height: 1.0,
                  text: 'del',
                  callback: widget.callback,
                ),
              ]),
            ],
          ),
        ),
        Container(
          width: MediaQuery.of(context).size.width * 0.25,
          color: Colors.red,
          child: Table(
            border: TableBorder.all(
              color: Colors.white,
              width: .2,
            ),
            children: [
              TableRow(children: [
                buildButton(
                    bgColors: Colors.deepPurpleAccent,
                    color: Colors.black,
                    height: 0.8,
                    text: '÷'),
              ]),
              TableRow(children: [
                buildButton(
                    bgColors: Colors.deepPurpleAccent,
                    color: Colors.black,
                    height: 0.9,
                    text: '×'),
              ]),
              TableRow(children: [
                buildButton(
                    bgColors: Colors.deepPurpleAccent,
                    color: Colors.black,
                    height: 0.9,
                    text: '-'),
              ]),
              TableRow(children: [
                buildButton(
                    bgColors: Colors.deepPurpleAccent,
                    color: Colors.black,
                    height: 0.9,
                    text: '+'),
              ]),
              TableRow(children: [
                buildButton(
                    bgColors: Colors.orange[300],
                    color: Colors.black,
                    height: 1.5,
                    text: '='),
              ]),
            ],
          ),
        )
      ],
    );
  }
}

Values values = new Values();

class buildButton extends StatefulWidget {
  Color bgColors;
  Color color;
  String text;
  double height;
  Function callback;

  buildButton(
      {this.color, this.height, this.text, this.bgColors, this.callback});
  @override
  _buildButtonState createState() => _buildButtonState();
}

class _buildButtonState extends State<buildButton> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.bgColors,
      height: MediaQuery.of(context).size.height * 0.1 * widget.height,
      child: FlatButton(
        onPressed: () {
          setState(() {
            values.result = 'hello functions';
            print(values.result);
            print('pressed');
          });
          widget.callback();
        },
        child: Text(
          widget.text,
          style: TextStyle(
            fontSize: 25,
            color: widget.color,
            fontWeight: FontWeight.w300,
          ),
        ),
      ),
    );
  }
}

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  refresh() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    print(values.result);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(' Result ${values.result}'),
            Expanded(
                child: Functions(
              callback: refresh,
            )),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
阶级价值观{
字符串方程='2+2';
字符串结果=“”;
双方程FontSize=30;
double resultFontSize=25;
}
类函数扩展StatefulWidget{
无效回调;
函数({this.callback});
@凌驾
_函数状态createState()=>_函数状态();
}
类_函数状态扩展状态