vendredi 28 mai 2021

Flutter: Running Navigation only if one thing is selected

Im trying to only allow the navigation to happen if _gender != ''. Meaning the user needs to select at least a gender. Currently, _gender is error underlined here.

How should the ternary operator run here as I know the error is probably caused by the genderwidget being in another dart file?

Lmk if there's any better way to run this? Open to any answers thanks!

etc

    return MaterialApp(
        home:
            Gender()); // This trailing comma makes auto-formatting nicer for build methods
  }
}

class Gender extends StatelessWidget {

etc

    return Scaffold(
      floatingActionButton: FloatingActionButton(
        backgroundColor: Color(0xff3a327f),
        elevation: 9,
        child: Icon(Icons.chevron_right, size: 27, color: Colors.white),
        onPressed: () {
          if (_gender != '')
          Navigator.push(context, _createRoute());
        },
     
      ),
      body: SafeArea(
        child: Column(children: <Widget>[
         
        Container(
              
                child: Text(
                  'What\'s your gender?',
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30),

              ),
            ),
 
        
          SizedBox(
            height: 60.0,
          ),
          child: genderWidget(),
        ]),
      ),
    );
  }
}

On another dart file

class genderWidget extends StatefulWidget {


etc

  String _gender = '';

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(9.0),
      child: Column(
        children: <Widget>[
          Card(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(15.0)),
            child: RadioListTile(
              title: CardTextStyle(gender: 'Female'),
              value: 'isFemale',
              activeColor: Colors.green,
              groupValue: _gender,
              onChanged: (value) {
                setState(() {
                  _gender = value;
                  print(_gender);
                });
              },
              controlAffinity: ListTileControlAffinity.trailing,
            ),
          ),
          Card(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(15.0),
            ),
            child: RadioListTile(
              title: CardTextStyle(gender: 'Male'),
              value: 'isMale',
              activeColor: Colors.green,
              groupValue: _gender,
              onChanged: (value) {
                setState(() {
                  _gender = value;
                  print(_gender);
                });
              },
              controlAffinity: ListTileControlAffinity.trailing,
            ),
          ),
          Card(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(15.0)),
            child: RadioListTile(
              title: CardTextStyle(gender: 'Non-Binary'),
              value: 'isNonBinary',
              activeColor: Colors.green,
              groupValue: _gender,
              onChanged: (value) {
                setState(() {
                  _gender = value;
                  print(_gender);
                });
              },
              controlAffinity: ListTileControlAffinity.trailing,
            ),
          )
        ],
      ),
    );
  }
}

class CardTextStyle extends StatelessWidget {
  CardTextStyle({this.gender});

  String gender;

  @override
  Widget build(BuildContext context) {
    return Text(
      gender,
      style: TextStyle(
          color: Colors.black, fontWeight: FontWeight.w600, fontSize: 15),
    );
  }
}



Aucun commentaire:

Enregistrer un commentaire