为什么文本窗口小部件中的变量更改时不更新?

时间:2019-07-31 07:14:29

标签: flutter dart

在我的代码中,我想创建一个简单的计数器,该计数器在单击另一页上的按钮时触发。我在按钮页面上创建计数器,然后将其发送到显示它的主页。

void onTap() {

    Widget displayWidget() {
      int drinks = 0;
      return Container(
        child: InkWell(
          child: Stack(
            alignment: Alignment.center,
            children: <Widget>[
              Container(child: Image(image: AssetImage('Images/HabitBars/pinkBar.png'))),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Text("Target: ${glasses.toString()}"),
                  Container(
                    child: Row(
                      children: <Widget>[
                        IconButton(
                            icon: Icon(Icons.remove),
                            onPressed: () {
                              setState(() {
                                drinks--;
                              });
                            }
                        ),
                        Text(drinks.toString()),
                        IconButton(
                            icon: Icon(Icons.add),
                            onPressed: () {
                              setState(() {
                                drinks++;
                              });
                            }
                        ),
                      ],
                    ),
                  )
                ],
              )
            ],
          ),
        ),
      );
    }

    Navigator.push(context, MaterialPageRoute(builder: (context) => MainPage(displayWidget())));

  }

这是按下按钮时调用的功能。

MainPage的代码如下

import 'package:flutter/material.dart';
import './NewHabitPage.dart';
import 'gloabals.dart' as globals;

class MainPage extends StatefulWidget {
  Widget habit;
  MainPage(this.habit);
  @override
  State createState() => MainPageState(newHabit: habit);
}

class MainPageState extends State<MainPage> {

   Widget newHabit;

  MainPageState({this.newHabit}) {
    if (newHabit != null) {
      globals.habits.add(newHabit);
    }
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text("Your Habits", style: TextStyle(color: Colors.green, fontFamily: 'Raleway', fontSize: 30.0), textAlign: TextAlign.center,),
          leading: IconButton(icon: Icon(Icons.dehaze, color: Colors.green,), onPressed: () => print("YEET")),
          backgroundColor: Colors.white,
          elevation: 0.0,
          centerTitle: true,
        ),
        bottomNavigationBar: GestureDetector(
          onTap: () => Navigator.push(context, MaterialPageRoute(builder: (context) => NewHabitPage())),
          child: Container(
              child: Image(image: AssetImage('Images/newHabit.png')),
          ),
        ),
        body: HabitView(habits: globals.habits,),
      ),
    );
  }
}

class HabitView extends StatefulWidget {
  List<Widget> habits;
  HabitView({@required this.habits});

  @override
  State createState() => HabitViewState(habits: habits);
}

class HabitViewState extends State<HabitView> {

  HabitViewState({@required this.habits});
  List<Widget> habits;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      child: ListView.builder(
          itemBuilder: (context, i) {
            if (habits.length == 0){
              return Center(
                  child: Padding(
                    padding: const EdgeInsets.all(50.0),
                    child: Text("Wow such empty", style: TextStyle(color: Colors.grey, fontSize: 15.0),),
                  ));
            } else {
              return habits[i];
            }
          },
          itemCount: habits.length == 0? 1 : habits.length,
      ),
    );
  }
}

全局变量是我存储全局变量的文件。当前唯一的全局变量是我用于listview.builder的小部件列表,它称为globals.habits。

问题是,当我将小部件从onTap发送到MainPage时,单击图标按钮时,文本(使用Drinks变量)(尽管Drinks变量已更改)仍未更改。

我是陌生的新手,所以不确定某些事情如何工作。

1 个答案:

答案 0 :(得分:0)