Flutter组件-文本组件

2021-10-15  本文已影响0人  aofeilin

1.Text类的结构

文本的对齐方式,文本显示的最大行数,代表文本相对于当前字体大小的缩放因子,


C Text.png

2.TextStyle类的结构

指定文本显示的样式如颜色、字体、粗细、背景等
height:该属性用于指定行高,但它并不是一个绝对值,而是一个因子,具体的行高等于fontSizeheight

截屏2021-10-14 16.46.33.png

3.Text类的使用

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) => MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
}

class MyTextStyle {
  TextStyle defultStyle() =>
      TextStyle(
          color: Colors.blue,
          fontSize: 18.0,
          height: 1.2,
          fontFamily: "Courier",
          background: Paint()..color=Colors.yellow,
          decoration:TextDecoration.underline,
          decorationStyle: TextDecorationStyle.dashed
      );
}

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

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class people {
  String msg;
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      people().msg = 'ss';
      print(people().msg);

      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) => Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              'You have pushed the button this many times:',
              style: Theme.of(context).textTheme.headline4,
            ),
            Text(
              'You have pushed the button this many times:',
              textAlign:TextAlign.left,
            ),
            Text(
              'You have pushed the button this many times:',
              textScaleFactor:1.5,
            ),
            Text(
              'You have pushed the button this many times:ellipsisellipsisellipsisellipsis',
              overflow:TextOverflow.ellipsis,
              style: MyTextStyle().defultStyle(),
            ),
            Text.rich(TextSpan(
              children: [
                TextSpan(
                  text: 'home'
                ),
                TextSpan(
                  text: 'https://flutterchina.club',
                  style: TextStyle(
                    color: Colors.blue,
                    decoration: TextDecoration.underline
                  )
                )
              ]
            ))

          ],

        ),
      ),

    );
}


3.Text的效果图

You have pushed the.PNG

参考:
https://book.flutterchina.club/chapter3/text.html

上一篇下一篇

猜你喜欢

热点阅读