• 欢迎光临~

hello flutter

开发技术 开发技术 2022-06-08 次浏览

//导入包
import 'package:flutter/material.dart';

void main() {
/**

  • 应用入口
    */
    runApp(const MyApp());
    }

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
//应用名称
title: 'Flutter Demo',
theme: ThemeData(
//应用主题
primarySwatch: Colors.blue,
),
//首页路由
home: const MyHomePage(title: 'hello world'),
);
}
}
/*
StatefulWidget类,表示它是一个有状态的组件 可以拥有状态
Stateless widget 是不可变的
Stateful widget 至少由两个类组成 一个StatefulWidget类。
一个 State类; StatefulWidget类本身是不变的,但是State类中持有的状态在 widget 生命周期中可能会发生变化。
_MyHomePageState类是MyHomePage类对应的状态类
MyHomePage类中并没有build方法
取而代之的是,build方法被挪到了_MyHomePageState
*/

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
int _counter = 0;

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

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('You have pushed the button this many times:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

程序员灯塔
转载请注明原文链接:hello flutter
喜欢 (0)