1 Text

多行文本显示

  1. final String _title = 'Flutter高级进阶班';
  2. final String _autor = 'Hank';
  3. Text(
  4. '<${_title}> -- $_autor.本套课程将针对iOS开发者快速上手Flutter技术.本课程设计贯穿整个实战项目,通过项目需求快速学习各项技术.同时在项目实战过程中会通过带着大家探索相应的原理性知识,完成从 Flutter 入门到进阶的转变',
  5. textAlign: TextAlign.center,
  6. style: _textStyle,
  7. maxLines: 4,
  8. overflow: TextOverflow.ellipsis,
  9. );

RichText

  1. RichText(
  2. text: TextSpan(
  3. text: '<Flutter China>',
  4. style: TextStyle(
  5. fontSize: 30,
  6. color: Colors.black,
  7. ),
  8. children: <TextSpan>[
  9. TextSpan(
  10. text: '--',
  11. style: TextStyle(
  12. fontSize: 20,
  13. color: Colors.red,
  14. )),
  15. TextSpan(
  16. text: 'Auhor', style: TextStyle(fontSize: 16, color: Colors.blue)),
  17. ],
  18. ),
  19. )

Text.rich

  1. Text.rich(
  2. TextSpan(
  3. children: [
  4. TextSpan(
  5. text: "Home: "
  6. ),
  7. TextSpan(
  8. text: "https://flutterchina.club",
  9. style: TextStyle(
  10. color: Colors.blue
  11. ),
  12. recognizer: _tapRecognizer
  13. ),
  14. ]
  15. ))

DefaultTextStyle用于设置默认文本样式

  1. DefaultTextStyle(
  2. //1.设置文本默认样式
  3. style: TextStyle(
  4. color:Colors.red,
  5. fontSize: 20.0,
  6. ),
  7. textAlign: TextAlign.start,
  8. child: Column(
  9. crossAxisAlignment: CrossAxisAlignment.start,
  10. children: <Widget>[
  11. Text("hello world"),
  12. Text("I am Jack"),
  13. Text("I am Jack",
  14. style: TextStyle(
  15. inherit: false, //2.不继承默认样式
  16. color: Colors.grey
  17. ),
  18. ),
  19. ],
  20. ),
  21. );

2 Icon

  1. Icon(
  2. Icons.add,
  3. size: 45,
  4. )

自定义icons

  • 字体图标

    1. pubspec.yaml中配置导入资源<br />1) 导入字体图标文件;这一步和导入字体文件相同,假设我们的字体图标文件保存在项目根目录下,路径为"fonts/iconfont.ttf"

fonts:
- family: myIcon #指定一个字体名
fonts:
- asset: fonts/iconfont.ttf
2) 为了使用方便,我们定义一个MyIcons类,功能和Icons类一样:将字体文件中的所有图标都定义成静态变量:

  1. class MyIcons{
  2. // book 图标
  3. static const IconData book = const IconData(
  4. 0xe614,
  5. fontFamily: 'myIcon',
  6. matchTextDirection: true
  7. );
  8. // 微信图标
  9. static const IconData wechat = const IconData(
  10. 0xec7d,
  11. fontFamily: 'myIcon',
  12. matchTextDirection: true
  13. );
  14. }

3) 使用

  1. Row(
  2. mainAxisAlignment: MainAxisAlignment.center,
  3. children: <Widget>[
  4. Icon(MyIcons.book,color: Colors.purple,),
  5. Icon(MyIcons.wechat,color: Colors.green,),
  6. ],
  7. )

String icons = “”; // accessible:  or 0xE914 or E914 icons += “\uE914”; // error:  or 0xE000 or E000 icons += “ \uE000”; // fingerprint:  or 0xE90D or E90D icons += “ \uE90D”;

