【问题标题】:How to pass a boolean value from one class to another and back?如何将布尔值从一个类传递到另一个类并返回?
【发布时间】:2021-09-16 10:12:29
【问题描述】:

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

首页

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 ?

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

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    看看这里发生了什么,当您将isVisible 设置为false 时,您必须在第二页上使用它意味着您必须将isVisible 数据从一页传递到另一页。可以参考here

    first.dart

    class MainScreen extends StatefulWidget {
      bool isVisible = true;
    
      MainScreen({this.isVisible});
    
    }
    
    Navigator.push(context,MaterialPageRoute(builder: (context) => Second(data: isVisible)));
    

    second.dart

    class Second extends StatefulWidget {
      final String data;
    
      MyPosts({this.data});
    }
    

    你可以使用widget.data

    【讨论】:

    • 如何在第二个屏幕上使用widget.isVisible ?这就是我卡住的地方,我按照你在第一个屏幕上的说法更新了我的构造函数。
    【解决方案2】:

    参考标题和函数参数。

    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);
            },
          ),
        );
      }
    }
    

    【讨论】:

    • 嘿,谢谢!我现在了解如何将布尔值发回,但是如何在第一页将 testFunction 初始化为 true?
    • @pannam 更新答案你可以有一个局部变量并在initState中设置值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 2011-11-14
    • 2011-08-26
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多