我试图一次向所有屏幕添加渐变背景,所以我找到了一个好方法,但它没有按预期工作!!
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.red,
Colors.white,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: MaterialApp(
builder: EasyLoading.init(),
home: Scaffold(
backgroundColor: Colors.transparent,
),
),
);
}
}
检查代码后,我发现如果删除
builder: EasyLoading.init(),
,它就可以正常工作!
但我想要 EasyLoading ..那么有办法吗?
请尝试使用
Stack
小部件将渐变背景和 EasyLoading 相互叠加。
Stack(
children: [
Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.red,
Colors.white,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
),
MaterialApp(
builder: (context, child) {
return Stack(
children: [child!, EasyLoading.init()],
);
},
home: Scaffold(
backgroundColor: Colors.transparent,
),
),
],
)
希望这有帮助!