文本字段上的 Flutter 滚动条

Flutter scrollbar on textfield

我有全屏 TextField,当用户添加更多 lines/text 时,屏幕会变大。我想为用户添加滚动条以了解 TextField 的大小以及他在哪里。

这是我的屏幕和 TextField 的代码:

import 'package:flutter/material.dart';

class TextEditor extends StatefulWidget {
  TextEditor();
  @override
  _TextEditorState createState() => _TextEditorState();
}

class _TextEditorState extends State<TextEditor> {
  TextEditingController _editTextController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    var scaffold = Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () => _backButton(),
        ),
        title: Text("File name"),
      ),
      body: _buildTextEditor(),
      resizeToAvoidBottomInset: true,
    );
    return scaffold;
  }

  Widget _buildTextEditor() {
    return TextField(
      autofocus: true,
      keyboardType: TextInputType.multiline,
      maxLines: null,
      autocorrect: true,
      onChanged: (s) => {},
      decoration: InputDecoration(
        border: InputBorder.none,
        isDense: true,
      ),
    );
  }

  _backButton() {}
}

这就是我的

这就是我想要的,右边的滚动条

我找不到执行此操作的方法,而且我不是 Flutter 专家,不知道如何自己执行此操作

幸运的是,TextField 小部件接受滚动控制器。这可用于生成滚动条。请参阅下面的示例。

TextEditingController _editTextController = TextEditingController();

// Initialise a scroll controller.
ScrollController _scrollController = ScrollController();

然后,将其添加到 TextField 小部件并将此小部件包装在 Scrollbar!

return Scrollbar(
  controller: _scrollController,
  isAlwaysShown: true,
  child: TextField(
    scrollController: _scrollController,
    autofocus: true,
    keyboardType: TextInputType.multiline,
    maxLines: null,
    autocorrect: true,
    onChanged: (s) => {},
    decoration: InputDecoration(
      border: InputBorder.none,
      isDense: true,
    ),
  ),
);