flutter - 具有 setState 的类的 Flutter Update 属性
问题描述
我有一个小部件页面,用户可以在其中输入代码的一些数据,如下所示:
class Container extends StatefulWidget {
@override
_ContainerState createState() => _ContainerState();
}
class _ContainerState extends State<Container> {
@override
Widget build(BuildContext context) {
int height= 5;
int width = 2;
HelperClass helper = HelperClass(
height: height,
width: width,
);
return Column(
children: [TextFormField(
onChanged: (newValue) {
setState(() {
height = int.tryParse(newValue);
});
print(height); // State changed OK
},
),
Text('Result: ${helper.calculate()}');
]
);
}
所以我有我的 HelperClass,我在其中存储和处理如下所示的数据:
class HelperClass {
int height;
int width;
AplanadoMA(
{this.height,
this.width,});
int calculate() {
print(longitude); // This value doesn't change despite setState is used in the other class
return height * width;
}
}
问题是,尽管在用户输入数据的类中,变量随 setState 变化,但数据并未在“HelperClass”处更新。
我该如何解决这个问题?
谢谢!
解决方案
您可以在下面复制粘贴运行完整代码您可以在
代码片段
中初始化并设置helper
initState
helper.height = height;
onChanged
class _YourContainerState extends State<YourContainer> {
int height = 5;
int width = 2;
HelperClass helper;
@override
void initState() {
helper = HelperClass(
height: height,
width: width,
);
super.initState();
}
...
onChanged: (newValue) {
if (newValue.isEmpty) {
newValue = "0";
}
setState(() {
height = int.tryParse(newValue);
helper.height = height;
});
print(height); // State changed OK
},
工作演示
完整代码
import 'package:flutter/material.dart';
class HelperClass {
int height;
int width;
HelperClass({
this.height,
this.width,
});
int calculate() {
//print(longitude); // This value doesn't change despite setState is used in the other class
return height * width;
}
}
class YourContainer extends StatefulWidget {
@override
_YourContainerState createState() => _YourContainerState();
}
class _YourContainerState extends State<YourContainer> {
int height = 5;
int width = 2;
HelperClass helper;
@override
void initState() {
helper = HelperClass(
height: height,
width: width,
);
super.initState();
}
@override
Widget build(BuildContext context) {
return Column(children: [
TextFormField(
onChanged: (newValue) {
if (newValue.isEmpty) {
newValue = "0";
}
setState(() {
height = int.tryParse(newValue);
helper.height = height;
});
print(height); // State changed OK
},
),
Text('Result: ${helper.calculate()}')
]);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
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++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(child: YourContainer()),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
推荐阅读
- javascript - 在这个例子中 `this` 有什么用?
- scala - 改进使用 scala 在 List 中查找奇数事件的解决方案
- javascript - 使用 .indexOf() 定义 .getRange()
- python - 我想我在这个 tkinter 小部件上犯了一个错误
- elasticsearch - ElasticSearch Count API vs SearchAPI,track_total_hits 设置为 true
- azure - 与团队中的其他人共享 Azure 项目积压设置
- javascript - 阻止特定域路径中的 Gatsby 页面出现在 Google 搜索中
- oracle - 有谁知道 PL/SQL 查询我需要这样做,我添加了我的代码
- javascript - 根据字典值动态突出显示单词
- javascript - GooglePlacesAutocomplete - 将信息传递给父组件并定义语言