首页 > 解决方案 > 具有 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”处更新。

我该如何解决这个问题?

谢谢!

标签: flutterdart

解决方案


您可以在下面复制粘贴运行完整代码您可以在 代码片段
中初始化并设置helperinitStatehelper.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),
      ),
    );
  }
}

推荐阅读