Flutter Container组件、Text组件详解

目录

[1. Container容器组件](#1. Container容器组件)

[1.1 Container使用](#1.1 Container使用)

[1.2 Container alignment使用](#1.2 Container alignment使用)

[1.3 Container border边框使用](#1.3 Container border边框使用)

[1.4 Container borderRadius圆角的使用](#1.4 Container borderRadius圆角的使用)

[1.5 Container boxShadow阴影的使用](#1.5 Container boxShadow阴影的使用)

[1.6 Container gradient背景颜色渐变](#1.6 Container gradient背景颜色渐变)

[1.7 Container gradient RadialGradient 背景颜色渐变](#1.7 Container gradient RadialGradient 背景颜色渐变)

[1.8 Container 实现圆角按钮](#1.8 Container 实现圆角按钮)

[1.9 Container transform的使用](#1.9 Container transform的使用)

[2. Text组件详解](#2. Text组件详解)

[2.1 TextStyle组件详解](#2.1 TextStyle组件详解)

[2.2 Text textAlign/maxLines/overflow 的使用](#2.2 Text textAlign/maxLines/overflow 的使用)

[2.2 Text 字符间距/倾斜字体的使用](#2.2 Text 字符间距/倾斜字体的使用)


1. Container容器组件

1.1 Container使用

复制代码
import 'package:flutter/material.dart';

main() {
  runApp(
    MaterialApp(
      home: Scaffold(appBar: AppBar(title: Text("Home")), body: MyApp()),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        alignment: Alignment.topLeft, //配置Container容器内的方位
        width: 100,
        height: 100,
        decoration: BoxDecoration(color: Colors.yellow),
        child: Text("你好", style: TextStyle(color: Colors.blue)),
      ),
    );
  }
}

1.2 Container alignment使用

复制代码
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        alignment: Alignment.center, //配置Container容器内的方位
        width: 100,
        height: 100,
        decoration: BoxDecoration(color: Colors.yellow),
        child: Text("你好", style: TextStyle(color: Colors.blue)),
      ),
    );
  }
}

1.3 Container border边框使用

复制代码
import 'package:flutter/material.dart';

main() {
  runApp(
    MaterialApp(
      home: Scaffold(appBar: AppBar(title: Text("Home")), body: MyApp()),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        alignment: Alignment.center,
        //配置Container容器内的方位
        width: 100,
        height: 100,
        decoration: BoxDecoration(
          color: Colors.yellow,
          border: Border.all(color: Colors.red), //边框
        ),
        child: Text("你好", style: TextStyle(color: Colors.blue)),
      ),
    );
  }
}

1.4 Container borderRadius圆角的使用

复制代码
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        alignment: Alignment.center,
        //配置Container容器内的方位
        width: 100,
        height: 100,
        decoration: BoxDecoration(
          color: Colors.yellow,
          border: Border.all(color: Colors.red),//边框
          borderRadius: BorderRadius.circular(10),//配置圆角
        ),
        child: Text("你好", style: TextStyle(color: Colors.blue)),
      ),
    );
  }
}

1.5 Container boxShadow阴影的使用

复制代码
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        alignment: Alignment.center,
        //配置Container容器内的方位
        width: 100,
        height: 100,
        decoration: BoxDecoration(
          color: Colors.yellow,
          border: Border.all(color: Colors.red),//边框
          borderRadius: BorderRadius.circular(10),//配置圆角
          boxShadow:[ //阴影
            BoxShadow(
              color: Colors.black,
              blurRadius: 10
            )
          ]
        ),
        child: Text("你好", style: TextStyle(color: Colors.blue)),
      ),
    );
  }
}

1.6 Container gradient背景颜色渐变

复制代码
import 'package:flutter/material.dart';

main() {
  runApp(
    MaterialApp(
      home: Scaffold(appBar: AppBar(title: Text("Home")), body: MyApp()),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        alignment: Alignment.center,
        //配置Container容器内的方位
        width: 100,
        height: 100,
        decoration: BoxDecoration(
          color: Colors.yellow,
          border: Border.all(color: Colors.red),//边框
          borderRadius: BorderRadius.circular(10),//配置圆角
          boxShadow:[ //阴影
            BoxShadow(
              color: Colors.black,
              blurRadius: 2
            )
          ],
          gradient:LinearGradient(colors: [Colors.red,Colors.yellow,]) //背景颜色渐变 LinearGradient=线性渐变 RadialGradient= 径性渐变
        ),
        child: Text("你好", style: TextStyle(color: Colors.blue)),
      ),
    );
  }
}
1.7 Container gradient RadialGradient 背景颜色渐变

1.8 Container 实现圆角按钮

复制代码
import 'package:flutter/material.dart';