Text(icons, style: TextStyle( fontFamily: “MaterialIcons”, fontSize: 24.0, color: Colors.green ), );

  1. <a name="nKekH"></a>
  2. ### 3 Button
  3. <a name="eSSlN"></a>
  4. #### floatingActionButton
  5. - FloatingActionButton
  6. ```dart
  7. /**
  8. const FloatingActionButton({
  9. Key key,
  10. this.child,//按钮显示的内容
  11. this.tooltip,//长按时显示的提示
  12. this.foregroundColor,//前景色,影响到文字颜色
  13. this.backgroundColor,//背景色
  14. this.heroTag = const _DefaultHeroTag(),//hero效果使用的tag,系统默认会给所有FAB使用同一个tag,方便做动画效果
  15. this.elevation = 6.0,//未点击时阴影值
  16. this.highlightElevation = 12.0,//点击下阴影值
  17. @required this.onPressed,
  18. this.mini = false,//FloatingActionButton有regular, mini, extended三种类型,默认为false即regular类型,true时按钮变小即mini类型,extended需要通过FloatingActionButton.extended()创建,可以定制显示内容
  19. this.shape = const CircleBorder(),//定义FAB的shape,设置shape时,默认为CircleBorder,RoundedRectangleBorder圆角矩形。
  20. this.clipBehavior = Clip.none,
  21. this.materialTapTargetSize,
  22. this.isExtended = false,//是否为”extended”类型
  23. })
  24. */
  25. //FloatingActionButton 构造函数方式创建
  26. floatingActionButton: FloatingActionButton(
  27. onPressed: () => print("FloatingActionButton"),
  28. child: Text("button"),
  29. tooltip: "按这么长时间干嘛",
  30. foregroundColor: Colors.red,
  31. // backgroundColor: Colors.red,
  32. // mini: true,
  33. // shape: CircleBorder()
  34. ),
  • FloatingActionButton.extended

    1. //FloatingActionButton extended方式创建
    2. floatingActionButton: FloatingActionButton.extended(
    3. icon: Icon(Icons.alarm), label: Text("文本"), onPressed: () {},
    4. shape: RoundedRectangleBorder(
    5. borderRadius: BorderRadius.all(Radius.circular(5))),
    6. ),
  • 设置FloatingActionButton位置 ```dart

