๐STUDY/๐ซDart&Flutter
[flutter] StatelessWidget / StatefulWidget ์ค์ต
ํด๋์
2021. 2. 6. 19:24
StatelessWidget ์ค์ต
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Scaffold(
appBar: AppBar(
title:Text('Hello World'),
),
body: Text(
'Hello World',
style: TextStyle(fontSize: 40),
),
)
);
}
}
stless์ ๊ฒฝ์ฐ home์ ๋ฐ๋ก ์์ฑ์ด ๊ฐ๋ฅํ์ง๋ง, ๋ณดํต ๊ฐ๋ ์ฑ์ ์ํด ๋ณ๋์ ํด๋์ค๋ก ๋ถ๋ฆฌํจ.
//๋ณ๋์ ํด๋์ค๋ก ๋ถ๋ฆฌํ ๊ฒฝ์ฐ. ์๋์ ์์ ๋์ผ
import 'package:flutter/material.dart';
//์ฑ ์์ ๋ถ๋ถ
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Text('Hello World'),
),
body: Text(
'Hello World',
style: TextStyle(fontSize: 40),
),
);
}
}
StatefulWidget ์ค์ต
๋ฒํผ์ ๋๋ฅด๋ฉด ๊ธ์๊ฐ ๋ณ๊ฒฝ๋๋ ์์ .
import 'package:flutter/material.dart';
//์ฑ ์์ ๋ถ๋ถ
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var _text = "Wow";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hello world'),
),
body: Text(
_text,
style: TextStyle(fontSize: 40),
),
floatingActionButton: FloatingActionButton(
onPressed: (){
setState(() {
_text = "hello!";
});
},
child: Icon(Icons.wb_sunny),
),
);
}
}