Study

43. Flutter로 다양한 화면 크기에 대응하는 적응형 디자인 구현하기 | Flutter

구구 구구 2024. 8. 12. 11:00
반응형

손그림 스타일, dall-e

 

 

Flutter에서 적응형 앱을 만드는 일반적인 접근 방법

 

01. 서론

1) Flutter에서 적응형 앱의 필요성

현대의 모바일 및 데스크탑 환경에서는 다양한 크기와 해상도의 디바이스가 존재합니다. 이러한 환경에서 앱이 모든 디바이스에서 일관된 사용자 경험을 제공하기 위해서는 적응형(adaptive) 디자인이 필수적입니다. 적응형 디자인을 통해 앱은 작은 스마트폰 화면에서부터 큰 태블릿이나 데스크탑 화면까지, 모든 크기의 화면에서 최적화된 인터페이스를 제공할 수 있습니다.

 

Flutter는 다양한 장치와 플랫폼을 지원하는 강력한 도구로, 단일 코드베이스로 여러 플랫폼에서 일관된 사용자 경험을 제공할 수 있도록 합니다. 따라서 Flutter를 사용하여 적응형 앱을 개발하는 것은 매우 중요합니다.

2) 일반적인 접근 방법 개요

Flutter에서 적응형 디자인을 구현하기 위해 Google 엔지니어들은 세 가지 주요 단계를 제안합니다: 추상화(Abstract), 측정(Measure), 분기(Branch).

  • 추상화(Abstract): 동적으로 만들고자 하는 위젯을 식별하고 해당 위젯의 생성자를 분석하여 공유할 수 있는 데이터를 추상화합니다.
  • 측정(Measure): 화면 크기를 결정하기 위해 MediaQueryLayoutBuilder를 사용하여 전체 화면 크기와 특정 위젯의 크기를 측정합니다.
  • 분기(Branch): UI를 표시할 버전을 선택할 때 사용할 크기 분기점을 결정합니다.

이 세 단계 접근법을 통해 Flutter 앱은 다양한 화면 크기와 장치에서 일관된 사용자 경험을 제공할 수 있습니다.

 

02. 추상화 단계

1) 적응형 위젯 식별 및 데이터 추상화

적응형 앱을 설계할 때 가장 먼저 해야 할 일은 적응이 필요한 위젯을 식별하는 것입니다. 적응형 위젯은 화면 크기나 장치 특성에 따라 다르게 동작하거나 표시되어야 하는 UI 요소를 말합니다. 일반적으로 적응형 위젯에는 전체 화면, 모달 다이얼로그, 내비게이션 UI 등이 포함됩니다.

 

위젯을 식별한 후에는 해당 위젯의 생성자를 분석하여 공유할 수 있는 데이터를 추상화합니다. 예를 들어, 다이얼로그 위젯에서는 다이얼로그 내용을 포함하는 정보를 추상화할 수 있습니다. 이렇게 추상화된 데이터는 화면 크기나 장치 특성에 따라 다른 방식으로 UI를 렌더링하는 데 사용됩니다.

예제: 적응형 위젯 식별 및 데이터 추상화

import 'package:flutter/material.dart';

class AdaptiveDialog extends StatelessWidget {
  final String title;
  final String content;

  const AdaptiveDialog({
    Key? key,
    required this.title,
    required this.content,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;

    return AlertDialog(
      title: Text(title),
      content: screenSize.width > 600 ? _buildWideContent() : _buildNarrowContent(),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(context).pop(),
          child: Text('Close'),
        ),
      ],
    );
  }

  Widget _buildWideContent() {
    return Row(
      children: [
        Icon(Icons.info, size: 48),
        SizedBox(width: 16),
        Expanded(child: Text(content)),
      ],
    );
  }

  Widget _buildNarrowContent() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Icon(Icons.info, size: 48),
        SizedBox(height: 16),
        Text(content),
      ],
    );
  }
}
            

2) 다이얼로그 및 내비게이션 UI 예제