//centerDocked 底部中间 //endDocked 底部右侧 //centerFloat 中间偏上 //endFloat 底部偏上 //startTop 左侧顶部 //endTop 右侧顶部 home: Scaffold( appBar: AppBar( title: Text(“FloatingActionButton”), ), body: Container(), floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, floatingActionButton: FloatingActionButton( onPressed: () => print(“FloatingActionButton”), child: Text(“button”), ), ),

  1. <a name="mtEfV"></a>
  2. #### FlatButton
  3. 扁平化的按钮,继承自MaterialButton
  4. ```dart
  5. /*
  6. const FlatButton({
  7. ...
  8. @required this.onPressed, //按钮点击回调
  9. this.textColor, //按钮文字颜色
  10. this.disabledTextColor, //按钮禁用时的文字颜色
  11. this.color, //按钮背景颜色
  12. this.disabledColor,//按钮禁用时的背景颜色
  13. this.highlightColor, //按钮按下时的背景颜色
  14. this.splashColor, //点击时,水波动画中水波的颜色
  15. this.colorBrightness,//按钮主题,默认是浅色主题
  16. this.padding, //按钮的填充
  17. this.shape, //外形
  18. @required this.child, //按钮的内容
  19. });
  20. */
  21. final Widget FlatButtonDemo = Row(
  22. mainAxisAlignment: MainAxisAlignment.center,
  23. children: <Widget>[
  24. FlatButton(
  25. child: Text('Button'),
  26. onPressed: () {},
  27. splashColor: Colors.grey,
  28. textColor: Theme.of(context).accentColor,
  29. ),
  30. FlatButton.icon(
  31. label: Text('Button'),
  32. icon: Icon(Icons.add),
  33. onPressed: () {},
  34. splashColor: Colors.grey,
  35. textColor: Theme.of(context).accentColor,
  36. )
  37. ],
  38. );
  39. FlatButton(
  40. onPressed:(){},
  41. child: Text("扁平按钮"),
  42. color: Colors.blue,
  43. textColor: Colors.black,
  44. shape: RoundedRectangleBorder(
  45. side: BorderSide(
  46. color: Colors.white,
  47. width: 1,
  48. ),
  49. borderRadius: BorderRadius.circular(8)),
  50. );

RaisedButton

凸起的按钮,其实就是Android中的Material Design风格的Button ,继承自MaterialButton

  1. const RaisedButton({
  2. Key key,
  3. @required VoidCallback onPressed,
  4. ValueChanged<bool> onHighlightChanged,
  5. ButtonTextTheme textTheme,
  6. Color textColor,
  7. Color disabledTextColor,
  8. Color color,
  9. Color disabledColor,
  10. Color highlightColor,
  11. Color splashColor,
  12. Brightness colorBrightness,
  13. double elevation,
  14. double highlightElevation,
  15. double disabledElevation,
  16. EdgeInsetsGeometry padding,
  17. ShapeBorder shape,
  18. Clip clipBehavior = Clip.none,
  19. MaterialTapTargetSize materialTapTargetSize,
  20. Duration animationDuration,
  21. Widget child,
  22. })

OutlineButton

带边框的按钮,继承自MaterialButton

  1. final Widget OutlineButtonDemo = Row(
  2. mainAxisAlignment: MainAxisAlignment.center,
  3. children: <Widget>[
  4. Theme(
  5. data: Theme.of(context).copyWith(
  6. buttonColor: Theme.of(context).accentColor,
  7. buttonTheme: ButtonThemeData(
  8. textTheme: ButtonTextTheme.primary,
  9. // shape: BeveledRectangleBorder(
  10. // borderRadius: BorderRadius.circular(5.0)
  11. // ),
  12. shape: StadiumBorder(),
  13. )),
  14. child: OutlineButton(
  15. child: Text('Button'),
  16. onPressed: () {},
  17. splashColor: Colors.grey[100],
  18. borderSide: BorderSide(
  19. color: Colors.black,//边框颜色
  20. ),
  21. textColor: Colors.black,
  22. highlightedBorderColor: Colors.grey,
  23. // textTheme: ButtonTextTheme.primary,
  24. ),
  25. ),
  26. SizedBox(width: 10.0),
  27. OutlineButton.icon(
  28. label: Text('Button'),
  29. icon: Icon(Icons.add),
  30. onPressed: () {},
  31. splashColor: Colors.grey,
  32. textColor: Theme.of(context).accentColor,
  33. )
  34. ],
  35. );

bottomNavigationBar

  1. return Scaffold(
  2. appBar: AppBar(
  3. title: Text('FloatingActionButtonDemo'),
  4. elevation: 0.0,
  5. ),
  6. floatingActionButton: _floatingActionButton,
  7. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,//floatingActionButton的位置
  8. bottomNavigationBar: BottomAppBar(
  9. child: Container(
  10. height: 80.0,
  11. ),
  12. shape: CircularNotchedRectangle(),
  13. ),
  14. );
  15. }
  16. }

IconButton

  1. IconButton(
  2. icon: Icon(Icons.home),
  3. onPressed: _log,
  4. color: Colors.blueAccent,
  5. highlightColor: Colors.red,
  6. );
  • RaisedButton.icon
  • OutlineButton.icon
  • FlatButton.icon

FixedWidthButton

  1. final Widget FixedWidthButton = Row(
  2. mainAxisAlignment: MainAxisAlignment.center,
  3. children: <Widget>[
  4. Container(
  5. width: 130.0,
  6. child: OutlineButton(
  7. child: Text('Button'),
  8. onPressed: () {},
  9. splashColor: Colors.grey[100],
  10. borderSide: BorderSide(
  11. color: Colors.black, //边框颜色
  12. ),
  13. textColor: Colors.black,
  14. highlightedBorderColor: Colors.grey,
  15. // textTheme: ButtonTextTheme.primary,
  16. ),
  17. ),
  18. ],
  19. );

ExpandedButton

  1. final Widget ExpandedButton = Row(
  2. mainAxisAlignment: MainAxisAlignment.center,
  3. children: <Widget>[
  4. Expanded(
  5. child: OutlineButton(
  6. child: Text('Button'),
  7. onPressed: () {},
  8. splashColor: Colors.grey[100],
  9. borderSide: BorderSide(
  10. color: Colors.black, //边框颜色
  11. ),
  12. textColor: Colors.black,
  13. highlightedBorderColor: Colors.grey,
  14. // textTheme: ButtonTextTheme.primary,
  15. ),
  16. ),
  17. SizedBox(width: 16.0),
  18. Expanded(
  19. flex: 2, //等分占的比例,默认是1
  20. child: OutlineButton(
  21. child: Text('Button'),
  22. onPressed: () {},
  23. splashColor: Colors.grey[100],
  24. borderSide: BorderSide(
  25. color: Colors.black, //边框颜色
  26. ),
  27. textColor: Colors.black,
  28. highlightedBorderColor: Colors.grey,
  29. // textTheme: ButtonTextTheme.primary,
  30. ),
  31. ),
  32. ],
  33. );

ButtonBar

  1. final Widget ButtonBarDemo = Row(
  2. mainAxisAlignment: MainAxisAlignment.center,
  3. children: <Widget>[
  4. Theme(
  5. data: Theme.of(context).copyWith(
  6. buttonTheme: ButtonThemeData(
  7. padding: EdgeInsets.symmetric(horizontal: 32.0),
  8. )),
  9. child: ButtonBar(
  10. children: <Widget>[
  11. OutlineButton(
  12. child: Text('Button'),
  13. onPressed: () {},
  14. splashColor: Colors.grey[100],
  15. borderSide: BorderSide(
  16. color: Colors.black, //边框颜色
  17. ),
  18. textColor: Colors.black,
  19. highlightedBorderColor: Colors.grey,
  20. // textTheme: ButtonTextTheme.primary,
  21. ),
  22. OutlineButton(
  23. child: Text('Button'),
  24. onPressed: () {},
  25. splashColor: Colors.grey[100],
  26. borderSide: BorderSide(
  27. color: Colors.black, //边框颜色
  28. ),
  29. textColor: Colors.black,
  30. highlightedBorderColor: Colors.grey,
  31. // textTheme: ButtonTextTheme.primary,
  32. ),
  33. ],
  34. ),
  35. );

PopupMenu

  1. import 'package:flutter/material.dart';
  2. class PopupMemuDemo extends StatefulWidget {
  3. @override
  4. _PopupMemuDemoState createState() => _PopupMemuDemoState();
  5. }
  6. class _PopupMemuDemoState extends State<PopupMemuDemo> {
  7. String _currentMemuItem = 'Home';
  8. @override
  9. Widget build(BuildContext context) {
  10. return Scaffold(
  11. appBar: AppBar(
  12. title: Text('PopupMenuButtonDemo'),
  13. elevation: 0.0,
  14. ),
  15. body: Container(
  16. padding: EdgeInsets.all(16.0),
  17. child: Column(
  18. mainAxisAlignment: MainAxisAlignment.center,
  19. children: <Widget>[
  20. Row(
  21. mainAxisAlignment: MainAxisAlignment.center,
  22. children: <Widget>[
  23. Text(_currentMemuItem),
  24. PopupMenuButton(
  25. onSelected: (value) {
  26. print(value);
  27. setState(() {
  28. _currentMemuItem = value;
  29. });
  30. },
  31. itemBuilder: (BuildContext context) => [
  32. PopupMenuItem(
  33. value: 'Home',
  34. child: Text('Home'),
  35. ),
  36. PopupMenuItem(
  37. value: 'Discover',
  38. child: Text('Discover'),
  39. ),
  40. PopupMenuItem(
  41. value: 'Community',
  42. child: Text('Community'),
  43. ),
  44. ],
  45. )
  46. ],
  47. ),
  48. ],
  49. ),
  50. ),
  51. );
  52. }
  53. }

shapeshape用来设置按钮的形状

其接收值是ShapeBorder类型,ShapeBorder是一个抽象类

  • BeveledRectangleBorder带斜角的长方形边框 ```dart //带斜角的长方形边框

