Home > Software engineering >  im trying to display text after clicking a button on flutter, its a sign up page
im trying to display text after clicking a button on flutter, its a sign up page

Time:12-17

      Positioned(
        left: 150.0,
        top: 630.0,
        right: null,
        bottom: null,
        width: 100.0,
        height: 50.0,
        child: Align(
  child: RaisedButton(
    child: Text(
      'Confirm',
      style: TextStyle(fontSize: 20.0),
    ),
    color: Color.fromARGB(255, 227, 236, 246),
    textColor: Color.fromARGB(255, 49, 69, 106),
    padding: EdgeInsets.all(5.0),
    onPressed: () {
      for(int i=0; i<usernames.length; i  ){
        if(conEmail.text.isEmpty | conName.text.isEmpty | conUsername.text.isEmpty | conPassword.text.isEmpty | conConfirm.text.isEmpty ){
        print("some fields are empty");
        break;

      }

instead of printing 'some fields are empty' I would like to create a text to pop up on the signup page displaying that same message. screen layout

CodePudding user response:

Based on your code excerpt, I'm guessing the UI is in a stateful widget. You can show an alert popup or show the text on the signup page. The best solution, in my opinion is to group the entries in a Form widget.

The documentation has example code: Form widget API and example

If that doesn't fit your needs and you want to show the error message on the signup page itself, you can include the error message as a text that is optionally visible. This means you have to have a boolean value showing if the entries are complete.

```
bool formIsOk = true;
String errorString = '';
...
Widget build(BuildContext context) {
  RaisedButton(...
      onPressed: () {
  for(int i=0; i<usernames.length; i  ){
    if(conEmail.text.isEmpty | conName.text.isEmpty | conUsername.text.isEmpty | conPassword.text.isEmpty | conConfirm.text.isEmpty ){
    errrorString = 'some fields are empty';
    break;
  }
  if (!errorString.isEmpty) {
    setState(() {
     }
    );
  }
...
//other widgets
...
Visibility(visible: errorString.isEmpty, 
  child: Text('some fields are empty'))

```

Finally, if you just want to pop up an alert, you can use the AlertDialog class using showDialog:

```
onPressed: () => onPressed() {
  for(int i=0; i<usernames.length; i  ){
    if(conEmail.text.isEmpty | conName.text.isEmpty | conUsername.text.isEmpty | conPassword.text.isEmpty | conConfirm.text.isEmpty ){
showDialog<String>(
    context: context,
    builder: (BuildContext context) => AlertDialog(
      title: const Text('ValidationError'),
      content: const Text('Some fields are empty'),
      actions: <Widget>[
        TextButton(
          onPressed: () => Navigator.pop(context, 'OK'),
          child: const Text('OK'),
        ),
      ],
    ),
  }
```

CodePudding user response:

Instead of print("some fields are empty"); call showDialog() with a message.

  • Related