简体   繁体   English

使用 Flutter GetX 不切换可见性小部件内项目的显示

[英]Using Flutter GetX the display of the item inside the visibility widget is not toggled

I am new to using GetX in Flutter and I am trying to toggle the display of the contents by using unfold bool, a floating action button and a visibility widget.我是在 Flutter 中使用 GetX 的新手,我正在尝试使用展开布尔、浮动操作按钮和可见性小部件来切换内容的显示。 Unfortunately, it is not working.不幸的是,它不起作用。 The unfolded value changes but the contents are not hidden.展开的值会发生变化,但内容不会被隐藏。 Here is the code:这是代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';

import 'note_controller.dart';

class HomeScreen extends StatelessWidget {
  static Route route() => MaterialPageRoute(builder: (_) => HomeScreen());
  var noteController = Get.put(NoteController());
  RxBool unfold = false.obs;
  HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('My Notes'),
        actions: [
          CircleAvatar(
            backgroundColor: Colors.blue.shade200,
            child: Obx(
              () => Text(
                noteController.notes.value.length.toString(),
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 22.0),
              ),
            ),
          ),
          const SizedBox(
            width: 10,
          ),
        ],
      ),
      body: Obx(
        () => ListView.separated(
          itemCount: noteController.notes.value.length,
          separatorBuilder: (context, index) => const Divider(
            color: Colors.blueGrey,
          ),
          itemBuilder: (context, index) => ListTile(
            trailing: SizedBox(
              width: 110.0,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  IconButton(
                    icon: const Icon(Icons.edit, color: Colors.blue),
                    onPressed: () {},
                  ),
                  IconButton(
                    icon: const Icon(
                      Icons.delete,
                      color: Colors.blue,
                    ),
                    onPressed: () {},
                  ),
                ],
              ),
            ),
            title: Text(noteController.notes.value[index].title!),
            subtitle: Visibility(
              child: Text(noteController.notes.value[index].content!),
              visible: !unfold.value,
            ),
            onTap: () {},
            onLongPress: () {},
          ),
        ),
      ),
      floatingActionButton: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Obx(
            () => FloatingActionButton(
                heroTag: "btn1",
                child:
                    unfold.value ? Icon(Icons.menu) : Icon(Icons.unfold_less),
                tooltip: unfold.value
                    ? 'show more. reveals notes content'
                    : 'Show less. Hide notes content',
                onPressed: () {
                  unfold.value = !unfold.value;
                }),
          ),
          /* Notes: for the "Show More" icon use: Icons.menu */

          FloatingActionButton(
            heroTag: "btn2",
            child: const Icon(Icons.add),
            tooltip: 'Add a new note',
            onPressed: () {},
          ),
        ],
      ),
    );
  }
}

Wrap the Visibility with an Obx as well.也用Obx包裹Visibility It's going to be like the following:它将如下所示:

subtitle: Obx(
  () => Visibility(
    child: Text(noteController.notes.value[index].content),
    visible: !unfold.value,
  ),
),

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

相关问题 在 Flutter 中使用 Visibility Widget 来切换小部件的显示 - Using Visibility Widget in Flutter to toggle widgets display 在 Flutter 中切换可见性小部件时,如何避免移动到小部件下方? - How to avoid moving below widgets when Visibility widget is toggled in Flutter? 在 getx 对话框中使用 getx obx 和 GetX 状态管理 - Using getx obx and GetX state management inside getx dialog flutter 使用 Flutter GetX 和 API,如何将 API 数据显示到 Text 小部件中? - Using Flutter GetX and an API, how do I display the API data into a Text widget? Flutter 带有 Getx 的 DropdownButton 小部件 - Flutter DropdownButton widget with Getx 在 flutter 中将 getx 与有状态小部件一起使用会导致任何性能问题吗? - Does using the getx with a stateful widget in flutter cause any performance issues? 使用 Getx 小部件-Flutter 运行时显示错误 - showing Error while running using Getx widget-Flutter 使用 Flutter Visibility 小部件时,可见性的变化是否可以动画化? - When using Flutter Visibility widget, can the change in visibility be animated? flutter 小部件中的可见性没有改变 - Visibility is not changing in a flutter widget 如何使用 flutter getx 从 TextEditingController 获取和显示值 - How to get and display value from a TextEditingController using flutter getx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM