Home » Flutter项目 » Flutter 生命周期

Flutter 生命周期

编 辑:Y ┊ 时 间:2022年07月22日 ┊ 访问: 18 次
// import 'dart:ffi';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("标题"),
        ),
        body: ContentWidget(),
      ),
    );
  }
}

class ContentWidget extends StatefulWidget {
  ContentWidget() {
    print("ContentWidget 构造函数被调用");
  }
  @override
  State<StatefulWidget> createState() {
    return ContentWidgetState();
  }

}

class ContentWidgetState extends State<ContentWidget> {
  int counter = 0;
  ContentWidgetState() {
    print("ContentWidgetState 构造函数被调用");
  }

  void initState() {
    print("ContentWidgetState 的initState函数被调用");
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    print("ContentWidgetState 的didChangeDependencies函数被调用");
  }

  @override
  void didUpdateWidget(covariant ContentWidget oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    print("ContentWidgetState 的didUpdateWidget函数被调用");
  }

  @override
  Widget build(BuildContext context) {
    print("ContentWidgetState 的build函数被调用");
    return Center(
       child: Column(
         children: [
           ElevatedButton(
               onPressed: (){
                 setState(() {
                   counter++;
                 });
               },
               child:  const Text("计数+1")
           ),
           Text("Hello World: $counter", style: TextStyle(fontSize: 30)),
         ],
       ),
    );
  }
  
}

I/flutter ( 4514): ContentWidget 构造函数被调用
I/flutter ( 4514): ContentWidgetState 构造函数被调用
I/flutter ( 4514): ContentWidgetState 的initState函数被调用
I/flutter ( 4514): ContentWidgetState 的didChangeDependencies函数被调用
I/flutter ( 4514): ContentWidgetState 的build函数被调用
D/EGL_emulation( 4514): app_time_stats: avg=455.78ms min=5.20ms max=18104.68ms count=41
I/flutter ( 4514): ContentWidget 构造函数被调用
I/flutter ( 4514): ContentWidgetState 的didUpdateWidget函数被调用
I/flutter ( 4514): ContentWidgetState 的build函数被调用

如果点击事件
I/flutter ( 4514): ContentWidgetState 的build函数被调用
I/flutter ( 4514): ContentWidgetState 的build函数被调用
D/EGL_emulation( 4514): app_time_stats: avg=7074.25ms min=312.33ms max=13836.17ms count=2
D/EGL_emulation( 4514): app_time_stats: avg=855.87ms min=7.44ms max=8406.17ms count=10
I/flutter ( 4514): ContentWidgetState 的build函数被调用




Copyright © 2026 Y 版权所有.网站运行:13年238天21小时24分