createState() => _TopState();
}
class _TopState extends Stateimport 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
print("HomePage build");
return Scaffold(
body: SafeArea(
child: Column(
children: [
Expanded(child: Top()),
Expanded(child: Bottom()),
],
),
),
);
}
}
class Top extends StatefulWidget {
const Top({super.key});
@override
State<Top> createState() => _TopState();
}
class _TopState extends State<Top> {
// 1. 상태
int num = 1;
// 2. 행위
void increase(){
num++;
setState(() {});
}
@override
Widget build(BuildContext context) {
print("Top build");
return Column(
children: [
Top1(num),
Top2(increase),
],
);
}
}
class Top1 extends StatelessWidget {
final num;
Top1(this.num);
@override
Widget build(BuildContext context) {
print("Top 1 build");
return Expanded(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Top 1", style: TextStyle(fontSize: 40, fontWeight: FontWeight.bold)),
Text("num : $num", style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold))
],
),
),
);
}
}
class Top2 extends StatelessWidget {
final increase;
Top2(this.increase);
@override
Widget build(BuildContext context) {
print("Top 2 build");
return Expanded(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Top 2", style: TextStyle(fontSize: 40, fontWeight: FontWeight.bold)),
ElevatedButton(onPressed: (){
increase();
}, child: Icon(Icons.add))
],
),
),
);
}
}
class Bottom extends StatelessWidget {
const Bottom({
super.key,
});
@override
Widget build(BuildContext context) {
print("Bottom build");
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Bottom", style: TextStyle(fontSize: 40, fontWeight: FontWeight.bold)),
],
),
);
}
}