android 我想隐藏一个小部件,并显示其他小部件,这是可能的,在主页上,我打电话给他们,但不可能在抽屉里扑

xriantvc  于 2023-05-15  发布在  Android
关注(0)|答案(1)|浏览(124)

如果我使用给定的代码,并使用图标按钮在应用程序栏,然后它隐藏一个小部件和显示其他

return Scaffold(
      appBar: AppBar(
          backgroundColor: Colors.red,
          centerTitle: true,
          title: const Text("Home Page"),
          actions: [
            IconButton(
                onPressed: () {
                  setState(() {
                    isvisible = !isvisible;
                    isvisibles = !isvisibles;
                  });
                },
                icon: const Icon(Icons.hide_source))
          ]),
      drawer: const DrawerWidget(),
      body: Stack(
        children: [
          Visibility(
            visible: isvisible,
            child: const MapInitialization(),
          ),
          Visibility(
            visible: isvisibles,
            child: const DonorMap(),
          ),
        ],
      ),
    );

但我使用抽屉中的一个按钮,然后它不工作,但它显示在控制台,它正在工作,它改变状态,但屏幕不会改变。是有办法在抽屉里实现的。我正在为两个用户构建一个应用程序,用户可以选择在用户之间切换**(像司机和乘客的应用程序一样,用户可以选择成为乘客或司机)**。这是我的抽屉小部件代码

class DrawerWidget extends StatefulWidget {
  const DrawerWidget({super.key});

  @override
  State<DrawerWidget> createState() => _DrawerWidgetState();
}

class _DrawerWidgetState extends State<DrawerWidget> {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: [
          UserAccountsDrawerHeader(
            decoration: const BoxDecoration(color: Colors.red),
            accountName: Text(
              "${gfname.toString()} ${glname.toString()}",
              style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            ),
            accountEmail: Text(
              gnumber.toString(),
              style: const TextStyle(fontSize: 18),
            ),
            currentAccountPicture: CircleAvatar(
              child: ClipOval(
                child: Image.asset(
                  'assets/user.png',
                  width: 80,
                  height: 80,
                  fit: BoxFit.cover,
                ),
              ),
            ),
          ),
          ListTile(
            leading: const Icon(
              Icons.home,
              size: 25,
            ),
            title: const Text(
              "Home",
              style: TextStyle(
                fontSize: 18,
              ),
            ),
            onTap: () {
              Navigator.pushReplacement(
                context,
                MaterialPageRoute(
                  builder: (context) => const HomePage(),
                ),
              );
            },
          ),
          const UpdateButton(),
          const ThemeServices(),
          const LogoutButton(),
          // i am using a button to hide but its not working
          ElevatedButton(
            onPressed: () {
              setState(() {
                isvisible = !isvisible;
                isvisibles = !isvisibles;
              });
            },
            child: const Text("Hide"),
          ),
        ],
      ),
    );
  }
}
uplii1fm

uplii1fm1#

您需要将VoidCallBack函数作为参数传递给DrawerWidget。

class DrawerWidget extends StatelessWidget {

  final VoidCallback onPressed; //this VoidCallBack

  const DrawerWidget({Key? key,
    required this.onPressed,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: Center(
        child: MaterialButton(
          color: Colors.teal,
          onPressed: onPressed, //is used here
          child: const Text("Hide/Show"),
        ),
      ),
    );
  }
}

当调用这个类时,你需要传递一个CallBackFunction:

return Scaffold(
      drawer: DrawerWidget(
        onPressed: (){
          setState(() {
            isVisible = !isVisible; //no need to use two booleans
          });
        },
      ),
      appBar: AppBar(),
      body: Wrap(
        children: [
          Center(
            child: Visibility(
              visible: isVisible,
              child: Container(...),
            ),
          ),
          Center(
            child: Visibility(
              visible: !isVisible,
              child: Container(...),
            ),
          ),
        ],
      )
    );

完整演示代码:Here

相关问题