app icon indicating copy to clipboard operation
app copied to clipboard

How to create a multiline Flutter textField?

Open SimonLab opened this issue 3 years ago • 6 comments

image

The text field can contains multiple lines, however at the moment the content is centered. I'm going to look at the Flutter layout and find a solution to make sure the text start at the top of the widget. I'm not sure if there is an option on the text field widget or if it's a column or expands widgets related issue

SimonLab avatar Feb 08 '23 10:02 SimonLab

@SimonLab thanks for capturing this quest. 👌

nelsonic avatar Feb 08 '23 11:02 nelsonic

    return MaterialApp(
        title: 'App',
        theme: ThemeData(
          primarySwatch: Colors.teal,
        ),
        home: Scaffold(
          appBar: AppBar(
            title: const Text('DWYL'),
            centerTitle: true,
          ),
          body: const TextField(
            decoration: InputDecoration(
                hintText: 'Capture what is on your mind...',
                border: OutlineInputBorder()),
            maxLines: null,
            expands: true,
            textAlignVertical: TextAlignVertical.top,
          ),
        ));

The TextField widget height is set by defining masLines to null and expands to `true image image

Then we set the textAlignVertical option with TextAlignVertica.top constant image

image

See TextField Widget documentation

SimonLab avatar Feb 08 '23 13:02 SimonLab

Note that now the field is multiline the submit button on the keyboard is replaced by the enter key to create new line. You can configure which type of keyboard you want to display for the text field with the option keyboardType, for example keyboardType: TextInputType.multiline (the default keyboard for multiline).

Other values: image see https://api.flutter.dev/flutter/services/TextInputType-class.html

If you use the text value the submit key is displayed however now you can not create new lines. We'll need to add the save button on the screen to save the item.

SimonLab avatar Feb 08 '23 14:02 SimonLab

I'm going to look at changing dynamically some of the options. Display the textField as a one line text the on focus expands the text field to takes all the screen and hide other items on the page.

We can try this UI, however I think we should try also to have the list of items and input text split into two views.

SimonLab avatar Feb 08 '23 14:02 SimonLab

Using stateful widget for the text field as I want the value for maxLines and expands to be dynamic.

First display the text field on one line: image

When the text field is tapped, change the maxLines and expands values to extends the text field. I've also added a save button: image

Clicking the button change the text file to be on one line again.

The onTap callback is not the best way to expand the text field widget, I'm looking now instead on how to detect focus on a widget

SimonLab avatar Feb 08 '23 17:02 SimonLab

@SimonLab isn't this issue been already completed and merged? https://github.com/dwyl/app/pull/302 👀

LuchoTurtle avatar Feb 23 '23 17:02 LuchoTurtle