【发布时间】:2020-06-08 07:43:34
【问题描述】:
我是一个初学者,我在颤动中遇到关于 stateManagement 的问题,我想做的是,当我按下有状态小部件类中的按钮时,另一个文件中的另一个类只包含一个字符串的简单类,该字符串应该被更改,它不会发生在我身上,请查看这两个类。
这是 Values.dart 类,其数据必须更改
class Values {
String equation = '2+2';
String result = '';
double equationFontSize = 30;
double resultFontSize = 25;
}
这是包含有状态小部件的 funtions.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,
),
),
),
);
}
}
`
【问题讨论】:
-
other class代码在哪里? -
查看最底部的第二个代码,它有一个有状态的小部件,还有 onpressed 方法。
-
我看到了方法,但我不知道谁使用了这些值。看看这个flutter.dev/docs/development/data-and-backend/state-mgmt
-
感谢@Kahou。这些文档真的很有帮助
标签: flutter dart state-management