
🙊 문제인식
- 앱을 개발하면 여러 디바이스를 지원하기 위해 디바이스에 대응하여 각 UI의 크기를 재조정해야한다. 이때, MediaQuery를 통해서 각 Widget들의 크기를 정의 할 수 있으나 상당히 귀찮다..
- 특히 flutter는 IOS, Android 두 OS 모두 지원해야 하기 때문에 디바이스 마다의 UI차이가 더욱 부각되는 점이 있다..!!
https://pub.dev/packages/flutter_screenutil
flutter_screenutil package를 다운 받는다. 그렇다면 pubspec.yaml 파일에 해당 종속성이 추가 된다.
dependencies:
flutter:
sdk: flutter
# add flutter_screenutil
flutter_screenutil: ^{latest version}
import 'package:flutter_screenutil/flutter_screenutil.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
Widget build(BuildContext context) {
//Set the fit size (Find your UI design, look at the dimensions of the device screen and fill it in,unit in dp)
return ScreenUtilInit(
designSize: const Size(360, 690),
minTextAdapt: true,
splitScreenMode: true,
builder: (context , child) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'First Method',
// You can use the library anywhere in the app even in theme
theme: ThemeData(
primarySwatch: Colors.blue,
textTheme: Typography.englishLike2018.apply(fontSizeFactor: 1.sp),
),
home: child,
);
},
child: const HomePage(title: 'First Method'),
);
}
}
Container(
width: 50.w,
height:200.h
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.r),
),
)
Text(
'16sp,if data is not set in MediaQuery,my font size will change with the system.',
style: TextStyle(
color: Colors.black,
fontSize: 16.sp,
),
),
- 위와 같은 형태로 반응형 UI가 필요한 모든 widget들의 크기, 글꼴 등을 조절해주면 된다.
- padding같은 값도 이와 같은 형태로 설정 할 수 있기에 혁명적이다..! (사실상 width, height를 가지는 모든 것들이 가능한 것 같다.)
ex) EdgeInsets.only(left:8.w,right:8.w)
- 이렇게 반응형 UI를 만들었지만 시스템 자체 글꼴 크기로 인해서 overflow 혹은 원하는 UI디자인이 제대로 구현되지 않을 수 있다.
- 이를 해결하기 위해서 system fontsize에 무관하게 동작하도록 설정하는 것이 권장된다.
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter_ScreenUtil',
theme: ThemeData(
primarySwatch: Colors.blue,
),
builder: (context, widget) {
return MediaQuery(
///Setting font does not change with system font size
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
child: widget,
);
},
home: HomePage(title: 'FlutterScreenUtil Demo'),
),
Great post! I really like how you explained the approach to building a responsive UI in Flutter. It’s such an important aspect when creating apps that work smoothly across different devices and screen sizes.
If you’re also curious about the broader aspects of optimizing apps, here’s a helpful resource on flutter performance. It highlights the strengths of Flutter and why it’s considered a powerful framework not only for UI but also for speed and efficiency. Combining insights about responsive design with performance best practices can really make a difference in building high-quality apps.