전
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(
home: Scaffold(
appBar: AppBar(),
body: SizedBox(
child: Text('ㅎ2'), //SizeBox가 길어질 가능성이 있음.
),
),
);
}
}
후
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: MyWidget(); // 3. 작명한대로 걍 같다쓰기만 하면 된다.
),
);
}
}
class MyWidget extends StatelessWidget { // 1. 클래스만들어서 작명
const MyWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox( // 2. 리턴부분에 위에있던 코드 그대로 복붙 하면 됨
child: Text('ㅎ2')
);
}
}
var a = SizedBox(child: Text('ㅎ2')); // 변수로 사용해도 된다.
1. 클래스 만들어서 작명하기
2. 긴코드 return 부분에 쓸 코드 복붙해서 넣기
3. 클래스명 불러오기
끗ㅋ
++
아무거나 커스텀위젯화 하지는 말자!
재사용이 많은 UI 활용을 위해,
큰 코드페이지의 사용을 위해
유용하게 사용하자.
변수로 담아서 사용해도 상관은 없지만,
안의 데이터가 바뀔일이 없는 UI로 사용해야 됨.
'백엔드개발자 > FLUTTER, DART' 카테고리의 다른 글
[Flutter] type 'MyApp' is not a subtype of type 'StatelessWidget' in type cast (0) | 2023.01.17 |
---|---|
[Flutter] 재랜더링, state, setState() 사용해보기 (0) | 2023.01.17 |
[Flutter] 쪽지 화면을 만들어 보았다. (0) | 2023.01.06 |
[Flutter] debug 없애기 (0) | 2023.01.01 |
[Flutter] 글자 스타일, 아이콘 스타일, 버튼스타일, 앱바 꾸미기 (0) | 2023.01.01 |