Flutter
flutter) 커스텀 위젯 만들 때 주의사항
탐훈
2024. 2. 10. 22:32
728x90
커스텀 위젯 만들 때 warning이 뜨는데
그것을 해결하기 위해 정리해본다
1. 생성자함수를 만들어줘야 한다
2. 메모리 할당할 때 재사용성을 위해서 const로 선언해준다
3. 커스텀 위젯이 const이기 때문에 사용할 때 제일 바깥쪽에도 const를 선언해준다
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: Scaffold(
backgroundColor: Colors.red,
body: GradientContainer(),
),
),
);
}
class GradientContainer extends StatelessWidget {
const GradientContainer({super.key});
@override
Widget build(context) {
return Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [Colors.blue, Colors.red],
begin: Alignment.topLeft,
end: Alignment.bottomRight)),
child: const Center(
child: Text(
'Hello World!',
style: TextStyle(fontSize: 28, color: Colors.white),
),
),
);
}
}