一、关于组件
Flutter把内容单独抽离成一个组件
在flutter中自定义组件其实就是一个类,这个类需要继承StatelessWidget/StatefulWidget前期我们都继承StatelessWidget是无状态的,状态不可变得widget。
StatefulWidget是有状态组件,持有的状态可能在widget生命周期改变。
二、MaterialApp和Scaffold两个组件装饰App
2-1 MaterialApp
MaterialApp是一个方便的Widget,它封装了应用程序实现Material Design所需的一些Widget。一般作为顶层的widget使用。
# 常用属性home 主页title 标题theme 主题routes 路由
2-2 Scaffold(脚手架)
是Material Design布局结构的基本实现。此类提供了用于显示drawer,snackbar和底部sheet的API
appBar 显示在界面顶部的一个AppBarbody 当前界面所显示的主要内容Widget(小装置)drawer 抽屉菜单控件
import 'package:flutter/material.dart';void main() {runApp(MyApp());}// 自定义组件class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {// TODO: implement buildreturn MaterialApp(home: Scaffold(appBar: AppBar(title: Text('Flutter Demo')), body: HomeContent()));}}class HomeContent extends StatelessWidget {@overrideWidget build(BuildContext context) {// TODO: implement buildreturn Center(child: Text("你好",textDirection: TextDirection.ltr,style: TextStyle(fontSize: 40.0, color: Colors.yellow)));}}
三、Container组件
- 相当于div
Container(width: 100.0,height: 100.0,decoration: BoxDecoration(gradient: RadialGradient(//背景径向渐变colors: [Colors.red, Colors.yellow],center: Alignment.topLeft,radius: .98),),transform: Matrix4.rotationZ(.2),//卡片倾斜变换margin: EdgeInsets.only(top: 50.0, left: 120.0),),
四、Image
4-1 网络图片
Image.network("http://file02.16sucai.com/d/file/2014/0829/372edfeb74c3119b666237bd4af92be5.jpg",width: 50,height: 50,)
4-2 本地图片
- 根目录下创建images文件夹
- 在pubspec.yaml中的flutter部分添加如下内容:

- 加载图片
Image(image: AssetImage("images/avatar.png"),width: 100.0);
五、ListView
- 包裹在MaterialApp组件里
class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {// TODO: implement buildreturn MaterialApp(home: Scaffold(appBar: AppBar(title:Text("flutter"),),body: HomeContent()),);}}
class HomeContent extends StatelessWidget{@overrideWidget build(BuildContext context) {// TODO: implement buildreturn ListView(children: <Widget>[ListTile(leading: Icon(Icons.music_video,color: Color(0xffC20C0C),size: 30,),title: Text("网易云"),subtitle: Text("网易云音乐真的牛皮"),),ListTile(trailing: Icon(Icons.music_video,color: Color(0xffC20C0C),size: 30,),//trailing 放在后面的title: Text("网易云"),subtitle: Text("网易云音乐真的牛皮"),),ListTile(leading: Icon(Icons.music_video,color: Color(0xffC20C0C),size: 30,),title: Text("网易云"),subtitle: Text("网易云音乐真的牛皮"),)],);}}
六、Text
return Text("hello world",textDirection: TextDirection.ltr,style: TextStyle(fontSize: 40,color: Colors.red),);
