当前位置: 首页 > news >正文

Flutter widgets——Text/Icon/Button

接着上一篇我们继续撸widget

Text

const Text(this.data, {
    Key key,
    this.style,
    this.textAlign,
    this.textDirection,
    this.locale,
    this.softWrap,
    this.overflow,
    this.textScaleFactor,
    this.maxLines,
    this.semanticsLabel,
  }) : assert(data != null),
       textSpan = null,
       super(key: key);
复制代码

softWrap 自动换行

overflow 文本超出样式

/// How overflowing text should be handled.
enum TextOverflow {
  /// Clip the overflowing text to fix its container.
  clip,

  /// Fade the overflowing text to transparent.
  fade,

  /// Use an ellipsis to indicate that the text has overflowed.
  ellipsis,
}
复制代码

Sample code

new Text(
  'Hello, $_name! How are you?',
  textAlign: TextAlign.center,
  overflow: TextOverflow.ellipsis,
  style: new TextStyle(fontWeight: FontWeight.bold),
) 
复制代码

我们也可以用Text.rich来构造不同样式的TextSpan(跟UWP里面完全一样)

const Text.rich(this.textSpan, {
    Key key,
    this.style,
    this.textAlign,
    this.textDirection,
    this.locale,
    this.softWrap,
    this.overflow,
    this.textScaleFactor,
    this.maxLines,
    this.semanticsLabel,
  }): assert(textSpan != null),
      data = null,
      super(key: key);
复制代码

Sample code

const Text.rich(
  const TextSpan(
    text: 'Hello', // default text style
    children: const <TextSpan>[
      const TextSpan(text: ' beautiful ', style: const TextStyle(fontStyle: FontStyle.italic)),
      const TextSpan(text: 'world', style: const TextStyle(fontWeight: FontWeight.bold)),
    ],
  ),
)
复制代码

如果你想让某个Widget里面的Text都使用共同一个Style的话。我们可以使用DefaultTextStyle,Style的原则还是就近原则,从树的叶子开始到根,这点也是跟UWP一致,各种Style也是就近原则进行merge的。

return DefaultTextStyle(
      style: TextStyle(color: Colors.red),
      child: new Column(
        children: <Widget>[
          new Text(
            'Hello, How are you?Hello, How are you?Hello, How are you?Hello, How are you?Hello, How are you?Hello, How are you?',
            textAlign: TextAlign.center,
            overflow: TextOverflow.ellipsis,
            style: new TextStyle(fontWeight: FontWeight.bold),
          ),
          Text.rich(
            const TextSpan(

              text: 'Hello', // default text style
              children: const <TextSpan>[
                const TextSpan(
                    text: ' beautiful ',
                    style: const TextStyle(fontStyle: FontStyle.italic)),
                const TextSpan(
                    text: 'world',
                    style: const TextStyle(fontWeight: FontWeight.bold)),
              ],
            ),
          )
          ,
          Text("hello flutter text",style: Theme.of(context).textTheme.title,),
        ],
      ),
    )
复制代码

Icon Flutter 里面的Icon是字体Icon,它的Data是IconData

class Icons {
  Icons._();

  // Generated code: do not hand-edit.
  // See https://github.com/flutter/flutter/wiki/Updating-Material-Design-Fonts
  // BEGIN GENERATED

  /// <i class="material-icons md-36">360</i> &#x2014; material icon named "360".
  static const IconData threesixty = IconData(0xe577, fontFamily: 'MaterialIcons');
复制代码

如果系统没有你想要的。。你可以去更新它

当然你也可以通过引用自定义的TTF来完成

这里是官方的一些自定义的Icon 搜搜看,很方便 下载好的字体文件夹为

将fonts文件夹里面的MyFlutterApp.ttf 放到项目的fonts文件夹下面 pubspec.yaml文件里面加入下面配置。。注意缩进。。

  fonts:
    - family:  MyFlutterApp
      fonts:
       - asset: fonts/MyFlutterApp.ttf
复制代码
import 'package:widgets_sample/Common/my_flutter_app_icons.dart';

