大家好,我正在做SliverAppbar,但是我想要这种类型的自定义SliverAppbar,我如何实现这一点?
发布于 2022-02-23 11:18:19
您可以尝试使用SliverPersistentHeader
Widget和CustomScrollView
中包含的一堆条来模拟同样的情况。您所需要做的就是给AppBar
,SliverPersistentHeader
和第一个Sliver
相同的背景色。
请看下面的代码(很抱歉格式化错误)。我还包括了一个gif在最后作为它的外观(对不起,糟糕的设计)。
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
debugShowCheckedModeBanner: false,
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(backgroundColor: const Color(0xff222222),
title: const Text("JazzCash"),
elevation: 0,
actions: const [Icon(Icons.info), Icon(Icons.notifications)]),
body: CustomScrollView(
slivers: <Widget>[
// persistent header
SliverPersistentHeader(
pinned: true,
delegate: SliverAppBarDelegate(
minHeight: 60.0,
maxHeight: 60.0,
child: Container(
color: const Color(0xff222222), child:Row(children:
const [SizedBox(width: 10), CircleAvatar(), SizedBox(width: 10), Text("Muhammad", style: TextStyle(fontSize: 20, color: Colors.white)),
])),
),
),
SliverList(
delegate: SliverChildListDelegate(
[
// first sliver
Container(color: const Color(0xff222222),
padding: const EdgeInsets.all(10),
child: Column(crossAxisAlignment: CrossAxisAlignment.start,
children: [const Text("Login -->", style: TextStyle(color: Colors.amber, fontSize: 25, fontWeight: FontWeight.bold)),
const Text("to Make Payments", style: TextStyle(color: Colors.white, fontSize: 25, fontWeight: FontWeight.bold)),
const SizedBox(height: 10),
Row(children: [ElevatedButton(onPressed: (){}, child: const Text("Add money")), const SizedBox(width: 20), ElevatedButton(onPressed: (){}, child: const Text("Add account"))])
])),
Container(color: Colors.purple, height: 150.0),
Container(color: Colors.green, height: 150.0),
],
),
),
],
),
);
}
}
// defining delegate for sliverpersistendheader
class SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
SliverAppBarDelegate({
@required this.minHeight,
@required this.maxHeight,
@required this.child,
});
final double? minHeight;
final double? maxHeight;
final Widget? child;
@override
double get minExtent => minHeight!;
@override
double get maxExtent => math.max(maxHeight!, minHeight!);
@override
Widget build(
BuildContext context,
double shrinkOffset,
bool overlapsContent)
{
return SizedBox.expand(child: child);
}
@override
bool shouldRebuild(SliverAppBarDelegate oldDelegate) {
return maxHeight != oldDelegate.maxHeight ||
minHeight != oldDelegate.minHeight ||
child != oldDelegate.child;
}
}
发布于 2022-02-23 09:43:27
尝试这样做,并根据您的需求定制
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CustomScrollView(
slivers: [
SliverAppBar(
expandedHeight: 300,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: const FittedBox(
child: Text("loadedServices.title"),
),
background: Image.network(
"https://picsum.photos/250?image=9",
fit: BoxFit.fill,
),
),
),
SliverList(
delegate: SliverChildListDelegate(
[
const SizedBox(height: 30.0),
Padding(
padding: const EdgeInsets.only(left: 8.0, right: 8.0),
child: Column(
children: [
const Text(
"loadedServices.description",
textAlign: TextAlign.justify,
style: TextStyle(
fontFamily: 'OpenSans',
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Card(
elevation: 8,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 12.0,
vertical: 8,
),
child: Column(
children: const [
Text(
"loadedServices.totalClass",
style: TextStyle(fontSize: 14),
),
Text(
"Total Class",
style: TextStyle(fontSize: 12),
),
],
),
),
),
Card(
elevation: 8,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 12.0,
vertical: 8,
),
child: Column(
children: const [
Text(
"loadedServices.classDuration",
style: TextStyle(fontSize: 14),
),
Text(
"Class Duration",
style: TextStyle(fontSize: 12),
),
],
),
),
),
Card(
elevation: 8,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 12.0,
vertical: 8,
),
child: Column(
children: const [
Text(
"loadedServices.courseDuration",
style: TextStyle(fontSize: 14),
),
Text(
"Course Duration",
style: TextStyle(fontSize: 12),
),
],
),
),
),
],
),
const SizedBox(height: 20),
const Text(
'For further Information make a phone call or contact our WhatsApp Number',
textAlign: TextAlign.justify,
style: TextStyle(
fontSize: 20.0,
color: Colors.green,
fontWeight: FontWeight.bold,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(width: 10),
const Text(
'Or',
style: TextStyle(
fontSize: 20.0,
//color: Colors.green,
fontWeight: FontWeight.bold,
),
),
IconButton(
color: Colors.green,
icon: const Icon(
Icons.favorite,
size: 35,
),
onPressed: (){
},
),
],
),
const SizedBox(height: 20),
const Text(
'Visit Our WebSite',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
color: Colors.green,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 10),
const SizedBox(height: 20),
const Text(
'Visit Our Social Platform',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
color: Colors.green,
fontWeight: FontWeight.bold,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
color: Colors.greenAccent,
icon: const Icon(
Icons.favorite,
size: 35,
),
onPressed: (){},
),
IconButton(
color: Colors.red,
icon: const Icon(
Icons.favorite,
size: 35,
),
onPressed: (){},
),
],
),
],
),
),
const SizedBox(height: 260),
],
),
),
],
);
}
}
https://stackoverflow.com/questions/71234346
复制相似问题