Flutter 基础控件 Text Widget

2021-01-26  本文已影响0人  Cache技术分享

Flutter 基础控件 Text Widget

1. 创建TextExample继承StatefulWidget

class TextExample extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    throw TextExampleState();
  }
}

2. 创建TextExampleState继承TextExampleState

class TextExampleState extends State<TextExample> {
  @override
  Widget build(BuildContext context) {
    return text(context);
  }
}

3. 创建Widget组建

Widget text1(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text("Text"),
    ),
    body: Container(
      child: Column(
        children: <Widget>[
          Text("Text最简单用用法"), // 添加Text控件
        ],
      ),
    ),
  );
}

Text Widget 用法

Text("Text最简单用用法"),
Text(
    "文字",
    textAlign: TextAlign.center,
    style: TextStyle(
      fontSize: 18,
      decoration: TextDecoration.none,
    ),
),
  Text(
    "文字" * 60,
    textAlign: TextAlign.right,
    maxLines: 1,
    overflow: TextOverflow.ellipsis,
    style:
        TextStyle(fontSize: 25, decoration: TextDecoration.underline),
  ),
Text(
    "文字,文字",
    textAlign: TextAlign.left,
    textScaleFactor: 1.6,
    softWrap: true,
    style: TextStyle(
        fontSize: 20, color: Colors.black, fontWeight: FontWeight.bold),
),
 Text.rich(
            TextSpan(
                text: "textSpan",
                style: TextStyle(
                    color: Colors.orange,
                    fontSize: 30,
                    decoration: TextDecoration.lineThrough),
                children: <TextSpan>[
                  new TextSpan(
                      text: "连接A", style: new TextStyle(color: Colors.teal)),
                  new TextSpan(
                      text: "连接B",
                      style: new TextStyle(color: Colors.tealAccent)),
                  new TextSpan(
                      text: "连接C有点击事件",
                      style: new TextStyle(color: Colors.deepOrangeAccent),
                      recognizer: new TapGestureRecognizer()
                        ..onTap = () {
                          print("Flutter点击事件提示");
                        }),
                ]),
          ),
          RichText(
            text: TextSpan(
              text: 'Hello ',
              style: DefaultTextStyle.of(context).style,
              children: <TextSpan>[
                TextSpan(
                    text: 'bold',
                    style: TextStyle(
                        fontWeight: FontWeight.bold,
                        decoration: TextDecoration.none)),
                TextSpan(
                    text: ' world!',
                    style: TextStyle(
                        fontWeight: FontWeight.bold,
                        decoration: TextDecoration.none)),
              ],
            ),
          ),
image.png image.png

完整代码

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class TextExample extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return TextExampleState();
  }
}

class TextExampleState extends State<TextExample> {
  @override
  Widget build(BuildContext context) {
    return text1(context);
  }
}

Widget text1(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text("Text"),
    ),
    body: Container(
      child: Column(
        children: <Widget>[
          Text("Text最简单用用法"),
          Text(
            "文字",
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 18,
              decoration: TextDecoration.none,
            ),
          ),
          Text(
            "文字" * 60,
            textAlign: TextAlign.right,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style:
                TextStyle(fontSize: 25, decoration: TextDecoration.underline),
          ),
          Text(
            "文字,文字" * 60,
            textAlign: TextAlign.left,
            textScaleFactor: 1.6,
            softWrap: false,
            style: TextStyle(
                fontSize: 20, color: Colors.black, fontWeight: FontWeight.bold),
          ),
          Text.rich(
            TextSpan(
                text: "textSpan",
                style: TextStyle(
                    color: Colors.orange,
                    fontSize: 30,
                    decoration: TextDecoration.lineThrough),
                children: <TextSpan>[
                  new TextSpan(
                      text: "连接A", style: new TextStyle(color: Colors.teal)),
                  new TextSpan(
                      text: "连接B",
                      style: new TextStyle(color: Colors.tealAccent)),
                  new TextSpan(
                      text: "连接C有点击事件",
                      style: new TextStyle(color: Colors.deepOrangeAccent),
                      recognizer: new TapGestureRecognizer()
                        ..onTap = () {
                          print("Flutter点击事件提示");
                        }),
                ]),
          ),
          RichText(
            text: TextSpan(
              text: 'Hello ',
              style: DefaultTextStyle.of(context).style,
              children: <TextSpan>[
                TextSpan(
                    text: 'bold',
                    style: TextStyle(
                        fontWeight: FontWeight.bold,
                        decoration: TextDecoration.none)),
                TextSpan(
                    text: ' world!',
                    style: TextStyle(
                        fontWeight: FontWeight.bold,
                        decoration: TextDecoration.none)),
              ],
            ),
          ),
        ],
      ),
    ),
  );
}

上一篇 下一篇

猜你喜欢

热点阅读