我正在开发一个 flutter 应用程序并使用 screen util 包 ScreenUtil,但是当使用 child 属性插入 Material 应用程序小部件时,该应用程序给了我一个错误。这是我的代码:`
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:spoon_app/screens/splash_screen/mobile.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const Spoon());
}
class Spoon extends StatelessWidget {
const Spoon({super.key});
@override
Widget build(BuildContext context) {
return ScreenUtilInit(
designSize: const Size(375, 812),
minTextAdapt: true,
splitScreenMode: true,
// 何时使用它产生的子属性并产生错误
child: MaterialApp(
theme: ThemeData(
scaffoldBackgroundColor: const Color(0XFFFFFFFF),
textTheme: TextTheme(
bodySmall: TextStyle(
fontSize: 14.sp,
fontFamily: "Alex",
fontWeight: FontWeight.w400,
height: 1.3.h,
),
bodyMedium: TextStyle(
fontSize: 12.sp,
fontFamily: "Alex",
fontWeight: FontWeight.w700,
),
bodyLarge: const TextStyle(
color: Colors.white,
fontSize: 18,
fontFamily: "Alex",
fontWeight: FontWeight.w700,
),
headlineMedium: const TextStyle(
fontSize: 32,
fontWeight: FontWeight.w500,
),
headlineLarge: TextStyle(
fontFamily: "Alex",
fontSize: 32.sp,
fontWeight: FontWeight.w500,
)),
),
debugShowCheckedModeBanner: false,
title: "SpoonApp",
home: MobileWelcomeScreen(),
),
);
}
}
`
我是否急需解决我的问题?
尝试像这样使用构建器和子属性:
@override
Widget build(BuildContext context) {
return ScreenUtilInit(
designSize: const Size(375, 812),
minTextAdapt: true,
splitScreenMode: true,
builder: (_ , child) {
return MaterialApp(
theme: ThemeData(
scaffoldBackgroundColor: const Color(0XFFFFFFFF),
textTheme: TextTheme(
bodySmall: TextStyle(
fontSize: 14.sp,
fontFamily: "Alex",
fontWeight: FontWeight.w400,
height: 1.3.h,
),
bodyMedium: TextStyle(
fontSize: 12.sp,
fontFamily: "Alex",
fontWeight: FontWeight.w700,
),
bodyLarge: const TextStyle(
color: Colors.white,
fontSize: 18,
fontFamily: "Alex",
fontWeight: FontWeight.w700,
),
headlineMedium: const TextStyle(
fontSize: 32,
fontWeight: FontWeight.w500,
),
headlineLarge: TextStyle(
fontFamily: "Alex",
fontSize: 32.sp,
fontWeight: FontWeight.w500,
)),
),
debugShowCheckedModeBanner: false,
title: "SpoonApp",
home: child,
);
},
child: MobileWelcomeScreen(),
);
}