有状态的Widget更新时使用了不同的参数,没有更新吗?

问题描述 投票:0回答:1

我刚开始学习颤动,我使用状态小工具,下面的代码是main.dart文件

void main() {
  runApp(App());
}

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
        home: new Scaffold(backgroundColor: Colors.blueGrey.shade100,
      body: Home.Homescreen(HomeText: "default",), //initially setting text to default
      appBar: new AppBar(
        centerTitle: true,
        title: new Text("newApp",
            textDirection: TextDirection.ltr,
            style: TextStyle(fontSize:20 ,color: Colors.white)),
      ),
      bottomNavigationBar: new BottomNavigationBar(items: [
        new BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: new Text(
              "Home",
              textDirection: TextDirection.ltr,
            )),
        new BottomNavigationBarItem(
            icon: Icon(Icons.face),
            title: new Text(
              "Profile",
              textDirection: TextDirection.ltr,
            )),
        new BottomNavigationBarItem(
            icon: Icon(Icons.exit_to_app),
            title: new Text(
              "Exit",
              textDirection: TextDirection.ltr,
            )),
      ],onTap: (int item){
        if(item == 0){
          setState(() {
            Home.Homescreen(HomeText:'this is home'); /* this should change homescreen text but it still says default same for all the below as well*/
          });
        }
        else if(item == 1){
          setState(() {
            Home.Homescreen(HomeText:'this is proile');
          });

        }
        else if(item == 2){
          setState(() {
            Home.Homescreen(HomeText:'this is exit');
          });
        }
      },),
    ));
  }
}

在这种情况下,将调用无状态小部件“ App”,并在脚手架的_AppState中将主体分配给在BottomNavigationBar下的main中导出为Home的无状态小部件“ HomeScreen”,为项目分配一个int,在点击时应更改HomeText相应,但不会更新,它在主屏幕上保持不变,只是说“ default”(最初是用它来调用的),以下代码是home_screen.dart的代码,该代码称为

class Homescreen extends StatefulWidget{
  Homescreen({this.HomeText}); // init hometext
  String HomeText;
  @override
  _Homescreen createState() => _Homescreen();

}

class _Homescreen extends State<Homescreen>{

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Center(
      child: new Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          new Text(
            widget.HomeText, // this is what should be updated when called
            textDirection: TextDirection.ltr,
            style: new TextStyle(fontSize: 30,color: Colors.black),
          )
        ],
      ),
    );
  }
}

[我不明白为什么在点击图标(bottomnavigationbaritems)时hometext为什么不更新,我已经使用debugprint测试了它们返回0,1,2的值。所以,那至少是正确的。

android flutter flutter-layout setstate statelesswidget
1个回答
0
投票

尝试下面的代码:

void main() {
  runApp(App());
}

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  String homeText = "default";
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
        home: new Scaffold(backgroundColor: Colors.blueGrey.shade100,
      body: Home.Homescreen(HomeText: homeText,), //initially setting text to default
      appBar: new AppBar(
        centerTitle: true,
        title: new Text("newApp",
            textDirection: TextDirection.ltr,
            style: TextStyle(fontSize:20 ,color: Colors.white)),
      ),
      bottomNavigationBar: new BottomNavigationBar(items: [
        new BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: new Text(
              "Home",
              textDirection: TextDirection.ltr,
            )),
        new BottomNavigationBarItem(
            icon: Icon(Icons.face),
            title: new Text(
              "Profile",
              textDirection: TextDirection.ltr,
            )),
        new BottomNavigationBarItem(
            icon: Icon(Icons.exit_to_app),
            title: new Text(
              "Exit",
              textDirection: TextDirection.ltr,
            )),
      ],onTap: (int item){
        if(item == 0){
          setState(() {
            homeText = "this is home"; 
          });
        }
        else if(item == 1){
          setState(() {
            homeText = "this is profile";
          });

        }
        else if(item == 2){
          setState(() {
            homeText = "this is exit";
          });
        }
      },),
    ));
  }
}

您遇到的问题是调用setState时没有改变身体。运行build方法时,它始终具有相同的主体。使用上面的代码,您将更新homeText值,并且当构建方法运行时,homeText具有一个新值,并且您的文本也会更新。

© www.soinside.com 2019 - 2024. All rights reserved.