因此,我正在尝试制作一个自定义可重用的appbar小部件,如下所示
class MyCustomAppBar extends StatelessWidget implements PreferredSizeWidget {
const MyCustomAppBar({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final primaryColor = Theme.of(context).primaryColor;
return AppBar(
backgroundColor: primaryColor,
elevation: 0, // to remove drop shadow
titleSpacing: 0, // to remove extra padding on appbar left side
title: Text("This Is appbar Title"),
);
}
@override
Size get preferredSize => const Size.fromHeight(50.0);
}然后我用它在我的脚手架上像这样
class NotificationListPage extends StatelessWidget {
const NotificationListPage({Key? key}) : super(key: key);
@override
Widget build(context) {
return Scaffold(
appBar: MyCustomAppBar(),
body: Column(
children: [
Container(
width: double.infinity,
height: 100,
color: Theme.of(context).primaryColor,
child: Center(
child: Text(
"this is container that has the same color with appbar",
style: TextStyle(
fontSize: 10,
),
),
),
),
],
),
);
}
}其结果是:

如您所见,在列内的appbar和容器之间有一条白色的水平线。而且它只会在Android上追加。怎么移除这个?
如果我将Scaffold背景色改为红色,那么这条水平线也会变成红色。
我需要定制一个可重用的小部件。如果我像下面的代码那样直接实现(没有小部件分离),那条水平线就不会出现。
class NotificationListPage extends StatelessWidget {
const NotificationListPage({Key? key}) : super(key: key);
@override
Widget build(context) {
final primaryColor = Theme.of(context).primaryColor;
return Scaffold(
appBar: AppBar( // I don't want to implement it directly like this. I want separate Appbar widget
backgroundColor: primaryColor,
elevation: 0, // to remove drop shadow
titleSpacing: 0, // to remove extra padding on appbar left side
title: Text("This Is appbar Title"),
),
body: Column(
children: [
Container(
width: double.infinity,
height: 100,
color: Theme.of(context).primaryColor,
child: Center(
child: Text(
"this is container that has the same color with appbar",
style: TextStyle(
fontSize: 10,
),
),
),
),
],
),
);
}
}发布于 2022-04-02 04:37:23
因为你的Scaffold背景色是白色的。
return Scaffold(
backgroundColor: primaryColor,
appBar: AppBar(...https://stackoverflow.com/questions/71714352
复制相似问题