简体   繁体   English

可见性不会在颤动中切换?

[英]Visibility won't toggle in flutter?

I've got a gridView where each grid has a FlatButton inside it.我有一个 gridView,其中每个网格里面都有一个 FlatButton。 The button is supposed to trigger the visibility for another button I have outside the GridView.该按钮应该触发我在 GridView 之外的另一个按钮的可见性。 I've set the state in onPressed to change the bool showCard for everytime the GridView button is pressed.我已经在 onPressed 中设置了状态,以在每次按下 GridView 按钮时更改 bool showCard。 In my print statement, it's saying that it's working, producing true and false each time the button is pressed, but it's not changing the visibility of the other button called 'CheckoutCard()'.在我的打印语句中,它说它正在工作,每次按下按钮时都会产生 true 和 false,但它不会改变另一个名为“CheckoutCard()”的按钮的可见性。 Can anyone help me?谁能帮我?

import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:bee/Cards/Items%20Card.dart';
import 'package:bee/Constants/Constants.dart';
import 'package:bee/MerchantCategories/My Categories.dart';
import 'package:bee/MenuButtons/Color Changer.dart';
import 'package:bee/Cards/Checkout Card.dart';
import 'package:bee/main.dart';
import 'Basket Menu.dart';

class MyMenu extends StatefulWidget {

  @override
  _MyMenuState createState() => _MyMenuState();

}

class _MyMenuState extends State<MyMenu> {

//  bool showCard = _MyButtonState().showCard;

  @override
  Widget build(BuildContext context) {


    return Scaffold(

      appBar: AppBar(

        brightness: Brightness.light,

        leading: new IconButton(
          icon: new Icon(Icons.arrow_back, color: Colors.black),
          onPressed: () { Navigator.of(context).pop();},
        ),

        backgroundColor: Colors.white,
        title: Padding(
          padding: const EdgeInsets.all(6.0),
          child: Image(image: AssetImage('images/Merchants/My_Image.png'),),
        ),
        elevation: 1.0,
        centerTitle: true,

        actions: <Widget>[
          Padding(
            padding: const EdgeInsets.only(right: 15.0),
            child: IconButton(icon: Icon(Icons.shopping_basket, color: Colors.blue[800],),
            onPressed: (){ Navigator.push(context, MaterialPageRoute(builder: (context){return BasketMenu();})); }
            ),
          ),

        ],
      ),





      body: Stack(
          children: <Widget>[
            Flex(
              direction: Axis.vertical,
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: Padding(
                    padding: const EdgeInsets.all(5.0),
                    child: Container(
                      child: ListView(children: <Widget>[
                        MyCategories(categoryText: Text('Restaurants', style: categoryTextStyle),),
                        MyCategories(categoryText: Text('Bars', style: categoryTextStyle),),
                        MyCategories(categoryText: Text('Games', style: categoryTextStyle),),

                      ],
                        scrollDirection: Axis.horizontal,
                      ),
                    ),
                  ),
                ),

              Expanded(
                  flex: 10,
                  child: Container(

                    child: GridView.count(

                      crossAxisCount: 2,

                      children: <Widget>[


                        ItemsCard(
                          categoryName: Text('Fast Food', style: secondCategoryTextStyle,),
                          itemText: FastFoodText,
                          priceText: Text('£21.67', style: priceTextStyle,),
                          gridOutline: MyButton(
                            tile: GridTile(
                              child: FastFoodImage,
                          ),
                          ),
                        ),


                        ItemsCard(
                          itemText: SnubbText,
                          priceText: Text('£44.95', style: priceTextStyle,),
                          gridOutline: MyButton(
                            tile: GridTile(
                                child: SnubbImage,
                              ),
                          ),
                        ),


                        ItemsCard(
                          itemText: FreshText,
                          priceText: Text('£41.23', style: priceTextStyle,),
                          gridOutline: MyButton(
                            tile: GridTile(
                                child: FreshImage,
                              ),
                          ),
                        ),


                        Container(),


                      ],
                    ),
                  ),
                ),
              ],
            ),

            Visibility(visible: _MyButtonState().showCard ? _MyButtonState().showCard : !_MyButtonState().showCard, child: CheckoutCard()),

          ],
      ),

    );

  }
}


class MyButton extends StatefulWidget {

  @override
  State<StatefulWidget> createState(){
    return _MyButtonState();
  }

  MyButton({this.tile});
  final GridTile tile;

