【问题标题】:Flutter Fill in Blank with RichTextFlutter 用 RichText 填空
【发布时间】:2020-05-19 23:34:44
【问题描述】:

我几乎完成了我的应用程序的填充空白实现,但不幸的是我一直遇到布局问题。

但是,随着我不断解决问题,我几乎完成了它,这是到目前为止的代码:

                        RichText(
                            text: TextSpan(
                                text: "dummy",
                                style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 20,
                                    height: 1.5,
                                    fontWeight: FontWeight.bold),
                                children: <InlineSpan>[
                                  TextSpan(
                                      text:
                                          ' to  to  to  to gdfgdfgdf to  to  to  to  to  to  to  ',
                                      style: TextStyle(
                                          height: 1.0,
                                          color: Colors.grey,
                                          fontSize: 20,
                                          fontWeight: FontWeight.bold)),
                                  WidgetSpan(
                                    alignment: PlaceholderAlignment.middle,
                                    child: SecretWord("turtle"),
                                  ),
                                  TextSpan(
                                      text:
                                          ' to  to  to  to gdfgdfgdf to  to  to  to  to  to  to  ',
                                      style: TextStyle(
                                          height: 1.0,
                                          color: Colors.grey,
                                          fontSize: 20,
                                          fontWeight: FontWeight.bold)),
                            )

还有我的 SecretWord 课程:

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';

class SecretWord extends StatelessWidget {

    final String answer;
    int answerLength;
    String answerHint;
    double answerWidth;

    SecretWord(this.answer){
        this.answerLength = answer.length;
        this.answerHint = '.' * answerLength;
        this.answerWidth = this.answerLength * 15.0;
    }

    String value = "";

    @override
    Widget build(BuildContext context) {
        return Container(
            //alignment: Alignment.bottomCenter,
            width: answerWidth,
            height: null,

          //  margin: const EdgeInsets.only(right: 10, left: 10),
            child: TextFormField(
                maxLines: null,
                cursorColor: Colors.cyanAccent,
                cursorRadius: Radius.circular(12.0),
                cursorWidth: 2.0,
                style: TextStyle(
                    color: (value == answer) ? Colors.amberAccent : Colors.lightGreenAccent,
                    fontWeight: FontWeight.bold,
                    fontSize: 20,
                    letterSpacing: 3,
                  //  height: 0.5,
                ),
                //textAlign: TextAlign.left,
                autofocus: false,
                maxLength: answerLength,
                onChanged: (text) {
                    value = text;

                },

                decoration: new InputDecoration(

                    //labelText: 'Name *',
                    border: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    enabledBorder: InputBorder.none,
                    errorBorder: InputBorder.none,
                    disabledBorder: InputBorder.none,
                    counterText: '',
                    hintText: answerHint,
                    hintStyle: TextStyle(

                        color: Colors.lightGreenAccent,
                        fontWeight: FontWeight.bold,
                        letterSpacing: 4,
                       // height: 0.5,
                    ),
                )
            )
        );
    }
}

不幸的是,这会产生一些问题:SecretWord 的容器高度优于 TextSpan,我怎样才能成功地使用 TextFormField 降低容器的高度以匹配 TextSpan 的高度?

请注意,第二行与第一行和第三行的空间比我预期的要大,这是因为 SecretWord 被认为垂直占用更多空间。我知道原因,但不知道如何解决。

【问题讨论】:

  • 尝试使用Expandedwidget
  • 我试过了,但更糟糕的是,我得到了每个 SecretWord() 的换行符。你有一个可行的例子吗?
  • 你能分享你看到的布局问题的图片吗?
  • 我刚刚添加了图片

标签: flutter abstract-class richtext


【解决方案1】:

您好,我想出了一个解决方案来减少 TextField/TextFormField 的内部填充,这看起来像您的问题。

为 TextField 的 InputDecoration 设置这些值应该会移除垂直填充:

     TextFormField(
          decoration: InputDecoration(
          isDense: true,
          contentPadding: const EdgeInsets.symmetric(vertical: -5),
          counterText: '',
        ),
      )

isDense=true 使输入是密集形式的一部分(即,使用较少的垂直 /// 空格)。

设置 contentPadding: const EdgeInsets.symmetric(vertical: -5) 将减少垂直填充

正如您在示例中所做的那样 counterText: '' 将阻止显示计数器文本。

所以这是你的新 SecretWordClass 与更改。我还移动了 build 方法下的属性,因为无状态小部件是不可变的,它们的属性应该是最终的。

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';

class SecretWord extends StatelessWidget {
  final String answer;

  SecretWord(this.answer);

  @override
  Widget build(BuildContext context) {
    String value = "";
    int answerLength = answer.length;
    String answerHint = '.' * answerLength;
    double answerWidth = answerLength * 15.0;
    return Container(
      width: answerWidth,
      height: null,
      child: TextFormField(
        maxLines: null,
        cursorColor: Colors.cyanAccent,
        cursorRadius: Radius.circular(12.0),
        cursorWidth: 2.0,
        style: TextStyle(
          color:
              (value == answer) ? Colors.amberAccent : Colors.lightGreenAccent,
          fontWeight: FontWeight.bold,
          fontSize: 20,
          letterSpacing: 3,
        ),
        autofocus: false,
        maxLength: answerLength,
        onChanged: (text) {
          value = text;
        },
        decoration: new InputDecoration(
          isDense: true,
          contentPadding: const EdgeInsets.symmetric(vertical: -5),
          counterText: '',
          border: InputBorder.none,
          focusedBorder: InputBorder.none,
          enabledBorder: InputBorder.none,
          errorBorder: InputBorder.none,
          disabledBorder: InputBorder.none,
          hintText: answerHint,
          hintStyle: TextStyle(
            color: Colors.lightGreenAccent,
            fontWeight: FontWeight.bold,
            letterSpacing: 4,
          ),
        ),
      ),
    );
  }
}

结果如下:

【讨论】:

  • 谢谢Ali,正是我要找的,也谢谢你在build方法下移动属性的额外提示!
  • 欢迎您。我很高兴这种 hacky 方式对你有用。祝你的应用好运。
猜你喜欢
  • 1970-01-01
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 2023-02-24
  • 2013-09-28
  • 2020-12-19
  • 2020-06-10
  • 1970-01-01
相关资源
最近更新 更多