Multi-line Textfield in flutter

后端 未结 11 1713
夕颜
夕颜 2020-12-13 07:54

It may sound easy but How can we do a multi-line editable textfield in flutter? TextField works only with a single line.

Edit: some precisions beca

相关标签:
11条回答
  • 2020-12-13 08:29

    Although other people already mentioned that the keyboard type "TextInputType.multiline" can be used, I wanted to add my implementation of a TextField that automatically adapts its height when a new line is entered, as it is often desired to immitate the input behaviour of WhatsApp and similar apps.

    I'm analyzing the number of '\n' chatacters in the input for this purpose each time the text is changed. This seems to be an overkill, but unfortunately I didn't find a better possibility to achieve this beahivour in Flutter so far and I didn't notice any performance problems even on older smartphones.

    class _MyScreenState extends State<MyScreen> {
      double _inputHeight = 50;
      final TextEditingController _textEditingController = TextEditingController();
    
      @override
      void initState() {
        super.initState();
        _textEditingController.addListener(_checkInputHeight);
      }
    
      @override
      void dispose() {
        _textEditingController.dispose();
        super.dispose();
      }
    
      void _checkInputHeight() async {
        int count = _textEditingController.text.split('\n').length;
    
        if (count == 0 && _inputHeight == 50.0) {
          return;
        }
        if (count <= 5) {  // use a maximum height of 6 rows 
        // height values can be adapted based on the font size
          var newHeight = count == 0 ? 50.0 : 28.0 + (count * 18.0);
          setState(() {
            _inputHeight = newHeight;
          });
        }
      }
    
    
      // ... build method here
      TextField(
        controller: _textEditingController,
        textInputAction: TextInputAction.newline,
        keyboardType: TextInputType.multiline,
        maxLines: null,
      )
    
    0 讨论(0)
  • 2020-12-13 08:32

    Use expands and you don't need to give minLines or maxLines any specific value:

    TextField(
      maxLines: null,
      expands: true, 
      keyboardType: TextInputType.multiline,
    )
    
    0 讨论(0)
  • 2020-12-13 08:36

    use this

    TextFormField(
          keyboardType: TextInputType.multiline,
          maxLines: //Number_of_lines(int),)
    
    0 讨论(0)
  • 2020-12-13 08:38
       TextFormField(
                      minLines: 2,
                      maxLines: 5,
                      keyboardType: TextInputType.multiline,
                      decoration: InputDecoration(
                        hintText: 'description',
                        hintStyle: TextStyle(
                          color: Colors.grey
                        ),
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.all(Radius.circular(20.0)),
                        ),
                      ),
                    ),
    
    0 讨论(0)
  • 2020-12-13 08:39

    For TextFormField widget, You can set minLines and maxLines if you want to set fix number of lines. Otherwise you can also set maxLines to null.

    TextFormField(
          minLines: 5,
          maxLines: 7,
          decoration: InputDecoration(
              hintText: 'Address',
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.all(Radius.circular(10.0)),
              ),
          ),
    ),
    
    0 讨论(0)
提交回复
热议问题