How can I hide a Button
based on a value from Firestore?
I know that I can do this in a way with the Visibility
widget, and I have tried this with just a Toggle
button and it works. But if I want to check if the user is admin, and then decide wether the Button
should be visible or not.
I have this loggedin
that I use to get the data from that specific user that is logged in.
And in my database I have a field Admin, with the value true or false.
Here is my code:
class ClientHomePage extends StatefulWidget {
static const String id = "client_home_page";
const ClientHomePage({Key? key}) : super(key: key);
@override
State<ClientHomePage> createState() => _ClientHomePageState();
}
final scaffoldkey = GlobalKey<ScaffoldState>();
class _ClientHomePageState extends State<ClientHomePage> {
final _toolidController = TextEditingController();
User? user =FirebaseAuth.instance.currentUser;
UserModel loggedin = UserModel();
@override
void initState() {
super.initState();
FirebaseFirestore.instance
.collection("users")
.doc(user!.uid)
.get()
.then((value) {
setState(() {
loggedin = UserModel.fromMap(value.data());
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldkey,
resizeToAvoidBottomInset: false,
appBar: AppBar(
backgroundColor: Colors.blue,
automaticallyImplyLeading: false,
title: Text(
'Client',
style: TextStyle(
fontFamily: 'Poppins',
color: Colors.white,
fontSize: 22,
),
),
centerTitle: true,
actions: <Widget>[
IconButton(
tooltip: 'Admin web',
icon: Icon(
Icons.admin_panel_settings,
color: Colors.white,
size: 30,
),
onPressed: (){
if(loggedin.admin == 'true'){
print('user admin');
Navigator.pushReplacementNamed(context, WebHomePage.id);
}else if (loggedin.admin == 'false'){
print ('user not admin');
}
print('admin knapp trykket');
},
),
SizedBox(
width: 5,
),
IconButton(
tooltip: 'Logg ut',
icon: Icon(
Icons.logout,
color: Colors.white,
size: 30,
),
onPressed: (){
FirebaseAuth.instance.signOut();
Navigator.pushReplacementNamed(context, LoginUserPage.id);
//logg ut funksjon
print('logg ut knapp trykket');
},
),
],
),
backgroundColor: Colors.white,
body: SafeArea(
child: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: EdgeInsetsDirectional.fromSTEB(100, 20, 100, 0),
child: Text(
'Du er innlogget som:',
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'Poppins',
color: Color(0xFF9E9E9E),
),
),
),
SizedBox(
height: 10,
),
Center(child: Text('${loggedin.name}')),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(0, 150, 0, 0),
child: Text(
'Scan verktøy',
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
],
),
),
),
);
}
}
CodePudding user response:
Create a state bool like bool showButton = false;
and changed it after getting data.
bool showButton = false;
@override
void initState() {
super.initState();
FirebaseFirestore.instance
.collection("users")
.doc(user!.uid)
.get()
.then((value) {
loggedin = UserModel.fromMap(value.data());
if (loggedin.admin == 'true') {
showButton = true;
}
setState(() {});
});
}
Now use
if(showButton) YourButton(),
CodePudding user response:
You can do this with loggedin.admin
, try this:
loggedin.admin == 'true' ? IconButton(
tooltip: 'Admin web',
icon: Icon(
Icons.admin_panel_settings,
color: Colors.white,
size: 30,
),
onPressed: (){},
): SizedBox()
In this case, IconButton
only show when that condition was true.