LonelyWolf
LonelyWolf

Reputation: 4392

Text field alert dialog for multiple platforms

I'm asking this question because I wasn't able to find one proper answer for implementing text field alert dialog. I basically put this thing together from multiple answers which works nicely but it doesn't switch to iOS style dialog when on iOS device. BTW Using CupertionAlertDialog throws error "no material widget found". I don't think it likes TextFormField in content

EDITED NEW FULLY FUNCTIONAL CODE

Here is my function file text_field_dialog.dart

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

String _typedValue;
TextEditingController _textFieldController = TextEditingController();

Future<String> textFieldDialog({BuildContext context, String initValue}) async {
  _typedValue = initValue;
  _textFieldController.text = initValue;
  Platform.isIOS
      ? await showCupertinoDialog(
          context: context,
          builder: (context) {
            return CupertinoAlertDialog(
              title: Text('TextField in Dialog'),
              content: Container(
                child: CupertinoTextField(
                  autofocus: true,
                  controller: _textFieldController,
                  keyboardType: TextInputType.number,
                  placeholder: "TextField in Dialog",
                  onChanged: (v) => _typedValue = v,
                ),
              ),
              actions: <Widget>[
                CupertinoDialogAction(
                  isDefaultAction: true,
                  child: Text("OK"),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
                CupertinoDialogAction(
                    child: new Text('CANCEL'),
                    onPressed: () {
                      _typedValue = initValue;
                      Navigator.of(context).pop();
                    }),
              ],
            );
          },
        )
      : await showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(
              title: Text('TextField in Dialog'),
              content: TextField(
                controller: _textFieldController,
                autofocus: true,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(hintText: "TextField in Dialog"),
                onChanged: (v) => _typedValue = v,
              ),
              actions: <Widget>[
                FlatButton(
                  child: new Text('OK'),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
                FlatButton(
                  child: new Text('CANCEL'),
                  onPressed: () {
                    _typedValue = initValue;
                    Navigator.of(context).pop();
                  },
                )
              ],
            );
          },
        );
  return _typedValue;
}

and here is button where I call the dialog in main class

FlatButton(
          child: Text('show'),
          onPressed: () async {
            String h = await textFieldDialog(context: context, initValue: '5');
            print(h);
          });

Upvotes: 1

Views: 2813

Answers (1)

Vettiyanakan
Vettiyanakan

Reputation: 8470

you need to call the CupertinoAlertDialog() to show the IOS style dialog.

Upvotes: 1

Related Questions