적응형 다이얼로그와 내비게이션 UI는 다양한 화면 크기에 따라 다른 레이아웃을 제공해야 합니다. 예를 들어, 모바일 장치에서는 하단 내비게이션 바를 사용하고, 태블릿이나 데스크탑에서는 내비게이션 레일을 사용할 수 있습니다.

예제: 적응형 내비게이션 UI

import 'package:flutter/material.dart';

class AdaptiveNavigation extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;

    return Scaffold(
      appBar: AppBar(title: Text('Adaptive Navigation')),
      body: Center(child: Text('Content Area')),
      bottomNavigationBar: screenSize.width > 600 ? null : _buildBottomNavigationBar(),
      drawer: screenSize.width > 600 ? _buildNavigationRail() : null,
    );
  }

  Widget _buildBottomNavigationBar() {
    return BottomNavigationBar(
      items: const [
        BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
        BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
        BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Settings'),
      ],
    );
  }

  Widget _buildNavigationRail() {
    return NavigationRail(
      destinations: const [
        NavigationRailDestination(icon: Icon(Icons.home), label: Text('Home')),
        NavigationRailDestination(icon: Icon(Icons.search), label: Text('Search')),
        NavigationRailDestination(icon: Icon(Icons.settings), label: Text('Settings')),
      ],
      selectedIndex: 0,
    );
  }
}
            

03. 측정 단계

1) MediaQuery를 사용한 전체 화면 크기 측정

MediaQuery는 Flutter에서 화면 크기, 방향, 밀도 등 다양한 미디어 관련 정보를 제공하는 유틸리티입니다. 이를 사용하면 전체 화면 크기를 측정하고, 그에 따라 UI를 동적으로 조정할 수 있습니다.

예제: MediaQuery를 사용한 전체 화면 크기 측정

import 'package:flutter/material.dart';

class MediaQueryExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    final screenWidth = screenSize.width;
    final screenHeight = screenSize.height;

    return Scaffold(
      appBar: AppBar(title: Text('MediaQuery Example')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Screen Width: $screenWidth'),
            Text('Screen Height: $screenHeight'),
          ],
        ),
      ),
    );
  }
}
            

2) LayoutBuilder를 사용한 특정 위젯 크기 측정

LayoutBuilder는 부모 위젯의 레이아웃 제약 조건을 제공하여 해당 위치에서 유효한 너비와 높이 범위를 얻을 수 있게 합니다. 이를 사용하면 특정 위젯의 크기를 측정하고, 그에 따라 UI를 조정할 수 있습니다.

예제: LayoutBuilder를 사용한 특정 위젯 크기 측정

import 'package:flutter/material.dart';

class LayoutBuilderExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('LayoutBuilder Example')),
      body: LayoutBuilder(
        builder: (context, constraints) {
          final maxWidth = constraints.maxWidth;
          final maxHeight = constraints.maxHeight;

          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('Max Width: $maxWidth'),
                Text('Max Height: $maxHeight'),
              ],
            ),
          );
        },
      ),
    );
  }
}
            

3) MediaQuery와 LayoutBuilder의 차이점 및 사용 사례

MediaQuery: 전체 화면 크기, 방향, 밀도 등의 정보를 제공합니다. 앱 전체의 레이아웃을 조정하거나, 화면 크기에 따라 다른 UI를 렌더링할 때 유용합니다.

LayoutBuilder: 부모 위젯의 레이아웃 제약 조건을 제공합니다. 특정 위젯의 크기에 따라 동적으로 레이아웃을 조정할 때 유용합니다.

사용 사례

  • MediaQuery: 전체 화면 크기에 따라 레이아웃을 조정할 때.
  • LayoutBuilder: 특정 위젯의 크기에 따라 레이아웃을 조정할 때.

 

04. 분기 단계

1) 크기 분기점을 사용한 UI 버전 선택

크기 분기점을 사용하면 화면 크기에 따라 다른 UI 버전을 선택할 수 있습니다. 이를 통해 다양한 화면 크기에서 최적화된 사용자 경험을 제공할 수 있습니다. Flutter에서는 화면 너비를 기준으로 크기 분기점을 설정하여 UI를 분기할 수 있습니다.

예제: 크기 분기점을 사용한 UI 버전 선택

