DolDurma
DolDurma

Reputation: 17289

Flutter using if statement for change widget color

in my application user can be register and my application can save user information with sqlite, each registered user can be verify his mobile number with sms and user can active his account on application by userActive item on database

now, i have some list of items witch that created by class:

  final List<DashboardItems> dashboardItems = [
    DashboardItems(menuItem: MenuItems.dashboard, title: Strings.drawerItemHome, icon: 'assets/icons/home.png', forActiveUser: false),
    DashboardItems(menuItem: MenuItems.healthProfile, title: Strings.drawerItemProfile, icon: 'assets/icons/cannabis.png', forActiveUser: true),
  ];

in application and there are some items for registered user and who active account by sms, in DashboardItems class this field is forActiveUser

for example suppose i have simple Container, who can i colorize white that for active users?

with below code i should be have one white container as MenuItems.dashboard and one gray container as MenuItems.healthProfile.

but i have two white container

decoration: BoxDecoration(
    color:
          user != null && user.userActive == 1 ? Colors.white :
          user == null || (user != null && user.userActive == 0) || !item.forActiveUser ? Colors.white :
    Colors.grey,
        borderRadius: BorderRadius.all(Radius.circular(5.0)), 
    boxShadow: [
      BoxShadow(
        blurRadius: 0.5,
        color: Colors.black,
        offset: Offset(0.0, 0.0),
      )
    ]),

UPDATED:

container should be white when:

`user!=null` and `user.userActive`==1   <-- all containers should be white

otherwise:

`user` is `null` && `user!=null and user.userActive==0` <-- containers should be gray when `forActiveUser` is true

Upvotes: 0

Views: 3456

Answers (1)

Thepeanut
Thepeanut

Reputation: 3397

If I understand you right - the condition should be like this:


decoration: BoxDecoration(
    color: (user != null
           && user.userActive == 1) || !item.forActiveUsers ? Colors.white : Colors.grey,
    borderRadius: BorderRadius.all(Radius.circular(5.0)), 
    boxShadow: [
      BoxShadow(
        blurRadius: 0.5,
        color: Colors.black,
        offset: Offset(0.0, 0.0),
      )
    ]),

UPDATED

Upvotes: 1

Related Questions