赞
踩
我们先编写一个简单的demo:
- import 'package:flutter/material.dart';
-
- void main() => runApp(Center(
- child: Text(
- 'Hello, world!',
- textDirection: TextDirection.ltr,
- ),
- ));
截图:
我们来看一个更加优雅的demo:
编写我们的main函数:
- void main() {
- runApp(MaterialApp(
- home: MyScaffold(),
- ));
- }
Dart语言本身相关:
a. 如何创建对象?像Java一样通过new关键字;像Kotlin一样,省略new关键字。这两种方式都ok
b. 关于可选的有名参数(optional named parameter),同样可以参考系列四
编写MyScaffold类:
- class MyScaffold extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return Material(
- child: Column(
- children: <Widget>[
- MyAppBar(
- title: Text(
- 'Example title',
- style: Theme.of(context).primaryTextTheme.title,
- ),
- ),
- Expanded(
- child: Center(
- child: Text('我是content,我在中间'),
- ),
- ),
- ],
- ),
- );
- }
- }
-
- class MyAppBar extends StatelessWidget {
- final Widget title;
-
- MyAppBar({this.title});
-
- @override
- Widget build(BuildContext context) {
- return Container(
- height: 56.0,
- padding: const EdgeInsets.symmetric(horizontal: 8.0),
- decoration: BoxDecoration(color: Colors.blue[500]),
- child: Row(
- children: <Widget>[
- IconButton(
- icon: Icon(Icons.menu),
- onPressed: null,
- ),
- Expanded(
- child: title,
- ),
- IconButton(icon: Icon(Icons.search), onPressed: null)
- ],
- ),
- );
- }
- }

从MyScaffold中看起:
Expanded:这个对象可以类比LinearLayout中的weight属性的作用,那么如何设置权重值呢?
- class Expanded extends Flexible {
- const Expanded({
- Key key,
- int flex = 1,
- @required Widget child,
- }) : super(key: key, flex: flex, fit: FlexFit.tight, child: child);
- }
通过flex来设置,默认是1
MyAppBarLayout中:
最终的运行效果为:
注意pubspec.yaml文件中
- flutter:
- uses-material-design: true
这个文件就相当于我们build.gradle文件一样,这里可以指定依赖、版本号等
在demo2中,MyScaffold、MyAppBar,这些都是我们自定义的Widget,实际上这些都有对应的Material组件已经定义好了。请看demo3:
- void main() {
- runApp(MaterialApp(
- home: MaterialHome(),
- ));
- }
-
- class MaterialHome extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- leading: IconButton(icon: Icon(Icons.menu), onPressed: null),
- title: Text("I'm title"),
- actions: <Widget>[
- IconButton(
- icon: Icon(Icons.search),
- onPressed: null,
- )
- ],
- ),
- body: Center(
- child: Text("I'm content and in center"),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: null,
- child: Icon(Icons.add),
- ),
- );
- }
- }

下面我想实现对按钮点击的响应:
- class MaterialHome extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- leading: IconButton(icon: Icon(Icons.menu), onPressed: null),
- title: Text("I'm title"),
- actions: <Widget>[
- IconButton(
- icon: Icon(Icons.search),
- onPressed: onSearchClick,
- )
- ],
- ),
- body: Center(
- child: Text("I'm content and in center"),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: onFabClick,
- child: Icon(Icons.add),
- ),
- );
- }
-
- void onSearchClick() {
- // 可以使用print函数输出日志
- print("search button clicked");
- }
-
- void onFabClick() {
- print("fab clicked");
- }
- }

注意两个onPressed属性的赋值:直接将我们定义好的函数名传递过去。在Function系列说过,在Dart语言中,所有的函数都是一个对象,它的类型为Function。点击按钮,我们就可以在控制台看到日志输出了,证明我们事件处理函数得到调用了。
参考文献:《Flutter Tutorial》
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。