简体   繁体   English

文本超出 TextFormField - Flutter

[英]Text goes out of TextFormField - Flutter

Faced such a problem that when I create a task and add 2 or more fields to it, then when transferring this text to another screen, it climbs out of the text field.遇到这样一个问题,当我创建一个任务并向其添加 2 个或更多字段时,然后将此文本传输到另一个屏幕时,它会爬出文本字段。 I can't figure out what to do to fix the error.我不知道该怎么做才能修复错误。 Hide extra text or increase the size of the TextFormField under the text.隐藏多余的文本或增加文本下的 TextFormField 的大小。 Please tell me how to solve this problem?请告诉我如何解决这个问题? Thank you.谢谢你。

edit_todo编辑待办事项

class EditTodoScreen extends StatelessWidget {
  final Todo todo;
  EditTodoScreen({Key? key, required this.todo}) : super(key: key);

  final _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    _controller.text = todo.description;

    return BlocBuilder<TodoBloc, TodoState>(builder: (context, state) {
      return Scaffold(
        appBar: AppBar(
          title: const Text(
            'Edit Todo',
            style: TextStyle(fontSize: 20.0),
          ),
        ),
        body: _body(context),
      );
    });
  }

  Widget _body(context) {
    return Padding(
      padding: const EdgeInsets.all(20.0),
      child: Column(
        children: [
          TextFormField(
            controller: _controller,
            autocorrect: true,
            decoration: const InputDecoration(hintText: 'Enter todo message'),
          ),
          const SizedBox(
            height: 10.0,
          ),
          InkWell(
            onTap: () {
              // BlocProvider.of<TodoBloc>(context)
              //     .add(UpdateTodos(Todo(description: _todoName)));
              final updateTodo = Todo(description: _controller.text);
              BlocProvider.of<TodoBloc>(context).add(
                UpdateTodos(
                  updateTodo,
                  // _controller.text,
                ),
              );
              Navigator.pop(context);
            },
            child: _updateBtn(context),
          )
        ],
      ),
    );
  }

 Widget _updateBtn(context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: 50.0,
      decoration: BoxDecoration(
          color: Colors.black, borderRadius: BorderRadius.circular(10.0)),
      child: Center(
        child: Text(
          'Update Todo',
          style: TextStyle(
              fontSize: 17.0,
              color: Colors.amber.shade700,
              fontWeight: FontWeight.bold),
        ),
      ),
    );
  }

todo_list待办事项列表

onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => EditTodoScreen(
                            todo: state.loadedUser[index],
                          ),
                        ),
                      );
                    },

在此处输入图像描述

在此处输入图像描述

You could define max and min lines for TextFormField .您可以为TextFormField定义最大和最小行。 I've used max lines of two here.我在这里使用了最多两行。 Use whatever you find best for your case.使用你认为最适合你的情况的任何东西。 If there are more lines than the maximum you've defined it will become scrollable.如果行数超过您定义的最大值,它将变为可滚动的。

TextFormField(
    controller: _controller,
    autocorrect: true,
    decoration: const InputDecoration(
        hintText: 'Enter todo message',
    ),
    maxLines: 2,
    minLines: 1,
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM