如果我使用给定的代码,并使用图标按钮在应用程序栏,然后它隐藏一个小部件和显示其他
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"),
),
],
),
);
}
}
1条答案
按热度按时间uplii1fm1#
您需要将
VoidCallBack
函数作为参数传递给DrawerWidget。当调用这个类时,你需要传递一个CallBackFunction:
完整演示代码:Here