Flutter初学笔记1

新建项目后

1.需要修改android目录下的,gradle版本  build gradle版本

2.需要修改升级后的Flutter版本代码

------------------------------------------------------>flutter 生命周期

------------------------------------------------------>flutter   fragment

------------------------------------------------------>flutter 状态改变  代码来源::https://flutter.cn/docs/development/ui/widgets-intro

import 'package:flutter/material.dart';

class CounterDisplay extends StatelessWidget {
CounterDisplay({this.count});

final int count;

@override
Widget build(BuildContext context) {
print("------> $count hashCode= ${this.hashCode} runtimeType= ${this.runtimeType}");
return Text("Count:$count");
}
}

class CounterIncermentor extends StatelessWidget {
CounterIncermentor({this.onPressed});

final VoidCallback onPressed;

@override
Widget build(BuildContext context) {
print("------> onPressed $onPressed hashCode= ${this.hashCode} runtimeType= ${this.runtimeType}");
return RaisedButton(
onPressed: this.onPressed,
);
}
}

class CounterDemo extends StatefulWidget {

@override
// State<StatefulWidget> createState() => _CounterState();
State<StatefulWidget> createState() {
print("------> 111 CounterDemo hashCode= ${this.hashCode} runtimeType= ${this.runtimeType}");
return _CounterState();
}
}

class _CounterState extends State<CounterDemo> {
int _counter = 0;

void _incerment() {
setState(() {
_counter++;
});
}

// 初始化一次 完成只需要发生一次的工作 https://flutter.cn/docs/development/ui/widgets-intro
@override
void initState() {
super.initState();
}

@override
void didUpdateWidget(CounterDemo oldWidget) {
print("------> 333 oldWidge= $context hashCode= ${oldWidget.hashCode} runtimeType= ${oldWidget.runtimeType}");
super.didUpdateWidget(oldWidget);
}

@override
Widget build(BuildContext context) {
print("------> 222 build $context hashCode= ${this.hashCode} runtimeType= ${this.runtimeType}");
return Row(
children: [
CounterIncermentor(
onPressed: _incerment,
),
CounterDisplay(
count: _counter,
)
],
);
}

//
@override
void dispose() {
// TODO: implement dispose
super.dispose();
}
}

  运行到手机上打印

I/flutter (30783): ------> 111 CounterDemo                           hashCode= 124851466 runtimeType= CounterDemo
I/flutter (30783): ------> 222 build CounterDemo(dirty, state: _CounterState#dbd8d)          hashCode= 1018019213 runtimeType= _CounterState
I/flutter (30783): ------> onPressed Closure: () => void from Function '_incerment@297498619':.    hashCode= 543933139 runtimeType= CounterIncermentor
I/flutter (30783): ------> 0                                    hashCode= 528427048 runtimeType= CounterDisplay
I/flutter (30783): ------> 333 oldWidge= CounterDemo(dirty, state: _CounterState#dbd8d)       hashCode= 124851466 runtimeType= CounterDemo
I/flutter (30783): ------> 222 build CounterDemo(dirty, state: _CounterState#dbd8d)          hashCode= 1018019213 runtimeType= _CounterState
I/flutter (30783): ------> onPressed Closure: () => void from Function '_incerment@297498619':.    hashCode= 241310281 runtimeType= CounterIncermentor
I/flutter (30783): ------> 0                                     hashCode= 420274008 runtimeType= CounterDisplay


V/AudioManager(30783): playSoundEffect effectType: 0    ------------->这里是点击了一次按钮后的打印
V/AudioManager(30783): querySoundEffectsEnabled...
I/flutter (30783): ------> 222 build CounterDemo(dirty, state: _CounterState#dbd8d)          hashCode= 1018019213 runtimeType= _CounterState
I/flutter (30783): ------> onPressed Closure: () => void from Function '_incerment@297498619':.    hashCode= 748014874 runtimeType= CounterIncermentor
I/flutter (30783): ------> 1                                  hashCode= 763180643 runtimeType= CounterDisplay

可以看到 

  1. Statefull,Stateless,build方法 在点击后都走了一次,(相当于重新生成了,但官方文档介绍widget的渲染方式为新旧对比,查找差异部分后,差异部分重新渲染,依据来处:https://flutter.cn/docs/development/ui/widgets-intro  当 widget 的状态改变时,它会重新构建其描述(展示的 UI),框架则会对比前后变化的不同,以确定底层渲染树从一个状态转换到下一个状态所需的最小更改)
  2. 222的  _CounterState 还是原来那个,没有重建
  3. onPressed function方法体还是原来那个,没有重建   onPressed 在 _CounterState 中

原文地址:https://www.cnblogs.com/caosq/p/13680577.html