Textfield选择荧光笔控件看起来太高

发布于 2025-02-12 07:01:47 字数 1199 浏览 1 评论 0原文

出于某种奇异的原因,下面使用的问题和代码的屏幕截图在文本字段上方。

var textField = TextField(
  selectionHeightStyle: BoxHeightStyle.max,
  scrollPadding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom + 20 * 4),
  textInputAction: TextInputAction.done,
  keyboardType: TextInputType.numberWithOptions(signed: true, decimal: true),
  decoration: InputDecoration(
    border: InputBorder.none,
  ),
  controller: textEditingController,
  onTap: () {
    Future.delayed(const Duration(milliseconds: 800), () {
      textEditingController.selectAll();
    });
  },
  onSubmitted: (newText) {
    if (newText.length == 0) {
      callback(0, true);
    } else {
      callback(int.parse(newText), true);
    }
  },
  textAlign: TextAlign.center,
  style: Theme.of(context).textTheme.headline4!.copyWith(
        fontWeight: FontWeight.w700,
        fontSize: 16.0,
        color: WWColors.pinkBrandColor(),
      ),
);

return Padding(
  padding: padding,
  child: Container(alignment: Alignment.center, child: textField),
);

Screenshot of the issue and code in use below, the selection controls are above the textfield for some bizarre reason.

Example of issue

var textField = TextField(
  selectionHeightStyle: BoxHeightStyle.max,
  scrollPadding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom + 20 * 4),
  textInputAction: TextInputAction.done,
  keyboardType: TextInputType.numberWithOptions(signed: true, decimal: true),
  decoration: InputDecoration(
    border: InputBorder.none,
  ),
  controller: textEditingController,
  onTap: () {
    Future.delayed(const Duration(milliseconds: 800), () {
      textEditingController.selectAll();
    });
  },
  onSubmitted: (newText) {
    if (newText.length == 0) {
      callback(0, true);
    } else {
      callback(int.parse(newText), true);
    }
  },
  textAlign: TextAlign.center,
  style: Theme.of(context).textTheme.headline4!.copyWith(
        fontWeight: FontWeight.w700,
        fontSize: 16.0,
        color: WWColors.pinkBrandColor(),
      ),
);

return Padding(
  padding: padding,
  child: Container(alignment: Alignment.center, child: textField),
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉溺在你眼里的海 2025-02-19 07:01:47

在回答我自己的问题时,在我的情况下,这在Textfield属性中完成了这项工作:

  decoration: InputDecoration(
    isDense: true, // important line
    contentPadding: EdgeInsets.fromLTRB(0, 5, 0, 5),
    border: InputBorder.none,
  ),

In answer to my own question, this in the TextField attributes did the job in my case:

  decoration: InputDecoration(
    isDense: true, // important line
    contentPadding: EdgeInsets.fromLTRB(0, 5, 0, 5),
    border: InputBorder.none,
  ),
人│生佛魔见 2025-02-19 07:01:47

嗨,您可以尝试为其创建一个自定义小部件以进行自定义,如下所示,是上述实现的简单示例。

class CounterWidget extends StatefulWidget {
  final double width;
  final double height;
  final double spaceWidth;
  final Color controlColor;
  final Color valueColor;
  final Function(int count) callback;
  const CounterWidget({
    Key? key,
    required this.title,
    required this.callback,
    required this.controlColor,
    required this.valueColor,
    this.width = 50,
    this.height = 30,
    this.spaceWidth = 5,
  }) : super(key: key);

  final String title;

  @override
  State<CounterWidget> createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
  int count = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Row(
        children: [
          //Incrementor
          InkWell(
            onTap: () {
              setState(() {
                count++;
                //Callback method to expose the count to the higher hier archy.
                widget.callback(count);
              });
            },
            child: Container(
              decoration: BoxDecoration(
                color: widget.controlColor,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(widget.height / 0.1),
                  bottomLeft: Radius.circular(widget.height / 0.1),
                ),
              ),
              child: const Center(
                child: Icon(
                  Icons.add,
                  color: Colors.white,
                ),
              ),
              width: widget.width,
              height: widget.height,
            ),
          ),
          SizedBox(width: widget.spaceWidth),
          Container(
            color: widget.valueColor,
            width: widget.width,
            height: widget.height,
            child: Center(
              child: Text(
                count.toString(),
                style: const TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                  fontSize: 20,
                ),
              ),
            ),
          ),
          SizedBox(width: widget.spaceWidth),
          InkWell(
            onTap: () {
              if (count > 0) {
                setState(() {
                  count--;
                  widget.callback(count);
                });
              }
            },
            child: Container(
              width: widget.width,
              height: widget.height,
              decoration: BoxDecoration(
                color: widget.controlColor,
                borderRadius: BorderRadius.only(
                  topRight: Radius.circular(widget.height / 0.1),
                  bottomRight: Radius.circular(widget.height / 0.1),
                ),
              ),
              child: const Center(
                child: Icon(
                  Icons.remove,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}


Hi you can try create a custom widget for it to make the customisation as you required below is the simple example for the mentioned implementation.

class CounterWidget extends StatefulWidget {
  final double width;
  final double height;
  final double spaceWidth;
  final Color controlColor;
  final Color valueColor;
  final Function(int count) callback;
  const CounterWidget({
    Key? key,
    required this.title,
    required this.callback,
    required this.controlColor,
    required this.valueColor,
    this.width = 50,
    this.height = 30,
    this.spaceWidth = 5,
  }) : super(key: key);

  final String title;

  @override
  State<CounterWidget> createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
  int count = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Row(
        children: [
          //Incrementor
          InkWell(
            onTap: () {
              setState(() {
                count++;
                //Callback method to expose the count to the higher hier archy.
                widget.callback(count);
              });
            },
            child: Container(
              decoration: BoxDecoration(
                color: widget.controlColor,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(widget.height / 0.1),
                  bottomLeft: Radius.circular(widget.height / 0.1),
                ),
              ),
              child: const Center(
                child: Icon(
                  Icons.add,
                  color: Colors.white,
                ),
              ),
              width: widget.width,
              height: widget.height,
            ),
          ),
          SizedBox(width: widget.spaceWidth),
          Container(
            color: widget.valueColor,
            width: widget.width,
            height: widget.height,
            child: Center(
              child: Text(
                count.toString(),
                style: const TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                  fontSize: 20,
                ),
              ),
            ),
          ),
          SizedBox(width: widget.spaceWidth),
          InkWell(
            onTap: () {
              if (count > 0) {
                setState(() {
                  count--;
                  widget.callback(count);
                });
              }
            },
            child: Container(
              width: widget.width,
              height: widget.height,
              decoration: BoxDecoration(
                color: widget.controlColor,
                borderRadius: BorderRadius.only(
                  topRight: Radius.circular(widget.height / 0.1),
                  bottomRight: Radius.circular(widget.height / 0.1),
                ),
              ),
              child: const Center(
                child: Icon(
                  Icons.remove,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文