shape: BeveledRectangleBorder( side: BorderSide( color: Colors.white, ), borderRadius: BorderRadius.all(Radius.circular(10)) ),

  1. - **CircleBorder**圆形边框
  2. ```dart
  3. shape: CircleBorder(
  4. side: BorderSide(
  5. color: Colors.white,
  6. ),
  7. ),
  • RoundedRectangleBorder圆角矩形

    1. borderRadius: BorderRadius.all(Radius.circular(10)),),
  • StadiumBorder两端是半圆的边框

    1. shape: StadiumBorder(),

    禁用水波纹效果

    ```dart class MyApp extends State<MyAppState> { return MaterialApp( theme: Theme.of(context).copyWith(

    1. highlightColor: Colors.transparent,
    2. splashFactory: const NoSplashFactory(),

    ), ///………./// ); } import ‘package:flutter/material.dart’;

class NoSplashFactory extends InteractiveInkFeatureFactory { const NoSplashFactory();

  1. InteractiveInkFeature create({
  2. @required MaterialInkController controller,
  3. @required RenderBox referenceBox,
  4. @required Offset position,
  5. @required Color color,
  6. TextDirection textDirection,
  7. bool containedInkWell: false,
  8. RectCallback rectCallback,
  9. BorderRadius borderRadius,
  10. ShapeBorder customBorder,
  11. double radius,
  12. VoidCallback onRemoved,
  13. }) {
  14. return new NoSplash(
  15. controller: controller,
  16. referenceBox: referenceBox,
  17. color: color,
  18. onRemoved: onRemoved,
  19. );
  20. }

}

class NoSplash extends InteractiveInkFeature { NoSplash({ @required MaterialInkController controller, @required RenderBox referenceBox, Color color, VoidCallback onRemoved, }) : assert(controller != null), assert(referenceBox != null), super(controller: controller, referenceBox: referenceBox, onRemoved: onRemoved) { controller.addInkFeature(this); } @override void paintFeature(Canvas canvas, Matrix4 transform) { } }

  1. > 如果需要局部启用,可以应用InkSplash.splashFactorysplashFactory上。
  2. <a name="TV5CK"></a>
  3. ### 4 Image
  4. Image支持以下类型的图片:JPEG,PNG,GIF,Animated GIF,WebP,Animated WebP,BMPWBMP
  5. <a name="EQ9vh"></a>
  6. #### 本地图片
  7. - Image:用于从ImageProvider获取图像
  8. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1994311/1621239533239-3dde2376-e6de-4520-baa3-57eaadce13ee.png#clientId=u93932f28-9386-4&from=paste&height=343&id=u9d23a184&margin=%5Bobject%20Object%5D&name=image.png&originHeight=686&originWidth=968&originalType=binary&size=56170&status=done&style=none&taskId=u32f1b164-3d44-4ff7-aa4c-22d1bbe55c0&width=484)
  9. - mage.asset : 使用keyAssetBundle获得的图片
  10. ```dart
  11. Image.asset(my_icon.png,
  12. width:26,
  13. height:26
  14. )
  • Image.file: 从本地文件中获取图片

    1) 加载完整路径的本地图片
    2) 使用path_provider插件,加载相对路径的本地图片

  • Image.memory 用于从Uint8List获取图片

  • 借助FadeInImage设置占位图placeholder

    1) 需要安装插件transparent_image: ^1.0.0
    2) import ‘package:transparent_image/transparent_image.dart’;
    image.png
    image.png

    网络图片

  • Image.nework 从网络URL中获取图片

    1. Image.network(
    2. "https://avatars2.githubusercontent.com/u/20411648?s=460&v=4",
    3. width: 100.0,
    4. )
  • NetworkImage

    1. Image(
    2. image: NetworkImage(
    3. "https://avatars2.githubusercontent.com/u/20411648?s=460&v=4"),
    4. width: 100.0,
    5. )
  • 缓存图片( 安装cached_network_image插件)

image.png

如何加载本地图片(SD卡里的图片)

  • 加载完整路径的本地图片
    1. import 'dart.io'
    2. Image.file(File('sdcard/Download/stack.png'));
  • 加载相对路径的本地图片(在pubspec.yaml中添加path_provider插件)

image.png