쉽게 말해 현재 코드의 동작은 그대로 유지하면서 더 이해하기 쉽고, 생각하기 쉽고, 확장하기 쉽게끔 재구성하는 것.

Widget build(BuildContext context) {
return ButtonTheme(
//버튼 모서리
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(4.0),
),
),
child: ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: Colors.white),
onPressed: () {},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
//이미지
Image.asset('assets/glogo.png'),
//텍스트
const Text(
'Login with Google',
style: TextStyle(color: Colors.black87, fontSize: 15.0),
),
//이미지
Opacity(
opacity: 0.0,
child: Image.asset('assets/glogo.png'),
),
],
),
),
);
위의 코드에서 버튼을 구현하려 했을 때 이미지, 텍스트, 클릭 이벤트 등등 직접 하나하나 작성하였는데 코드 리펙토링을 진행하면
const MyButton(
{super.key,
required this.image,
required this.text,
required this.color,
required this.radius,
required this.onPressed});
final Widget image;
final Widget text;
final Color color;
final double radius;
final VoidCallback onPressed;
Widget build(BuildContext context) {
return ButtonTheme(
height: 50.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(radius),
),
),
child: ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: color),
onPressed: onPressed,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
image,
text,
Opacity(
opacity: 0.0,
child: image,
),
],
),
),
);
이와 같이 리펙토링 코드만 만들어서 본 코드에서는
MyButton(
image: Image.asset('assets/glogo.png'),
text: const Text(
'Login with Google',
style: TextStyle(color: Colors.black87, fontSize: 15.0),
),
color: Colors.white,
radius: 4.0,
onPressed: () {}),
필수 값만 채우면 자동으로 버튼이 생성된다.