我是一个初学者,我对flutter中的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,
),
),
),
);
}
}
`
答案 0 :(得分:0)
您可以在下面复制粘贴运行完整代码
您可以在父窗口小部件(callback(refresh)
)中定义MyHomePage
此callback(refresh)
函数将执行setState
那么您可以将此callback
传递给子小部件Functions
和buildButton
代码段
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),
),
);
}
}