1、效果图
2、本节的知识点
2-1、为静态资源创建配置类
比如本节内容每张图片都有名字和描述,那我们统一配置一个类
class TravelBean {
String name;//名字
String location;//描述
String url;//静态资源的地址
TravelBean(this.name, this.location, this.url);
//我们可以添加静态方法 返回指定的内容
static List <TravelBean > generateTravelBean(){
return [
TravelBean (*,*,*)、、、
]
}
}
2-2、获取手机屏幕刘海(即手机型号等状态栏)的高度
有时候我们可能不会设置appBar,让背景图穿透AppBar
例如抖音的个人中心背景图它是直接穿透状态栏的,我们本次的详情页面也是穿透
MediaQuery.of(context).padding.top
2-3、获取屏幕的宽度/高度
1、double.infinity //无限大
2、MediaQuery.of(context).size.width /高度:.size.height
2-4、跨页面动画
本节效果的首页顶部图片切换路由至详情页的顶部动画
使用Hero
组件:注意此效果是跨页面即跨路由
Hero源码
const Hero({
Key key,
@required this.tag, //必选参数规定两个页面之间的Hero标签
this.createRectTween,
this.flightShuttleBuilder,
this.placeholderBuilder,
this.transitionOnUserGestures = false,//IOS滑动切换路由是否支持
@required this.child,//子节点
}) : assert(tag != null),
assert(transitionOnUserGestures != null),
assert(child != null),
super(key: key);
具体使用方法可以查看Flutter进阶:在应用中实现 Hero(飞行) 动画
2-4、获取AppBar的高度
kToolbarHeight
2-5、设置沉浸式导航栏
通常我们使用的都是
material
风格的样式,就会造成状态这一块有阴影
if (Platform.isAndroid) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.white,
statusBarBrightness: Brightness.dark,
statusBarIconBrightness: Brightness.dark));
}
2-6、设置状态栏字体黑白颜色
AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: Brightness.dark,
statusBarBrightness: Brightness.dark,
)
2-7、本节的重点:CustomScrollView组件
CustomScrollView
是可以使用Sliver
来自定义滚动模型(效果)的组件。它可以包含多种滚动模型,举个例子,假设有一个页面,顶部需要一个GridView
,底部需要一个ListView,而要求整个页面的滑动效果是统一的,即它们看起来是一个整体。如果使用GridView+ListView
来实现的话,就不能保证一致的滑动效果,因为它们的滚动效果是分离的,所以这时就需要一个"胶水",把这些彼此独立的可滚动组件"粘"起来,而CustomScrollView
的功能就相当于“胶水”。
3、源码剖析
//HomePage
import 'package:flutter/material.dart';
import 'TravelBean.dart';
import 'DetailPage.dart';
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0,
actions: <Widget>[
Padding(
padding: EdgeInsets.only(right: 15),
child: Icon(
Icons.menu,
color: Colors.black,
),
)
],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
child: Text(
"Travel Blog",
style: TextStyle(
color: Colors.black,
fontSize: 30,
),
),
),
Expanded(
flex: 2,
child: TravelWidget(),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 15),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Most Popular",
style: TextStyle(
color: Colors.black,
fontSize: 20,
),
),
Text(
"View all",
style: TextStyle(
color: Colors.deepOrange,
fontSize: 20,
),
),
],
),
),
Expanded(
flex: 1,
child: MostPopularWidget(),
)
],
),
);
}
}
class MostPopularWidget extends StatelessWidget {
List<TravelBean> _list = TravelBean.generateMostPopularBean();
@override
Widget build(BuildContext context) {
return ListView.builder(
padding: EdgeInsets.symmetric(horizontal: 15),
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
var bean = _list[index];
return GestureDetector(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
return DetailPage(bean);
}));
},
child: Hero(
tag: bean.url,
child: Stack(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(bottom: 30, right: 10),
child: ClipRRect(
borderRadius: BorderRadius.circular(5),
child: Image.asset(
bean.url,
width: 170,
fit: BoxFit.cover,
),
),
),
Positioned(
bottom: 50,
left: 15,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Material(
color: Colors.transparent,
child: Text(
bean.location,
style: TextStyle(
color: Colors.white,
fontSize: 15,
),
),
),
Material(
color: Colors.transparent,
child: Text(
bean.name,
style: TextStyle(
color: Colors.white,
fontSize: 15,
),
),
),
],
),
),
],
),
),
);
},
itemCount: _list.length,
);
}
}
class TravelWidget extends StatelessWidget {
List<TravelBean> _list = TravelBean.generateTravelBean();
@override
Widget build(BuildContext context) {
return PageView.builder(
controller: PageController(viewportFraction: 0.9),
itemBuilder: (context, index) {
var bean = _list[index];
return GestureDetector(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
return DetailPage(bean);
}));
},
child: Hero(
tag: bean.url,
child: Stack(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(bottom: 30, right: 10),
child: ClipRRect(
borderRadius: BorderRadius.circular(5),
child: Image.asset(
bean.url,
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover,
),
),
),
Positioned(
bottom: 80,
left: 15,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Material(
color: Colors.transparent,
child: Text(
bean.location,
style: TextStyle(
color: Colors.black54,
fontSize: 15,
),
),
),
Material(
color: Colors.transparent,
child: Text(
bean.name,
style: TextStyle(
color: Colors.black,
fontSize: 20,
),
),
),
],
),
),
Positioned(
bottom: 0,
right: 30,
child: Container(
width: 60,
height: 60,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(30),
),
child: Icon(
Icons.arrow_forward,
color: Colors.white,
size: 30,
),
),
)
],
),
),
);
},
itemCount: _list.length,
);
}
}
//DetailPage.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_ui/TravelBean.dart';
class DetailPage extends StatefulWidget {
final TravelBean bean;
DetailPage(this.bean);
@override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
final double expanded_height = 400;
final double rounded_container_height = 50;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
CustomScrollView(
slivers: <Widget>[
_buildSliverHead(),
SliverToBoxAdapter(
child: _buildDetail(),
)
],
),
Padding(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
child: SizedBox(
height: kToolbarHeight,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 15),
child: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.white),
onPressed: () {
Navigator.of(context).pop();
},
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 15),
child: Icon(Icons.menu, color: Colors.white),
)
],
)),
)
],
),
);
}
Widget _buildDetail() {
return Container(
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_bulidUserInfo(),
Padding(
padding: EdgeInsets.symmetric(vertical: 15, horizontal: 15),
child: Text(
"The balearic Lsnaled,The Lsnaled,The balea balearic Lsnaled,"
"The balearic Lsnaled,Lsnaled,The balea The balearic Lsnaled,"
"The balearic Lsnaled,Lsnaled,The balea The balearic Lsnaled,"
"The balearic Lsnaled,Lsnaled,The balea The balearic Lsnaled,"
"The balearic Lsnaled,Lsnaled,The balea The balearic Lsnaled,"
"The balearic Lsnaled,The balearic Lsnaled,The balea Lsnaled,"
"The balearic Lsnaled,The balearic Lsnaled,",
style: TextStyle(
color: Colors.black38,
height: 1.4,
fontSize: 14,
decoration: TextDecoration.none),
),
),
Padding(
padding: EdgeInsets.only(left: 15, right: 30, bottom: 10, top: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Featured",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 18,
letterSpacing: 1.6,
decoration: TextDecoration.none),
),
Text(
"View All",
style: TextStyle(
color: Colors.deepOrange,
fontWeight: FontWeight.bold,
fontSize: 18,
letterSpacing: 1.6,
decoration: TextDecoration.none),
)
],
),
),
Container(
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 0),
child: FeaturedWidget(),
height: 130,
),
Padding(
padding: EdgeInsets.symmetric(vertical: 15, horizontal: 15),
child: Text(
"The balearic Lsnaled,The Lsnaled,The balea balearic Lsnaled,"
"The balearic Lsnaled,Lsnaled,The balea The balearic Lsnaled,"
"The balearic Lsnaled,Lsnaled,The balea The balearic Lsnaled,"
"The balearic Lsnaled,Lsnaled,The balea The balearic Lsnaled,"
"The balearic Lsnaled,Lsnaled,The balea The balearic Lsnaled,"
"The balearic Lsnaled,The balearic Lsnaled,The balea Lsnaled,"
"The balearic Lsnaled,The balearic Lsnaled,",
style: TextStyle(
color: Colors.black38,
height: 1.4,
fontSize: 14,
decoration: TextDecoration.none),
),
),
],
),
);
}
Widget _bulidUserInfo() {
return Padding(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 8),
child: Row(
children: <Widget>[
CircleAvatar(
backgroundColor: Colors.white,
child: ClipRRect(
borderRadius: BorderRadius.circular(50),
child: Image.asset(
widget.bean.url,
width: 50,
height: 50,
fit: BoxFit.cover,
),
),
),
SizedBox(width: 10),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
widget.bean.name,
style: TextStyle(
color: Colors.black,
fontSize: 18,
decoration: TextDecoration.none,
fontWeight: FontWeight.bold),
),
Text(
"Writer,Wonderlust",
style: TextStyle(
color: Colors.black,
fontSize: 14,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal),
)
],
),
Spacer(),
Icon(
Icons.share,
color: Colors.black54,
)
],
),
);
}
Widget _buildSliverHead() {
return SliverPersistentHeader(
delegate: DetailSliverDelegate(
expanded_height, widget.bean, rounded_container_height));
}
}
class FeaturedWidget extends StatelessWidget {
List<TravelBean> _list = TravelBean.generateMostPopularBean();
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index) {
return Container(
width: 120,
height: 130,
margin: EdgeInsets.only(right: 15),
child: Image.asset(
_list[index].url,
width: 120,
height: 130,
fit: BoxFit.cover,
),
);
},
itemCount: _list.length,
scrollDirection: Axis.horizontal);
}
}
class DetailSliverDelegate extends SliverPersistentHeaderDelegate {
final double expandedHeight;
final TravelBean bean;
final double rounded_container_height;
DetailSliverDelegate(
this.expandedHeight, this.bean, this.rounded_container_height);
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: Brightness.dark,
statusBarBrightness: Brightness.dark,
),
child: Stack(
children: <Widget>[
Hero(
tag: bean.url,
child: Image.asset(
bean.url,
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover,
)),
Positioned(
child: Container(
width: MediaQuery.of(context).size.width,
height: rounded_container_height,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30))),
),
top: expandedHeight - rounded_container_height + 5 - shrinkOffset,
left: 0,
),
Positioned(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
bean.name,
style: TextStyle(
color: Colors.white,
fontSize: 20,
decoration: TextDecoration.none),
),
Text(
bean.location,
style: TextStyle(
color: Colors.white,
fontSize: 15,
decoration: TextDecoration.none),
)
],
),
top: expandedHeight - 120 - shrinkOffset,
left: 30,
),
],
),
);
}
@override
double get maxExtent => expandedHeight;
@override
double get minExtent => 0;
@override
bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
return true;
}
}
//main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'HomePage.dart';
void main(){
runApp(MyApp());
if(Platform.isAndroid){
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(statusBarColor:Colors.white,statusBarBrightness: Brightness.dark,statusBarIconBrightness: Brightness.dark));
}
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "精美UI",
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
//TravelBean.dart
class TravelBean {
String name;
String location;
String url;
TravelBean(this.name, this.location, this.url);
static List<TravelBean> generateTravelBean() {
return [
TravelBean("Peach", "Spain ES1", "assets/images/top1.jpg"),
TravelBean("Grassland", "Spain ES2", "assets/images/top2.jpg"),
TravelBean("Starry sky", "Spain ES3", "assets/images/top3.jpg"),
TravelBean("Beauty Pic", "Spain ES4", "assets/images/top4.jpg"),
];
}
static List<TravelBean> generateMostPopularBean() {
return [
TravelBean("Peach", "Spain ES", "assets/images/bottom1.jpg"),
TravelBean("Grassland", "Spain ES", "assets/images/bottom2.jpg"),
TravelBean("Starry sky", "Spain ES", "assets/images/bottom3.jpg"),
TravelBean("Beauty Pic", "Spain ES", "assets/images/bottom4.jpg"),
];
}
}