import 'package:flutter/material.dart';

class ResponsiveUI extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Responsive UI')),
      body: LayoutBuilder(
        builder: (context, constraints) {
          if (constraints.maxWidth > 600) {
            return _buildWideScreenLayout();
          } else {
            return _buildNarrowScreenLayout();
          }
        },
      ),
    );
  }

  Widget _buildWideScreenLayout() {
    return Row(
      children: [
        Expanded(child: _buildSidebar()),
        Expanded(flex: 3, child: _buildContent()),
      ],
    );
  }

  Widget _buildNarrowScreenLayout() {
    return Column(
      children: [
        _buildContent(),
        _buildBottomNavigationBar(),
      ],
    );
  }

  Widget _buildSidebar() {
    return Container(
      color: Colors.blue,
      child: Center(child: Text('Sidebar')),
    );
  }

  Widget _buildContent() {
    return Container(
      color: Colors.green,
      child: Center(child: Text('Content')),
    );
  }

  Widget _buildBottomNavigationBar() {
    return BottomNavigationBar(
      items: const [
        BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
        BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
        BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Settings'),
      ],
    );
  }
}
            

2) Material 디자인 가이드라인을 따르는 예제

Material 디자인 가이드라인에서는 화면 너비에 따라 적응형 UI를 구현하는 방법을 제시합니다. 예를 들어, 화면 너비가 600 픽셀 미만인 경우 하단 내비게이션 바를 사용하고, 600 픽셀 이상인 경우 내비게이션 레일을 사용하는 것입니다.

예제: Material 디자인 가이드라인을 따르는 적응형 UI

import 'package:flutter/material.dart';

class MaterialAdaptiveNavigation extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Material Adaptive Navigation')),
      body: Center(child: Text('Content Area')),
      bottomNavigationBar: _buildNavigation(context),
    );
  }

  Widget _buildNavigation(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;

    if (screenSize.width > 600) {
      return NavigationRail(
        destinations: const [
          NavigationRailDestination(icon: Icon(Icons.home), label: Text('Home')),
          NavigationRailDestination(icon: Icon(Icons.search), label: Text('Search')),
          NavigationRailDestination(icon: Icon(Icons.settings), label: Text('Settings')),
        ],
        selectedIndex: 0,
      );
    } else {
      return BottomNavigationBar(
        items: const [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
          BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
          BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Settings'),
        ],
      );
    }
  }
}
            

3) 적응형 내비게이션 구현

적응형 내비게이션은 화면 크기와 장치 특성에 따라 내비게이션 UI를 동적으로 조정합니다. 이를 통해 사용자 경험을 최적화할 수 있습니다.

예제: 적응형 내비게이션 구현

import 'package:flutter/material.dart';

class AdaptiveNavigationExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;

    return Scaffold(
      appBar: AppBar(title: Text('Adaptive Navigation Example')),
      body: Center(child: Text('Content Area')),
      bottomNavigationBar: screenSize.width > 600 ? null : _buildBottomNavigationBar(),
      drawer: screenSize.width > 600 ? _buildNavigationRail() : null,
    );
  }

  Widget _buildBottomNavigationBar() {
    return BottomNavigationBar(
      items: const [
        BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
        BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
        BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Settings'),
      ],
    );
  }

  Widget _buildNavigationRail() {
    return NavigationRail(
      destinations: const [
        NavigationRailDestination(icon: Icon(Icons.home), label: Text('Home')),
        NavigationRailDestination(icon: Icon(Icons.search), label: Text('Search')),
        NavigationRailDestination(icon: Icon(Icons.settings), label: Text('Settings')),
      ],
      selectedIndex: 0,
    );
  }
}
            

 

05. 실제 사례 분석

1) Google의 대형 앱에서의 적응형 디자인 사례

Google은 자사의 대형 앱에서 적응형 디자인을 광범위하게 사용하고 있습니다. Google의 다양한 서비스(예: Google Calendar, Google Drive)는 모두 다양한 장치에서 일관된 사용자 경험을 제공하기 위해 적응형 디자인을 채택하고 있습니다. Google Calendar 앱을 예로 들어 보겠습니다.

 

