본문 바로가기
스파르타/Flutter

[Flutter] 클론코딩(Shazam) 1.두번째 페이지 - ( week 2 )

by bakcoding_sparta 2023. 4. 18.

지난번 과제를 할 때 처음부터 만드려니 기능적인 부분 구현에 있어서 오래 걸리는 것 같아 베이스코드 위에서 다시 작업한다.

 

1. 두번째 페이지

// 두번째 페이지
class SecondTab extends StatelessWidget {
  const SecondTab({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(child: Text('두번째 페이지'));
  }
}

두 번째 페이지의 배경색은 그러데이션이 들어가 있는데 그 부분부터 만들어준다.

 

return Container(
      child: SafeArea(
        child: Text("Second Page"),
      ),
    );

SafeArea는 기기의 홈버튼이나 아이폰의 경우 노치를 피해서 위치를 잡아준다.

 

return Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: [Colors.blue[300]!, Colors.blue[900]!],
        ),
      ),
      child: SafeArea(
        child: Text("Second Page"),
      ),
    );

BoxDecoration으로 그래디언트를 넣어준다. 색의 경우 배열로 시작색 ~ 끝색으로 넣어줄 수 있다. 여기서 색값이 null임을 방지하기 위해서! 를 붙여준다.

 

상단 아이콘

상단의 아이콘을 넣어주는데 이 아이콘은 앱바에서 생성되는게 아니고 SafeArea를 통해서 만든 공간에서 만드는 것이다. 

child: SafeArea(
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                IconButton(
                  onPressed: () {},
                  icon: Icon(Icons.person),
                ),
                Spacer(),
                IconButton(
                  onPressed: () {},
                  icon: Icon(Icons.show_chart),
                ),
              ],
            ),
          ],
        ),
      ),

아이콘의 정렬은 mainAxisAlignment를 spaceBetween으로 해도 되고 아이콘 사이에 Spacer를 넣어주어도 동일하게 동작한다.  IconButton의 경우 아이콘에 기본 패딩이 들어가 있기 때문에 해당 패딩을 제어해서 처리해도 되지만 더 간단하게 Icon과 GestureDetector를 쓰도록 한다.

Column(
                  children: [
                    GestureDetector(
                      onTap: () {
                        DefaultTabController.of(context)!.animateTo(0);
                      },
                      child: Icon(
                        Icons.person,
                        color: Colors.white,
                      ),
                    ),
                    Text(
                      "라이브러리",
                      style: TextStyle(
                        color: Colors.white,
                      ),
                    ),
                  ],
                ),
Column(
                  children: [
                    GestureDetector(
                      onTap: () {
                        DefaultTabController.of(context)!.animateTo(2);
                      },
                      child: Icon(
                        Icons.show_chart,
                        color: Colors.white,
                      ),
                    ),
                    Text(
                      "차트",
                      style: TextStyle(
                        color: Colors.white,
                      ),
                    ),
                  ],
                ),

기본 색상이 블랙이기 때문에 white로 변경해 주고 눌렀을 때 동작을 추가해 준다. 여기서 DefaultTabController.of(context)~의 context는 페이지에 대한 정보를 가져온다.

 

그리고 아이콘에 여백을 주기 위해서 아이콘들을 묶었던 Row를 Padding으로 묶고 여백을 준다.

Padding(
              padding:
                  const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
              child: Row(

 

 

내용

문구 넣기

텍스트는 내부 Column안에 위젯을 넣어주면 중앙에 배치되는데 SizeBox를 사용해서 상단에서 띄워주도록 한다.

SizedBox(
              height: MediaQuery.of(context).size.height * 0.1,
            ),
            Text(
              "Shazam하려면 탭하세요",
              style: TextStyle(
                color: Colors.white,
                fontSize: 24,
                fontWeight: FontWeight.bold,
              ),
            ),

 

MediaQuery.of 는 현재 실행환경에 대한 정보를 가져올 수 있다. 해당 환경에서 크기 중 높이의 10%의 여백을 주도록 한다.

 

이미지 넣기

리소스 이미지에는 배경이 없기 때문에 BoxDecoration을 사용해서 직접 배경 원을 그려준다.

SizedBox(height: MediaQuery.of(context).size.height * 0.06),
Container(
              alignment: Alignment.center,
              width: 200,
              height: 200,
              decoration: BoxDecoration(
                color: Colors.blue[300],
                shape: BoxShape.circle,
              ),
              child: Image.network(
                "https://i.ibb.co/hxNbZ8p/shazam.png",
                color: Colors.white,
                width: 130,
                height: 130,
              ),
            ),

이미지는 문구와 어느 정도 거리를 띄우기 위해 SizeBox로 여백을 만든다. 정렬을 가운데로 맞춰 이미지가 원 중앙에 들어오도록 한다.

 

그 아래 검색 아이콘도 마찬가지로 만들어준다.

 SizedBox(height: MediaQuery.of(context).size.height * 0.06),
            Container(
              alignment: Alignment.center,
              width: 50,
              height: 50,
              decoration: BoxDecoration(
                color: Colors.white,
                shape: BoxShape.circle,
              ),
              child: Icon(Icons.search),
            )

 

처음부터 직접 만들려고 하다 보니 시간을 낭비하게 되었다.

기본 기능이 만들어져 있는 베이스 코드에서 위젯만 추가하는 과제였는데 어렵게 생각했나 보다.