程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何从Flutter中的其他StatefulWidget设置/更新StatefulWidget的状态?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何从Flutter中的其他StatefulWidget设置/更新StatefulWidget的状态??

开发过程中遇到如何从Flutter中的其他StatefulWidget设置/更新StatefulWidget的状态?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何从Flutter中的其他StatefulWidget设置/更新StatefulWidget的状态?的解决方法建议,希望对你解决如何从Flutter中的其他StatefulWidget设置/更新StatefulWidget的状态?有所启发或帮助;

:创建_MyHomePageState的全局实例。在_SubState中将此实例用作_myHomePageState.setState

:无需创建全局实例。而是将父实例传递给子窗口小部件

import 'package:Flutter/material.dart';

voID main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @overrIDe
  Widget build(BuildContext context) {
    return new MaterialApp(
      Title: 'Flutter Demo',
      theme: new themeData(
        primarySwatch: colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}

EdgeInsets globalmargin =
    const EdgeInsets.symmetric(horizontal: 20.0, vertical: 20.0);
TextStyle textStyle = const TextStyle(
  FontSize: 100.0,
  color: colors.black,
);

class MyHomePage extends StatefulWidget {
  @overrIDe
  _MyHomePageState createState() => _MyHomePageState();
}

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

  @overrIDe
  Widget build(BuildContext context) {
    return new Scaffold(
      appbar: new Appbar(
        Title: new Text('SO Help'),
      ),
      body: new Column(
        children: <Widget>[
          new Text(
            number.toString(),
            style: textStyle,
          ),
          new GrIDVIEw.count(
            crossAxisCount: 2,
            shrinkWrap: true,
            scrollDirection: Axis.vertical,
            children: <Widget>[
              new InkResponse(
                child: new Container(
                    margin: globalmargin,
                    color: colors.green,
                    child: new Center(
                      child: new Text(
                        "+",
                        style: textStyle,
                      ),
                    )),
                onTap: () {
                  setState(() {
                    number = number + 1;
                  });
                },
              ),
              new Sub(this),
            ],
          ),
        ],
      ),
      floatingActionbutton: new floatingActionbutton(
        onpressed: () {
          setState(() {});
        },
        child: new Icon(Icons.update),
      ),
    );
  }
}

class Sub extends StatelessWidget {

  _MyHomePageState parent;

  Sub(this.parent);

  @overrIDe
  Widget build(BuildContext context) {
    return new InkResponse(
      child: new Container(
          margin: globalmargin,
          color: colors.red,
          child: new Center(
            child: new Text(
              "-",
              style: textStyle,
            ),
          )),
      onTap: () {
        this.parent.setState(() {
          this.parent.number --;
        });
      },
    );
  }
}

请让我知道它是否有效。

解决方法

  1. 例如,下面的代码中的加号按钮可以工作并且可以更新文本,但是减号按钮不能。
  2. 但是,如果我们按FloatingActionButton,则状态将刷新。
  3. 减号按钮正在更改变量的值,但未更新父窗口小部件的状态。

这是代码…

import 'package:flutter/material.dart';

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

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

int number;

EdgeInsets globalMargin = const EdgeInsets.symmetric(horizontal: 20.0,vertical: 20.0);
TextStyle textStyle = const TextStyle(
  fontSize: 100.0,color: Colors.black,);

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    number = number ?? 0;
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),body: new Column(
        children: <Widget>[
          new Text(
            number.toString(),style: textStyle,new GridView.count(
            crossAxisCount: 2,shrinkWrap: true,scrollDirection: Axis.vertical,children: <Widget>[
              new InkResponse(
                child: new Container(
                    margin: globalMargin,color: Colors.green,child: new Center(
                      child: new Text(
                        "+",)),onTap: () {
                  setState(() {
                    number = number + 1;
                  });
                },new Sub(),],floatingActionButton: new FloatingActionButton(
        onPressed: () {
          setState(() {});
        },child: new Icon(Icons.update),);
  }
}

class Sub extends StatefulWidget {
  @override
  _SubState createState() => new _SubState();
}

class _SubState extends State<Sub> {
  @override
  Widget build(BuildContext context) {
    return new InkResponse(
      child: new Container(
          margin: globalMargin,color: Colors.red,child: new Center(
            child: new Text(
              "-",onTap: () {
        setState(() {
          number = number - 1;
        });
      },);
  }
}

大佬总结

以上是大佬教程为你收集整理的如何从Flutter中的其他StatefulWidget设置/更新StatefulWidget的状态?全部内容,希望文章能够帮你解决如何从Flutter中的其他StatefulWidget设置/更新StatefulWidget的状态?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: