Flutter开发(十一):Flutter表单输入与富文本

1.获取用户输入

用户输入用 TextField 或 TextFormField 实现,通过 TextEditingContriller 获得用户输入。

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final myController = TextEditingController();

  @override
  void dispose() {
    // TODO: implement dispose
    myController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: TextField(
          controller: myController,
        ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){
           showDialog(
            context:context,
            builder: (context){
              return AlertDialog(
                content: Text(myController.text),
              );
            },
          );
        },
        tooltip: 'Show',
        child: Icon(Icons.text_fields),
      ),
    );
  }
}

2.设置输入框提示文字

设置参数 InputDecoration 来展示提示,或占位符数字。

        body: TextField(
          controller: myController,
          decoration: InputDecoration(hintText: "这是提示"),
        )

3.显示验证错误信息

InputDecoration 不仅可以添加提示信息,还可以添加错误信息。下面代码中 onSubmitted 方法,点击回车或确定会调用,errorText是提示的错误信息。

class _MyHomePageState extends State<MyHomePage> {
  final myController = TextEditingController();
  String _errorText = "";

  @override
  void dispose() {
    // TODO: implement dispose
    myController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: TextField(
        onSubmitted: (String text) {
          setState(() {
            if (!isEmail(text)) {
              _errorText = "输入错误";
            } else {
              _errorText = "";
            }
          });
        },
        controller: myController,
        decoration:
            InputDecoration(hintText: "这是提示", errorText: getErrorText()),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
            context: context,
            builder: (context) {
              return AlertDialog(
                content: Text(myController.text),
              );
            },
          );
        },
        tooltip: 'Show',
        child: Icon(Icons.text_fields),
      ),
    );
  }

  getErrorText() {
    return _errorText;
  }

  bool isEmail(String emailString) {
    print("输入信息为:" + emailString);
    if (emailString == "123") {
      return true;
    } else {
      return false;
    }
  }
}

4.富文本显示

一行文字显示多种样式,比如下面图片:

上面的样式,可以用 TextSpan 来实现,上面可以分为三个 TextSpan ,12之前是一个,12是单独一个,12之后是一个,创建一个 TextSpan 列表,加入三个即可,如下:

class _MyHomePageState extends State<MyHomePage> {
  List<TextSpan> spans =[];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    spans.add(TextSpan(text: "上海驿宿家民宿(民陈",style: TextStyle(fontSize: 20,color: Colors.blue)));
    spans.add(TextSpan(text: "12",style: TextStyle(fontSize: 20,color: Colors.yellow)));
    spans.add(TextSpan(text: "组路分店)",style: TextStyle(fontSize: 20,color: Colors.blue)));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: RichText(
        text: TextSpan(
          children: spans,
        ),
      ),
    );
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值