当前位置: 首页 > 知识库问答 >
问题:

flutter小部件中的可见性没有改变

卫招
2023-03-14

每当我试图更改可见性的状态时,setState都不起作用,尽管在控制台中打印的消息被更正了。这是我的浮动操作按钮缠绕了几个小部件。问题是每当我点击它时,isVisible属性应该被更改为false,它应该是不可见的,但这并没有发生。“Pressed Undo”(按下撤消)打印在控制台中。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:maya/screens/profile.dart';
import 'package:maya/screens/screen.dart';
import 'package:maya/screens/settings.dart';
import 'package:avatar_glow/avatar_glow.dart';
import 'package:tcard/tcard.dart';
import 'package:glass_kit/glass_kit.dart';
import 'package:fluttertoast/fluttertoast.dart';

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  double likeBtnWidth = 75;
  double dislikeBtnWidth = 75;
  TCardController _controller = TCardController();
  int cardNumber;
  bool isVisible;   //here is the declaration



  Widget _layoutDetails() {
    @override
    Orientation orientation = MediaQuery.of(context).orientation;

      setState(() {
        likeBtnWidth = 150;
        dislikeBtnWidth = 150;
      });

      List<String> images = [
        'assets/images/girls/girl1.png',
        'assets/images/girls/girl2.png',
        'assets/images/girls/girl3.png',
        'assets/images/girls/girl4.png',
        'assets/images/girls/girl5.png',
        'assets/images/girls/girl6.png',
      ];

      List<String> personalDetails = [
        'Prakriti Regmi%20%Lets read Murakami together.%Chabahil (1 mi away)',
        'Dristi Sigdel%19%Belle áme%Sundhara (2 mi away)',
        'Prajita Upreti%19%Here to make friends.%Makalbari (2.3 mi away)',
        'Sugandhi C.%21%Not looking for any drama.%Bhaktapur (7 mi away)',
        "Pawana Shrestha%20%Why am I here for?%Attarkhel",
        "Tanuja Shrestha%21%Hi there! ♐︎%Dakshin Dhoka"
      ];

      List<Widget> cards = List.generate(
        images.length < personalDetails.length
            ? images.length
            : personalDetails.length,
        (int index) {
          return GlassContainer.frostedGlass(
            height: 800,
            width: 800,

            child: Padding(
                padding: const EdgeInsets.fromLTRB(30, 20, 30, 0),
                child: Container(

                  child: Stack(
                    children: [
                      Column(
                        children: [
                          SizedBox(
                            height: 20,
                          ),
                          Center(child: _picture(images[index])),
                          SizedBox(
                            height: 30,
                          ),
                          _bio(personalDetails[index].split("%")),
                          SizedBox(
                            height: 10,
                          ),
                          _likeUnlikeButtons()
                        ],
                      ),
                      Positioned(
                        left: 0,
                        bottom: 15,
                        child: SizedBox(
                          width: 30,
                          child: Visibility(
                            visible: isVisible,
                            child: FloatingActionButton(
                                    onPressed: () {
                                      setState(() {
                                        isVisible = false;                           //this is the problamatic part.
                                        print("pressed undo");
                                      });
                                    },
                                    child: Text("helllllo"),
                                  )
                              
                            replacement: Container(),
                          ),
                        ),
                      ),
                    ], 
                  ),
                )),
          );
          // );
        },
      );

      return SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: TCard(
          controller: _controller,
          onForward: (index, info) {
            if (info.direction == SwipDirection.Right) {
              Fluttertoast.showToast(
                  msg: "Liked",
                  toastLength: Toast.LENGTH_SHORT,
                  gravity: ToastGravity.CENTER,
                  timeInSecForIosWeb: 1,
                  backgroundColor: Colors.yellow,
                  textColor: Colors.black,
                  fontSize: 16.0);
            }

            setState(() {
              isFirstCard = false;
            });
          },
          onBack: (index, info) {
            // _controller.forward(SwipDirection);
            // print(index);
          },
          onEnd: () {
            _controller.reset();
          },
          size: Size(double.infinity, double.infinity),
          cards: cards,
        ),
      );

      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return _layoutDetails();
  }
}

   ),
    );
  }
}

共有1个答案

郦昆
2023-03-14

isVisibile需要在_MainPageState的build方法中声明,以便更新小部件。

 类似资料:
  • 假设您正在编写一个Android项目(不是库)。所有文件都编译在一起,因此......在这种情况下使用可见性修饰符有任何意义吗?

  • Flutter大量使用widget这个词,这使得很难找到关于这个主题的文档。在Android上(我相信这在iOS上是不可能的),我们可以在我们的家庭仪表板上添加小部件,允许我们看到应用程序相关的信息或触发一键操作,而不需要打开有问题的应用程序。 有可能在Dart&Flutter中构建这样的“小部件”吗?或者我应该在java中这样做,并以某种方式将其插入我的flutter应用程序? 你能分享一个包含

  • 我是Netbeans和Java的新手,对jpanels上的jlabel有一个问题。我有一个jTabbedPane,里面有一个jPanel。我在JPanel上有一个jLabel。我想将jLabel的可见性设置为false,但似乎不起作用。当我运行程序时,标签仍然可见。我不明白为什么。

  • 问题内容: 这样的事情会使小部件正常显示: 虽然这样,将使小部件完全不显示: 问题答案: 您可能对 小部件的和方法感兴趣。在以下示例中,单击后按钮消失

  • 有趣的是,我尝试了一个switch case语句,它给了我同样的警告,因此我无法运行代码。是我做错了什么,还是让人无法使用if/else或switch语句而不认为有死代码?