Flutter入门02 -- Flutter开始

2021-10-17  本文已影响0人  YanZi_33

Flutter项目的创建

Hot Reload 与 Hot Restart

Widget

StatelessWidget

main函数自定义入口Widget

import 'package:flutter/material.dart';

main(){
  //1.runApp函数 在flutter/material.dart系统库中
  runApp(
    Center(
      child: Text(
        "Hello World!!!",
        textDirection: TextDirection.ltr,
        style: TextStyle(
            fontSize: 30,
            color: Colors.orange
        ),
      ),
    )
  );
}

MaterialApp

import 'package:flutter/material.dart';

main(){
  //1.runApp函数 在flutter/material.dart系统库中
  runApp(
    MaterialApp(
      home: Center(
        child: Text(
          "Hello World!!!",
          textDirection: TextDirection.ltr,
          style: TextStyle(
              fontSize: 30,
              color: Colors.orange
          ),
        ),
      ),
    )
  );
}
import 'package:flutter/material.dart';

main(){
  //1.runApp函数 在flutter/material.dart系统库中
  runApp(
    MaterialApp(
      //去除右上角的debug标签
      debugShowCheckedModeBanner: true,
      home: Scaffold(
        appBar: AppBar(
          title: Text("第一个Flutter程序"),
        ),
        body: Center(
          child: Text(
            "Hello World!!!",
            textDirection: TextDirection.ltr,
            style: TextStyle(
                fontSize: 30,
                color: Colors.orange
            ),
          ),
        ),
      )
    )
  );
}

代码重构

import 'package:flutter/material.dart';

main() => runApp(SFMyApp());

///组件化开发
class SFMyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      //去除右上角的debug标签
        debugShowCheckedModeBanner: false,
        home: SFHomePage()
    );
  }
}

class SFHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("第一个Flutter程序"),
      ),
      body: SFContentBody()
    );
  }
}

class SFContentBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Center(
      child: Text(
        "Hello World!!!",
        textDirection: TextDirection.ltr,
        style: TextStyle(
            fontSize: 30,
            color: Colors.red
        ),
      ),
    );
  }
}

项目案例

import 'package:flutter/material.dart';

main() => runApp(SFMyApp());

///组件化开发
class SFMyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      //去除右上角的debug标签
        debugShowCheckedModeBanner: false,
        home: SFHomePage()
    );
  }
}

class SFHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("第一个Flutter程序"),
      ),
      body: SFContentBody()
    );
  }
}


///可以创建状态管理类 来管理状态
class SFContentBody extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
     return SFContentBodyState();
  }
}

///这里可以定义状态
class SFContentBodyState extends State<SFContentBody> {
  //用来记录Checkbox的选中状态
  var flag = true;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Checkbox(
            value: flag,
            onChanged: (value){
              setState(() {
                flag = value;
              });
            },
          ),
          Text(
            "同一协议",
            style: TextStyle(
              fontSize: 20
            )
          )
        ],
      ),
    );
  }
}
image.png
上一篇下一篇

猜你喜欢

热点阅读