main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("Home")),
        body: Column(children: [MyApp(), MyButton()]),
      ),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        alignment: Alignment.center,
        //配置Container容器内的方位
        width: 100,
        height: 100,
        decoration: BoxDecoration(
          color: Colors.yellow,
          border: Border.all(color: Colors.red),
          //边框
          borderRadius: BorderRadius.circular(10),
          //配置圆角
          boxShadow: [
            //阴影
            BoxShadow(color: Colors.black, blurRadius: 2),
          ],
          gradient: LinearGradient(
            colors: [Colors.red, Colors.yellow],
          ), //背景颜色渐变 LinearGradient=线性渐变 RadialGradient= 径性渐变
        ),
        child: Text("你好", style: TextStyle(color: Colors.blue)),
      ),
    );
  }
}

class MyButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      // margin: EdgeInsets.all(10), //边距
      // margin: EdgeInsets.all(10), //边距
      margin: EdgeInsets.fromLTRB(0,20,0,0), //边距
      height: 48,
      width: 150,
      decoration: BoxDecoration(
        color: Colors.blue,
        borderRadius: BorderRadius.circular(30),
      ),
      child: Text("按钮", style: TextStyle(color: Colors.white)),
    );
  }
}

1.9 Container transform的使用

复制代码
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        alignment: Alignment.center,
        //配置Container容器内的方位
        width: 100,
        height: 100,
        // transform: Matrix4.translationValues(10, 0, 0),//位移
        // transform: Matrix4.rotationZ(1),//选择
        transform: Matrix4.skewY(3),//倾斜
        decoration: BoxDecoration(
          color: Colors.yellow,
          border: Border.all(color: Colors.red),
          //边框
          borderRadius: BorderRadius.circular(10),
          //配置圆角
          boxShadow: [
            //阴影
            BoxShadow(color: Colors.black, blurRadius: 2),
          ],
          gradient: LinearGradient(
            colors: [Colors.red, Colors.yellow],
          ), //背景颜色渐变 LinearGradient=线性渐变 RadialGradient= 径性渐变
        ),
        child: Text("你好", style: TextStyle(color: Colors.blue)),
      ),
    );
  }
}

2. Text组件详解

2.1 TextStyle组件详解

2.2 Text textAlign/maxLines/overflow 的使用

复制代码
child: Text(
        "HowHowHowHowHowHowHowHowHowHowHowHowHowHowHowHow",
        textAlign: TextAlign.center,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      )

2.2 Text 字符间距/倾斜字体的使用

复制代码
  child: Text(
        "HowHowHowHowHowHowHowHowHowHowHowHowHowHowHowHow",
        textAlign: TextAlign.center,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
         style: TextStyle(
           fontSize: 20,
           fontWeight: FontWeight.w800,//字体大小
           color: Colors.red,
           fontStyle: FontStyle.italic,//倾斜字体
           letterSpacing: 7 //字符间距
         ),
      ),

2.3 Text 底部下划线/底部虚线的使用

复制代码
 child: Text(
        "HowHowHowHowHowHowHowHowHowHowHowHowHowHowHowHow",
        textAlign: TextAlign.center,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontSize: 20,
          fontWeight: FontWeight.w800,
          //字体大小
          color: Colors.red,
          fontStyle: FontStyle.italic,
          //倾斜字体
          letterSpacing: 1,
          //字符间距
          decoration: TextDecoration.underline,
          decorationColor: Colors.black //底部下划线
          ,decorationStyle: TextDecorationStyle.dashed //底部虚线
        ),
      ),
相关推荐
天天打码33 分钟前
npm/yarn/pnpm安装时Sharp模块报错解决方法
前端·npm·node.js
码农捻旧43 分钟前
JavaScript 性能优化按层次逐步分析
开发语言·前端·javascript·性能优化
小辉懂编程1 小时前
2025年最新基于Vue基础项目Todolist任务编辑器【适合新手入手】【有这一片足够了】【附源码】
前端·vue.js·编辑器
我是哈哈hh1 小时前
【Vue3】生命周期 & hook函数 & toRef
开发语言·前端·javascript·vue.js·前端框架·生命周期·proxy模式
傻虎贼头贼脑1 小时前
day28JS+Node-JS打包工具Webpack
开发语言·前端·javascript·webpack
菥菥爱嘻嘻3 小时前
React---day2
前端·javascript·react.js
前端_学习之路4 小时前
React与Vue的内置指令对比
前端·vue.js·react.js
会飞的鱼先生4 小时前
vue3自定义指令来实现 v-focus 功能
前端·javascript·vue.js
伍六星4 小时前
在 Vue + Vite 项目中,直接使用相对路径或绝对路径引用本地图片资源时,图片无法正确显示。
前端·javascript·vue.js
余渔鱼11234 小时前
VUE项目部署IIS服务器手册
服务器·前端·vue.js