其他分享
首页 > 其他分享> > hello flutter

hello flutter

作者:互联网

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

void main() {
/**

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.
);
}
}

标签:StatefulWidget,MyHomePageState,title,flutter,build,key,MyHomePage,hello
来源: https://www.cnblogs.com/webzom/p/16354092.html