Google Calendar 적응형 디자인 사례:

  • 모바일 버전: 모바일 장치에서는 화면이 좁기 때문에, 주요 내비게이션 요소가 하단 내비게이션 바에 배치됩니다. 일정을 추가하거나 보기를 전환하는 버튼은 쉽게 접근할 수 있도록 화면 하단에 배치됩니다.
  • 태블릿 버전: 태블릿에서는 화면이 넓기 때문에, 사이드바를 사용하여 내비게이션 요소를 배치합니다. 일정 목록과 세부 정보가 동시에 표시되며, 일정 추가 버튼은 화면의 더 넓은 영역에서 쉽게 접근할 수 있습니다.
  • 데스크탑 버전: 데스크탑에서는 더욱 넓은 화면을 활용하여 여러 패널을 동시에 표시합니다. 사이드바 내비게이션, 일정 목록, 일정 세부 정보가 모두 한 화면에 표시되어 사용자 경험을 최적화합니다.

이러한 적응형 디자인 접근 방식은 Google Calendar가 다양한 장치에서 일관된 사용자 경험을 제공하는 데 도움을 줍니다.

2) 다양한 장치에서의 적응형 UI 구현 예제

Flutter를 사용하여 다양한 장치에서 적응형 UI를 구현하는 방법을 살펴보겠습니다. 예를 들어, 동일한 앱이 모바일, 태블릿, 데스크탑에서 각각 다른 레이아웃을 가질 수 있도록 적응형 UI를 구현할 수 있습니다.

예제: 다양한 장치에서의 적응형 UI

import 'package:flutter/material.dart';

class AdaptiveAppExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Adaptive App Example')),
      body: LayoutBuilder(
        builder: (context, constraints) {
          if (constraints.maxWidth > 1200) {
            return _buildDesktopLayout();
          } else if (constraints.maxWidth > 600) {
            return _buildTabletLayout();
          } else {
            return _buildMobileLayout();
          }
        },
      ),
    );
  }

  Widget _buildMobileLayout() {
    return Column(
      children: [
        Expanded(child: Center(child: Text('Mobile Layout'))),
        _buildBottomNavigationBar(),
      ],
    );
  }

  Widget _buildTabletLayout() {
    return Row(
      children: [
        Expanded(flex: 1, child: Center(child: Text('Tablet Sidebar'))),
        Expanded(flex: 3, child: Center(child: Text('Tablet Content'))),
      ],
    );
  }

  Widget _buildDesktopLayout() {
    return Row(
      children: [
        Expanded(flex: 1, child: Center(child: Text('Desktop Sidebar'))),
        Expanded(flex: 2, child: Center(child: Text('Desktop Content'))),
        Expanded(flex: 1, child: Center(child: Text('Desktop Additional Panel'))),
      ],
    );
  }

  Widget _buildBottomNavigationBar() {
    return BottomNavigationBar(
      items: const [
        BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
        BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
        BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Settings'),
      ],
    );
  }
}
            

 

06. 적응형 앱을 위한 모범 사례

1) 장치별 레이아웃 계획 팁

적응형 디자인을 구현할 때는 장치별 레이아웃 계획이 중요합니다. 다음은 장치별 레이아웃을 계획할 때 유용한 팁입니다.

  • 모바일 장치: 화면이 작기 때문에 중요한 정보와 기능을 우선적으로 배치합니다. 내비게이션 바는 하단에 배치하고, 버튼과 같은 인터랙티브 요소는 쉽게 접근할 수 있는 곳에 위치시킵니다.
  • 태블릿 장치: 화면이 넓어지므로 추가적인 정보와 기능을 함께 표시할 수 있습니다. 사이드바 내비게이션을 사용하여 주요 기능에 쉽게 접근할 수 있도록 합니다.
  • 데스크탑 장치: 화면이 가장 넓으므로 여러 패널을 동시에 표시할 수 있습니다. 사이드바, 상단 내비게이션 바, 메인 콘텐츠 영역을 분리하여 사용자 경험을 최적화합니다.

2) 조건부 위젯 사용 방법