  bool isVisible = false;
  int itemNumber = 0;

  bool showCheckoutCard(){
    return isVisible = !isVisible;
  }

  int itemCounter(){
    return itemNumber++;
  }

}


class _MyButtonState extends State<MyButton> {

  bool changeColor = false;

  static var myNewButtonClass = MyButton();

  bool showCard = myNewButtonClass.isVisible;

  @override
  Widget build(BuildContext context) {
    return FlatButton(
      shape: Border(bottom: BorderSide(color: changeColor ? Colors.blue[800] : Colors.transparent, width: 3.0)),
      child: widget.tile,
      onPressed: (){
        setState(() {
          changeColor = !changeColor;
          myNewButtonClass.itemCounter();
          print(myNewButtonClass.itemCounter());
          setState(() {
            showCard = !showCard;
            print(showCard);
          });
        });
      },
    );
  }
}

You are calling the setState method inside your Button .您正在Button中调用setState方法。 I don't think it will change the state of your MyMenu widget.我认为它不会改变您的MyMenu小部件的状态。 I would suggest you to change your Button as following:我建议你改变你的Button如下:

class MyButton extends StatelessWidget {
  final Color color;
  final GridTile tile;
  final VoidCallback onPressed;

  const MyButton({Key key, this.color, this.tile, this.onPressed})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FlatButton(
      shape: Border(bottom: BorderSide(color: color)),
      child: tile,
      onPressed: onPressed,
    );
  }
}

After that, you need to declare two variable in your MyMenu widget as follows:之后,您需要在MyMenu小部件中声明两个变量,如下所示:

class _MyMenuState extends State<MyMenu> {
  bool changeColor = false;
  bool showCard = false;

  @override
  Widget build(BuildContext context) {
    yourBuildMethod()

In your MyMenu widget you can call button like this:在您的MyMenu小部件中,您可以像这样调用按钮:

MyButton(
  tile: GridTile(child: SnubbImage),
  color: changeColor
    ? Colors.blue[800]
    : Colors.transparent,
  onPressed: () {
    setState(() {
      changeColor = !changeColor;
      showCard = !showCard;
    });
  },
),

And now check your Visibility like this:现在像这样检查你的Visibility

Visibility(
  visible: showCard,
  child: CheckoutCard(),
)

Now your variables are in your MyMenu widget and you are calling setState function in MyMenu widget.现在你的变量在您MyMenu小部件,你调用setState函数MyMenu部件。 So it will be able to update the state of your widget.因此它将能够更新您的小部件的状态。 I hope this will be helpful for you.我希望这对你有帮助。

To trigger a rebuild of your view based when changing the value of a variable you need to use setState .要在更改变量值时触发视图的重建,您需要使用setState Where you are are changing the value of the isVisible variable, you need to surround it with a setState :在您更改isVisible变量的值的地方,您需要用setState将其包围:

setState(() {
  isVisible = !isVisible;
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 应用程序在 flutter SDK 升级后无法运行 - Flutter app won't run after flutter SDK upgrade 使用单选按钮切换相对布局可见性 - Toggle Relative Layout visibility with Radio Buttons Flutter Navigator.pop 不会发回数据 - Flutter Navigator.pop won't send data back 如何在同一活动中切换不同 TextView 的可见性 - How to toggle the visibility of different TextView's in the same activity Android Studio 不会自动导入包和 @override 方法用于 Flutter 应用程序开发 - Android Studio won't import packages and @override method automatically for flutter app development 为什么我的 Flutter Inspector 面板不能像其他工具一样停靠在 IDE 的右侧? - Why won't my Flutter Inspector panel dock to the right side of the IDE like the other tools? Flutter 在 android studio 3.5 之后,应用程序不会构建 appbundle/APK 或在 Android 上运行 - Flutter App won't build appbundle/APK or run on Android after android studio 3.5 Flutter 应用程序无法运行构建。 安装新更新后出现异常失败 - Flutter app won`t run Build. It failed with an exception after installing new updates 为什么此切换按钮在 flutter - dart 中不起作用? - Why this toggle button not working in flutter - dart? “pubspec.yaml”文件在哪里? 如果不添加路径,Pub.dev 示例将无法在带有 Flutter 插件的 Android Studio 中编译,但它在哪里? - Where is the 'pubspec.yaml' file? Pub.dev examples won't compile in Android Studio with Flutter Plugin without adding a path, but where is it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM