您好,欢迎关注我,本篇文章是关于 Flutter 的系列文,从简单的 Flutter 介绍开始,一步步带你了解进入 Flutter 的世界。你最好有必定的移动开发经验,若是没有也不要担忧,在个人专栏底部给我留言,我会尽个人能力给你解答。
上篇文章咱们介绍了Flutter的总体架构,相信你们必定印象深入,本篇文章介绍 Flutter UI的基础构建,从主题、提示、图片加载和动画四个方向介绍。git
一.使用主题管理颜色和字体样式
使用主题能够在应用中采用统一的颜色和样式。定义主题有两种方式:内建主题或自定义Theme类。github
1.内建主题web
new MaterialApp( title: title, theme: new ThemeData( brightness: Brightness.dark, primaryColor: Colors.lightBlue[800], accentColor: Colors.cyan[600], ), );
2.自定义主题缓存
new Theme( // Create a unique theme with "new ThemeData" data: new ThemeData( accentColor: Colors.yellow, ), );
3.使用主题网络
经过以上两种方式建立主题后,咱们能够在Widget的build方法中经过Theme.of(context)函数使用主题。架构
new Container( color: Theme.of(context).accentColor, child: new Text( 'Text with a background color', style: Theme.of(context).textTheme.title, ), );
经过ThemeData文档能够查看到主题里面支持预约义的颜色app
经过TextTheme能够查看系统预制的字体样式。例如示例中提到的theme.textTheme.title就是这个样子的:less
SnackBar
在Android中有Toast弹出提示这个概念,可是在Flutter中没有Toast,取而代之的是SnackBar。ide
想要建立一个SnackBar,咱们须要用到Scaffold容器,以前文章有讲过Scaffold是一个包含Material Design的容器。函数
Scaffold( appBar: AppBar( title: Text('SnackBar Demo'), ), body: SnackBarPage(), // We'll fill this in below! );
点击按钮的时候显示SnackBar:
void _showSnackBar() { final snackBar = SnackBar( content: Text('Yay! A SnackBar!'), action: SnackBarAction( label: 'Undo', onPressed: () { // Some code to undo the change! }, ), ); Scaffold.of(context).showSnackBar(snackBar); }
二.从网络加载图片
在Flutter中直接使用Image.network就能够加载图片了
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { var title = 'Web Images'; return MaterialApp( title: title, home: Scaffold( appBar: AppBar( title: Text(title), ), body: Image.network( 'https://github.com/flutter/website/blob/master/_includes/code/layout/lakes/images/lake.jpg?raw=true', ), ), ); } }
该方法还能够直接加载GIF图片
Image.network( 'https://github.com/flutter/plugins/raw/master/packages/video_player/doc/demo_ipod.gif?raw=true', );
经过placeholder属性能够增长一个占位图:
FadeInImage.assetNetwork( placeholder: 'assets/loading.gif', image: 'https://github.com/flutter/website/blob/master/_includes/code/layout/lakes/images/lake.jpg?raw=true', );
值得注意的是用Image.network加载的图片并无缓存,若是想加载图片并缓存,须要使用:
CachedNetworkImage( placeholder: CircularProgressIndicator(), imageUrl: 'https://github.com/flutter/website/blob/master/_includes/code/layout/lakes/images/lake.jpg?raw=true', );
若是对Flutter的图片缓存策略感兴趣,请继续关注本专栏,以后的文章中我会分享给你们
三.动画
本段只简单的介绍动画入门,以后有文章会详细介绍Flutter动画。
上篇文章说到过在Flutter中全部的东西都是Widget,包括动画也不例外,若是你想让某个Widget包含动画属性,那么你须要用AnimatedOpacity将其包裹起来,AnimatedOpacity也是一个Widget。
AnimatedOpacity( // If the Widget should be visible, animate to 1.0 (fully visible). If // the Widget should be hidden, animate to 0.0 (invisible). opacity: _visible ? 1.0 : 0.0, duration: Duration(milliseconds: 500), // The green box needs to be the child of the AnimatedOpacity child: Container( width: 200.0, height: 200.0, color: Colors.green, ), );
咱们使用一个StatefulWidget来调用setState()方法刷新_visible的值,就能显示动画了,是否是很简单?
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { final appTitle = 'Opacity Demo'; return MaterialApp( title: appTitle, home: MyHomePage(title: appTitle), ); } } // The StatefulWidget's job is to take in some data and create a State class. // In this case, our Widget takes in a title, and creates a _MyHomePageState. class MyHomePage extends StatefulWidget { final String title; MyHomePage({Key key, this.title}) : super(key: key); @override _MyHomePageState createState() => _MyHomePageState(); } // The State class is responsible for two things: holding some data we can // update and building the UI using that data. class _MyHomePageState extends State<MyHomePage> { // Whether the green box should be visible or invisible bool _visible = true; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: AnimatedOpacity( // If the Widget should be visible, animate to 1.0 (fully visible). If // the Widget should be hidden, animate to 0.0 (invisible). opacity: _visible ? 1.0 : 0.0, duration: Duration(milliseconds: 500), // The green box needs to be the child of the AnimatedOpacity child: Container( width: 200.0, height: 200.0, color: Colors.green, ), ), ), floatingActionButton: FloatingActionButton( onPressed: () { // Make sure we call setState! This will tell Flutter to rebuild the // UI with our changes! setState(() { _visible = !_visible; }); }, tooltip: 'Toggle Opacity', child: Icon(Icons.flip), ), // This trailing comma makes auto-formatting nicer for build methods. ); } }
本篇文章从主题、提示、图片加载和动画四个方面简单的介绍了Flutter的UI建立过程,为了不文章太长致使可读性较差,因此只简单的讲了这四个方面,还有更多内容会在以后的文章里介绍。相信本篇文章读完以后,你已经知道如何使用Flutter Widget了,下一篇专栏来点实战,我会教你们如何实现一个轮播指示器。
想学习更多Android方面的技术或者flutter相关内容均可以加个人交流群:群号:925019412
进群领取以下免费资料学习