조건부 위젯을 사용하면 화면 크기나 장치 특성에 따라 동적으로 UI를 변경할 수 있습니다. Flutter에서는 LayoutBuilder와 같은 위젯을 사용하여 조건부 위젯을 구현할 수 있습니다.

예제: 조건부 위젯 사용

import 'package:flutter/material.dart';

class ConditionalWidgetExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Conditional Widget Example')),
      body: LayoutBuilder(
        builder: (context, constraints) {
          if (constraints.maxWidth > 600) {
            return _buildWideLayout();
          } else {
            return _buildNarrowLayout();
          }
        },
      ),
    );
  }

  Widget _buildWideLayout() {
    return Row(
      children: [
        Expanded(child: Center(child: Text('Wide Layout'))),
        Expanded(child: Center(child: Text('Additional Panel'))),
      ],
    );
  }

  Widget _buildNarrowLayout() {
    return Center(child: Text('Narrow Layout'));
  }
}
            

3) 장치 특성 고려

적응형 디자인을 구현할 때 각 장치의 고유한 특성을 고려하는 것이 중요합니다. 예를 들어, 모바일 장치는 터치 인터페이스를 주로 사용하므로 큰 버튼과 간결한 레이아웃을 선호합니다. 반면에 데스크탑 장치는 마우스와 키보드를 주로 사용하므로 더 많은 정보를 한 화면에 표시할 수 있습니다.

예제: 장치 특성 고려

import 'package:flutter/material.dart';

class DeviceSpecificLayoutExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final isMobile = MediaQuery.of(context).size.width < 600;

    return Scaffold(
      appBar: AppBar(title: Text('Device Specific Layout Example')),
      body: isMobile ? _buildMobileLayout() : _buildDesktopLayout(),
    );
  }

  Widget _buildMobileLayout() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text('Mobile Layout'),
          SizedBox(height: 20),
          ElevatedButton(
            onPressed: () {},
            child: Text('Touch Button'),
          ),
        ],
      ),
    );
  }

  Widget _buildDesktopLayout() {
    return Row(
      children: [
        Expanded(child: Center(child: Text('Desktop Layout'))),
        Expanded(
          child: Center(
            child: ElevatedButton(
              onPressed: () {},
              child: Text('Click Button'),
            ),
          ),
        ),
      ],
    );
  }
}
            

관련된 다른 글도 읽어보시길 추천합니다

 

2024.08.02 - [Study] - 38. 스크롤링 개요: 다양한 스크롤링 방법과 위젯 소개 | Flutter

 

38. 스크롤링 개요: 다양한 스크롤링 방법과 위젯 소개 | Flutter

Flutter에서 스크롤링 개요: 다양한 스크롤링 방법과 위젯 소개 01. 서론1) Flutter에서 스크롤링의 중요성Flutter는 모바일, 웹, 데스크탑 등 다양한 플랫폼에서 일관된 UI를 제공하는 오픈 소스 UI 프

guguuu.com

2024.08.02 - [Study] - 37. Flutter에서 긴 리스트를 효율적으로 처리하는 방법 | Flutter

 

37. Flutter에서 긴 리스트를 효율적으로 처리하는 방법 | Flutter

Flutter에서 긴 리스트를 효율적으로 처리하는 방법 01. 서론1) Flutter에서 긴 리스트의 중요성Flutter는 모바일, 웹, 데스크탑 등 다양한 플랫폼에서 일관된 UI를 제공하는 오픈 소스 UI 프레임워크입

guguuu.com

2024.08.02 - [Study] - 36. Flutter에서 일정 간격이 있는 리스트 만들기 | Flutter

 

36. Flutter에서 일정 간격이 있는 리스트 만들기 | Flutter

Flutter로 일정 간격이 있는 리스트 만들기 01. 서론Flutter에서 리스트 간격의 필요성Flutter는 크로스플랫폼 모바일 애플리케이션 개발을 위한 강력한 프레임워크로, 다양한 UI 구성 요소를 제공합

guguuu.com


읽어주셔서 감사합니다

공감은 힘이 됩니다

 

:)

반응형