import 'package:flutter/material.dart';
void main() {
runApp(const MyApp()); // 1.
}
class MyApp extends StatelessWidget { // 2.
const MyApp({super.key});
// This widget is the root of your application.
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: '플러터 데모 홈페이지'),
);
}
}
void main() {
runApp(MyApp());
}
runApp() 함수는 인수로 넘긴 위젯을 화면에 표시한다. 여기서는 Stateless 위젯인 MyApp 클래스를 사용한다. runApp() 함수에는 Stateful 위젯도 넣을 수 있다.
class MyHomePage extends StatefulWidget { // 3.
const MyHomePage({super.key, required this.title});
final String title;
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> { // 4.
int _counter = 0; // 5.
void _incrementCounter() { // 6.
setState(() {
_counter++;
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton( // 7.
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}