YUYANE

FLUTTER / Stack, RichText, Navigator 본문

Framework/FLUTTER

FLUTTER / Stack, RichText, Navigator

YUYA 2021. 5. 5. 16:17

출처 github.com/abuanwar072/Plant-App-Flutter-UI

 

1. Stack 사용 방법 예시

 

- Stack 위젯에서 시작하면 아래에서 하나하나 쌓이는 느낌

- 원하는 위치가 있다면 Position 위젯 사용해서 위치 지정하면 됨

Container(
      margin: EdgeInsets.only(bottom: kDefaultPadding * 2.5),
      // It will cover 20% of our total height
      height: size.height * 0.2,
      child: Stack(
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(
              left: kDefaultPadding,
              right: kDefaultPadding,
              bottom: 36 + kDefaultPadding,
            ),
            height: size.height * 0.2 - 27,
            decoration: BoxDecoration(
              color: kPrimaryColor,
              borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(36),
                bottomRight: Radius.circular(36),
              ),
            ),
            child: Row(
              children: <Widget>[
                Text(
                  'Hi Uishopy!',
                  style: Theme.of(context).textTheme.headline5.copyWith(
                      color: Colors.white, fontWeight: FontWeight.bold),
                ),
                Spacer(),
                Image.asset("assets/images/logo.png")
              ],
            ),
          ),
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: Container(
              alignment: Alignment.center,
              margin: EdgeInsets.symmetric(horizontal: kDefaultPadding),
              padding: EdgeInsets.symmetric(horizontal: kDefaultPadding),
              height: 54,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20),
                boxShadow: [
                  BoxShadow(
                    offset: Offset(0, 10),
                    blurRadius: 50,
                    color: kPrimaryColor.withOpacity(0.23),
                  ),
                ],
              ),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: TextField(
                      onChanged: (value) {},
                      decoration: InputDecoration(
                        hintText: "Search",
                        hintStyle: TextStyle(
                          color: kPrimaryColor.withOpacity(0.5),
                        ),
                        enabledBorder: InputBorder.none,
                        focusedBorder: InputBorder.none,
                        // surffix isn't working properly  with SVG
                        // thats why we use row
                        // suffixIcon: SvgPicture.asset("assets/icons/search.svg"),
                      ),
                    ),
                  ),
                  SvgPicture.asset("assets/icons/search.svg"),
                ],
              ),
            ),
          ),
        ],
      ),
    );

 

2. 화면 비율 조정

 

- 전체를 아우르는 클래스에서 사이즈 읽고,

- 각 위젯에서 차지할 사이즈를 지정하면 된다.

class Body extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // It will provie us total height  and width of our screen
    Size size = MediaQuery.of(context).size;
    // it enable scrolling on small device
    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          HeaderWithSearchBox(size: size),
          TitleWithMoreBtn(title: "Recomended", press: () {}),
          RecomendsPlants(),
          TitleWithMoreBtn(title: "Featured Plants", press: () {}),
          FeaturedPlants(),
          SizedBox(height: kDefaultPadding),
        ],
      ),
    );
  }
}
class HeaderWithSearchBox extends StatelessWidget {
  const HeaderWithSearchBox({
    Key key,
    @required this.size,
  }) : super(key: key);

  final Size size;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(bottom: kDefaultPadding * 2.5),
      // It will cover 20% of our total height
      height: size.height * 0.2,
      child: Stack(

 

3. 클래스 나누는 기준

 

- 재사용 가능성이 있는 것은 클래스로 나누되, 한 곳에서만 재사용하는 것이라면 파일을 굳이 구분하지는 않는다.

 

 

4. RichText

 

- 같은 위젯 안에 있는 텍스트 스타일을 다르게 만들고 싶을 때 유용

- www.youtube.com/watch?v=rykDVh-QFfw

 

 

5. 주소 변화 없이 다른 Page 이동 방법

 

press: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => DetailsScreen(),
                ),
              );
            },
 onPressed: () {
                          Navigator.pop(context);
                        },

(다시 뒤로가기)

'Framework > FLUTTER' 카테고리의 다른 글

FLUTTER / Remove Tooltip of PopupMenuButton Widget  (0) 2021.06.19
FLUTTER/ Drawer, Expanded, Responsive  (0) 2021.05.06
Comments