    new Column(
      children: <Widget>[
        Icon(Icons.add),
        Icon(MyFlutterApp.spin3),
        IconButton(icon: Icon(Icons.list), onPressed: () => {})
      ],
    );
复制代码

RaisedButton

Flutter的世界里面任何东西都可以由更小的Widget组成

Button 最下一层实现是InkWell(提供交互响应),如果你只是想要tap等事件,不想要那种duangduangduang的水波纹效果,你可以使用GestureDetector。

///  * [GestureDetector], for listening for gestures without ink splashes.
///  * [RaisedButton] and [FlatButton], two kinds of buttons in material design.
///  * [InkResponse], a variant of [InkWell] that doesn't force a rectangular
///    shape on the ink reaction.
复制代码

Button默认是有最小宽度88.0,高36.0,

你可以通过包一个ButtonTheme重写

child: ButtonTheme(
        textTheme: ButtonTextTheme.normal,
        minWidth: 88.0,
        height: 36.0,
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text("RaisedButton"),
              onPressed: () => {},
            ),
复制代码

Button的Disable状态是由OnPressed事件是否为Null来判断的。

Flutter本身提供很多种Button。

OutlineButton 下面是一个自定义的圆角按钮

class ButtonWithRadius extends OutlineButton {
  ButtonWithRadius(
      {Key key,
      @required onPressed,
      Color color,
      Color textColor,
      Widget child,
      double radius = 10.0})
      : super(
            key: key,
            onPressed: onPressed,
            child: child,
            color: color,
            textColor: textColor,
            shape: new RoundedRectangleBorder(
                side: BorderSide.none,
                borderRadius: new BorderRadius.circular(radius)));
复制代码

FlatButton

DropdownButton

            DropdownButton(
              value: dropdownValue,
              items: <String>['One', 'Two', 'Free', 'Four'].map((String value) {
                return new DropdownMenuItem<String>(
                  value: value,
                  child: new Text(value),
                );
              }).toList(),
              onChanged: (String newValue) {
                setState(() {
                  dropdownValue = newValue;
                });
              },
            ),
复制代码

IconButton

            IconButton(
              icon: Icon(Icons.add),
              onPressed: () {},
            ),
复制代码

InkWell A rectangular area of a Material that responds to touch.

ButtonBar

FloatingActionButton

            FloatingActionButton(
                child: const Icon(Icons.add),
                onPressed: () {
                  // Perform some action
                }),
复制代码

PopupMenuButton

            PopupMenuButton(itemBuilder: (BuildContext context) {
              return <String>['One', 'Two', 'Free', 'Four'].map((String item) {
                return new PopupMenuItem<String>(
                  value: item,
                  child: new Text(item),
                );
              }).toList();
            })
复制代码

全部以上介绍的Widgets都在Sample Code。

相关文章:

  • window 环境 spring boot 发布脚本整理
  • The POM for XXX is invalid, transitive dependencies (if any) will not be available解决方案
  • spring第一冲刺阶段第四天
  • 福大软工1816 · 团队现场编程实战(抽奖系统)
  • Servlet重写init(ServletConfig config)还是init()
  • 微信小程序填坑清单
  • 【.NET Core项目实战-统一认证平台】第五章 网关篇-自定义缓存Redis
  • WPF自定义Window窗体样式
  • python编程入门----while与文件用法
  • [洛谷P3950]部落冲突
  • 技术工坊|高TPS和去中心化存储带来的第三代区块链技术革新机遇(深圳)
  • 深入Redis持久化
  • 【模板】最近公共祖先(LCA)
  • 端口的作用
  • Scrum立会报告+燃尽图(十一月二十二日总第三十次):加强回归测试
  • 【RocksDB】TransactionDB源码分析
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • chrome扩展demo1-小时钟
  • JavaScript标准库系列——Math对象和Date对象(二)
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • JS实现简单的MVC模式开发小游戏
  • js作用域和this的理解
  • k8s如何管理Pod
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • Web标准制定过程
  • 跨域
  • 入门到放弃node系列之Hello Word篇
  • 实战|智能家居行业移动应用性能分析
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 算法之不定期更新(一)(2018-04-12)
  • 微信开源mars源码分析1—上层samples分析
  • 微信小程序实战练习(仿五洲到家微信版)
  • 终端用户监控:真实用户监控还是模拟监控?
  • 自动记录MySQL慢查询快照脚本
  • 扩展资源服务器解决oauth2 性能瓶颈
  • #android不同版本废弃api,新api。
  • #pragma once与条件编译
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • (1) caustics\
  • (3)llvm ir转换过程
  • (3)STL算法之搜索
  • (附源码)springboot 个人网页的网站 毕业设计031623
  • (四)Controller接口控制器详解(三)
  • (算法)Game
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • ./configure,make,make install的作用
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .NET Core使用NPOI导出复杂,美观的Excel详解
  • .net MVC中使用angularJs刷新页面数据列表
  • .net 无限分类
  • .NET/C# 使窗口永不获得焦点
  • .NetCore部署微服务(二)
  • .NET中使用Protobuffer 实现序列化和反序列化
  • []T 还是 []*T, 这是一个问题