gpt4 book ai didi

文本字段上的 Flutter 滚动条

转载 作者:行者123 更新时间:2023-12-05 08:29:53 25 4
gpt4 key购买 nike

我有全屏 TextField,当用户添加更多行/文本时,它会变大。我想为用户添加滚动条以了解 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() {}
}

这是我的

What I have

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

What I want

我找不到办法做到这一点,而且我不是 Flutter 专家,不知道如何自己做到这一点

最佳答案

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

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,
),
),
);

关于文本字段上的 Flutter 滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67475181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com