flutter CupertinoTextField实现应用常用反馈,意见输入

效果

在这里插入图片描述

在这里插入图片描述
调用:
MyTextInput(
hint: “请输入你的意见”,
onChanged: (str) {
//str为文本内容
})

MyTextInput源码:

import ‘package:flutter/cupertino.dart’;
import ‘package:flutter/material.dart’;

class MyTextInput extends StatefulWidget {
final String hint;
final double height;
final Function(String) onChanged;

MyTextInput({
this.hint,
this.onChanged,
this.height,
});

@override
State createState() {
return MyTextInputState();
}
}

class MyTextInputState extends State
with SingleTickerProviderStateMixin {
@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return Container(
child: _textInput(widget.hint),
);
}

String result = “”;

_textInput(String hint) {
return Stack(
alignment: AlignmentDirectional.bottomEnd,
children: [
Container(
height: widget.height,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(4.0),
),
border: Border.all(color: Color(0xFFDEDEDE)),
),
alignment: AlignmentDirectional.topStart,
margin: EdgeInsets.only(bottom: 20),
child: CupertinoTextField(
placeholder: hint,
style: TextStyle(fontSize: 15, color: Colors.black),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(2)),
),
maxLines: 10,
maxLength: 150,
maxLengthEnforced: true,
textInputAction: TextInputAction.unspecified,
onChanged: (str) {
result = str;
widget.onChanged(result);
setState(() {
});
},
),
),
Container(
margin: EdgeInsets.only(bottom: 25, right: 20),
child: Text(
result.length.toString() + “/150”,
style: TextStyle(color: Colors.grey),
),
)
],
);
}
}

猜你喜欢

转载自blog.csdn.net/qq_36071410/article/details/87890272