Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter - how to create forms in popup

I want to create a form inside a pop-up with flutter like the image below: popup

popup.

how can I do that with flutter?

like image 912
geek man Avatar asked Feb 01 '19 13:02

geek man


People also ask

How do I embed a form in a popup?

From the form editor, navigate to Share → Embed. Scroll down to the "Popup" section. Copy the embed code from that section and paste it on the desired page in your website.

How do you add dialog boxes in Flutter?

Creating a full-screen dialog cannot be done by the showDialog method. Instead, use the showGeneralDialog method. In the pageBuilder , you should specify your dialog widget implementation. As a first widget, you can specify the SizedBox.


4 Answers

Here you go! showDialog takes a WidgetBuilder as a parameter so you can return any widget.

   import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(home: new MyApp()));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            showDialog(
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    content: Stack(
                      overflow: Overflow.visible,
                      children: <Widget>[
                        Positioned(
                          right: -40.0,
                          top: -40.0,
                          child: InkResponse(
                            onTap: () {
                              Navigator.of(context).pop();
                            },
                            child: CircleAvatar(
                              child: Icon(Icons.close),
                              backgroundColor: Colors.red,
                            ),
                          ),
                        ),
                        Form(
                          key: _formKey,
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              Padding(
                                padding: EdgeInsets.all(8.0),
                                child: TextFormField(),
                              ),
                              Padding(
                                padding: EdgeInsets.all(8.0),
                                child: TextFormField(),
                              ),
                              Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: RaisedButton(
                                  child: Text("Submitß"),
                                  onPressed: () {
                                    if (_formKey.currentState.validate()) {
                                      _formKey.currentState.save();
                                    }
                                  },
                                ),
                              )
                            ],
                          ),
                        ),
                      ],
                    ),
                  );
                });
          },
          child: Text("Open Popup"),
        ),
      ),
    );
  }
}

Hop it helps!

like image 170
Ajay Kumar Avatar answered Oct 18 '22 21:10

Ajay Kumar


Using the rflutter_alert plugin rflutter_alert

You must add the library as a dependency to your project.

 dependencies:    rflutter_alert: ^1.0.3 

To open a popup, Let’s to be a function and do the following:

 _openPopup(context) {     Alert(         context: context,         title: "LOGIN",         content: Column(           children: <Widget>[             TextField(               decoration: InputDecoration(                 icon: Icon(Icons.account_circle),                 labelText: 'Username',               ),             ),             TextField(               obscureText: true,               decoration: InputDecoration(                 icon: Icon(Icons.lock),                 labelText: 'Password',               ),             ),           ],         ),         buttons: [           DialogButton(             onPressed: () => Navigator.pop(context),             child: Text(               "LOGIN",               style: TextStyle(color: Colors.white, fontSize: 20),             ),           )         ]).show();   } 

And call it this way

onPressed: () {   _openPopup(context), }  

enter image description here

like image 36
Paresh Mangukiya Avatar answered Oct 02 '22 03:10

Paresh Mangukiya


Here is an example of code that will allow you to create a button that can produce this kind of popup .

Code :

RaisedButton(
          child: Text("Open Popup"),
          onPressed: () {
            showDialog(
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    scrollable: true,
                    title: Text('Login'),
                    content: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Form(
                        child: Column(
                          children: <Widget>[
                            TextFormField(
                              decoration: InputDecoration(
                                labelText: 'Name',
                                icon: Icon(Icons.account_box),
                              ),
                            ),
                            TextFormField(
                              decoration: InputDecoration(
                                labelText: 'Email',
                                icon: Icon(Icons.email),
                              ),
                            ),
                            TextFormField(
                              decoration: InputDecoration(
                                labelText: 'Message',
                                icon: Icon(Icons.message ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                     actions: [
                      RaisedButton(
                          child: Text("Submit"),
                          onPressed: () {
                            // your code
                          })
                    ],
                  );
                });
          },
        ),

Output :

enter image description here

For more options, you would have to manipulate the properties of the Form widget, the TextField widget or the RaisedButton widget such as autovalidation, decoration, color etc ... If this is not enough , you can use the Dialog widget instead of the AlertDialog widget. But in this case, you will replace the content property with child. And make the necessary modifications.

like image 8
Kabirou Agouda Avatar answered Oct 18 '22 22:10

Kabirou Agouda


Screenshot (without any 3rd party packages):

enter image description here


Code: Just call this method:

void showDialogWithFields() {
  showDialog(
    context: context,
    builder: (_) {
      var emailController = TextEditingController();
      var messageController = TextEditingController();
      return AlertDialog(
        title: Text('Contact Us'),
        content: ListView(
          shrinkWrap: true,
          children: [
            TextFormField(
              controller: emailController,
              decoration: InputDecoration(hintText: 'Email'),
            ),
            TextFormField(
              controller: messageController,
              decoration: InputDecoration(hintText: 'Message'),
            ),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: Text('Cancel'),
          ),
          TextButton(
            onPressed: () {
              // Send them to your email maybe?
              var email = emailController.text;
              var message = messageController.text;
              Navigator.pop(context);
            },
            child: Text('Send'),
          ),
        ],
      );
    },
  );
}
like image 5
CopsOnRoad Avatar answered Oct 18 '22 22:10

CopsOnRoad