如何将布尔值从一个类传递到另一个类并返回? [英] How to pass a boolean value from one class to another and back?

查看:32
本文介绍了如何将布尔值从一个类传递到另一个类并返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Visibility 小部件来显示和隐藏我的页面.这是我的逻辑,当在第一页 booean isVisible 为真时显示 Container 小部件,但当我转到另一个屏幕时,我设置了布尔值 isVisiblevis 设置为 false 以便我的容器隐藏并保持其状态.当我从第二个屏幕回来时,我想将布尔值设置回 true 从而显示我的容器.

I am trying to use the Visibility widget to show and hide my page. Here is my logic when at the first page the booean isVisible is true showing the Container widget but as I go to another screen I set the boolean isVisiblevis to false such that my container hides and maintains it state. When I come back from the second screen I want to set the boolean back to true hence showing my container.

首页

class MainScreen extends StatefulWidget {
 bool isVisible = true;

  MainScreen({this.isVisible});

 ...
 @override
  Widget build(BuildContext context) {
  body: Container(
            //change the margin
            margin: EdgeInsets.fromLTRB(0, 0, 0, 300),
            child: isVisible ?
            Visibility(
                maintainAnimation: true,
                maintainState: true,
                child: (Container(
                        Text ('first page')
                 ): Container ()

                 .....
                 GestureDetector(
                      onTap: () {
                        isVisible= false; //set the visibility false
                        Navigator.push(
                            //send to search screen
                            context,
                            MaterialPageRoute(
                                builder: (context) => (SecondScreen())));
                       
                      },

现在在第二页上,当我弹出时如何将布尔值 isVisible 在第一页上设置回 true ?

Now on the second page when I pop how do I set the boolean isVisible back to true on first page ?

  GestureDetector(
        onTap: () {
            Navigator.pop(
                //send back data
                context,
                dropOffTextEditingController.text,
            );
            MainScreen(mapVisible: true,); //doesn't work
        },

推荐答案

参考标题和函数参数.

screenone.dart

class ScreenOne extends StatefulWidget {
  ScreenOne({Key key = const Key("ScreenOne")}) : super(key: key);

  @override
  _ScreenOneState createState() => _ScreenOneState();
}

class _ScreenOneState extends State<ScreenOne> {
  bool checkScreenOneValue = true;
  
 @override
  void initState() {
    checkScreenOneValue = true;
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Screen One',
        ),
      ),
      body: Container(
        color: Colors.white,
        padding: EdgeInsets.all(15),
        child: InkWell(
          onTap: () {
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => ScreenTwo(
                        testFunction: testFunction, title: "Screen two")));
          },
          child: Center(
            child: Text(
              "Screen Two",
            ),
          ),
        ),
      ),
    );
  }

  testFunction(bool checkValue) {
    checkScreenOneValue = checkValue;
    print("****TestFunction $checkScreenOneValue");
  }
}

screentwo.dart

class ScreenTwo extends StatefulWidget {
  final Function testFunction;
  final String title;

  const ScreenTwo({required this.testFunction, required this.title});
  @override
  _ScreenTwoState createState() => _ScreenTwoState();
}

class _ScreenTwoState extends State<ScreenTwo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.title,
        ),
      ),
      body: InkWell(
        child: Center(child: Text("Back")),
        onTap: () {
          Navigator.pop(context);
          widget.testFunction(false);
        },
      ),
    );
  }
}

这篇关于如何将布尔值从一个类传递到另一个类并返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