Flutter输入框换行后自适应高度

本文介绍了如何使用Flutter中的TextEditingController监听输入框内容变化,动态计算输入框高度,以实现输入框高度随换行自动适应的自适应高度设计。
摘要由CSDN通过智能技术生成

Flutter输入框换行后输入框高度随之增加

效果

请添加图片描述
请添加图片描述

设计思想

通过TextEditingController在build中监听输入框,输入内容后计算输入框高度然后自定义适合的值,并且改变外部容器高度达到自适应高度的目的

参考代码

//以下代码中的值只适用于案例,实际情况还需改为自己需要的实际值

//自定义输入框高度
 double inputH=25; 

 
  Widget build(BuildContext context) {
    _textEditingController.addListener(() {
      String textvalue=_textEditingController.text;
      final textStyle = TextStyle(fontSize: 16,fontWeight: FontWeight.bold);

      final textPainter = TextPainter(
        text: TextSpan(text:textvalue, style: textStyle),
        textDirection: TextDirection.ltr,
      );
      textPainter.layout(maxWidth: 260 * Global.widthX); // 减去/加上的数值根据自己需求调整,下同
      setState(() {
        if(textPainter.height<=25.0){ //只有一行的情况
          this.inputH=25.0;
        }else if(25.0<textPainter.height && textPainter.height<=95){ //大于一行小于等于4行的情况
          this.inputH=textPainter.height;
        }else if(textPainter.height>=95){ //大于四行后固定高度,输入框内容滑动展示
          this.inputH=95.0;
        }
      });
     
    });
	return Container();

}

Widget _bottom(){
 return SizedBox(
	height:inputH*Global.heightY+80*Global.heightY,
	child:Container(
		//输入框外部容器高度
		 constraints: BoxConstraints.tightFor(width: Global.screenWidth,height:inputH*Global.heightY+50*Global.heightY),
		 child: Container(
            constraints: BoxConstraints(
                maxHeight: 100.0,
                maxWidth: 265 * Global.widthX,
                minHeight: 20*Global.heightY,
                minWidth: 265 * Global.widthX),
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border.all(  //边框,颜色以及宽度
                // color: Color(0xFFFCCCCCC),
                width: 1.0,
                color: Colors.black12
              ),
              borderRadius: BorderRadius.circular(10.0),
            ),
            margin: EdgeInsets.fromLTRB(9* Global.widthX, 0, 0, 0),
            padding: EdgeInsets.fromLTRB(5* Global.widthX,0, 0, 0),
            child: Center(

              child: TextField(
                // focusNode: _focusNode1,
                maxLines: null,
                // maxLength: 170,
                keyboardType: TextInputType.multiline,
                controller: _textEditingController,
                // textAlign: TextAlign.center,
                textAlignVertical: TextAlignVertical.center,
                decoration: InputDecoration.collapsed(

                  hintText: _hintText,

                ),
                
              ),
            ),
          ),
		 
	)
);
}
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 Flutter WebView 页面自适应高度,可以使用 flutter_webview_plugin 插件。该插件提供了一个 onStateChanged 回调函数,可以监听 WebView 页面的状态改变。当页面加载完成后,可以获取 WebView 页面的高度,并将其应用到 WebView 上,从而实现自适应高度。 以下是一个简单的示例代码,演示如何实现 Flutter WebView 页面自适应高度: ```dart import 'package:flutter/material.dart'; import 'package:flutter_webview_plugin/flutter_webview_plugin.dart'; class MyWebView extends StatefulWidget { final String url; MyWebView({required this.url}); @override _MyWebViewState createState() => _MyWebViewState(); } class _MyWebViewState extends State<MyWebView> { double webViewHeight = 100.0; @override Widget build(BuildContext context) { return Scaffold( body: Container( height: webViewHeight, child: WebviewScaffold( url: widget.url, withZoom: false, withJavascript: true, withLocalStorage: true, hidden: true, initialChild: Container( color: Colors.white, child: Center( child: CircularProgressIndicator(), ), ), appBar: AppBar( title: Text('WebView Demo'), ), onStateChanged: (state) { if (state.type == WebViewState.finishLoad) { setState(() { webViewHeight = double.parse(state.height.toString()); }); } }, ), ), ); } } ``` 在上面的代码中,我们定义了一个 MyWebView 类,使用 WebviewScaffold 来渲染 WebView 页面。在 onStateChanged 回调函数中,我们监听 WebView 页面的状态改变,并在页面加载完成后获取 WebView 的高度,并将其应用到 WebView 上。这样,就可以实现 WebView 页面的自适应高度了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值