簡體   English   中英

如何在 Flutter 中以固定行數顯示一個 TextFormField 的最后一行

[英]How to show last line of one TextFormField with fixed number of lines in Flutter

我有一個固定行數的 TextFormField,當屏幕加載時,這個 TextFormField 已經有比我定義的固定行數更多的行,

如果我想看最后一行

我需要手動將其滾動到最后一行。

有沒有辦法自動顯示此 TextFormField 的最后一行?

    Container(
                  child:  SingleChildScrollView(
                    child: TextField(
                      readOnly: true,
                      keyboardType: TextInputType.multiline,
                      maxLines: 9,
                      controller: chatController,
                      decoration: InputDecoration(
                      hintText: "Chat...",
                      border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(15.0),
                        ),
                      ),
                    ),
                  ),
                ),

這似乎是 Flutter 框架中缺少的功能,有人提交了一個錯誤來解決它: https : //github.com/flutter/flutter/issues/9365

但是站點包括一個變通方法。 請看一看。

  1. 使這個公共變量:

     ScrollController scrollController = new ScrollController();
  2. 將其附加到 TextField

     TextField( //... scrollController: scrollController, //... ),
  3. 把它放到你的 initState 函數中:

     WidgetsBinding.instance.addPostFrameCallback((_) async { scrollController.jumpTo(double.parse((9 * 17 * 1.9 /*number of lines * fontSize * 1.9 (you can of course experiment with this values)*/).toStringAsFixed(1))); });

我希望它有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM