extended_nested_scroll_view
extended_nested_scroll_view copied to clipboard
I have used ExtendedNestedScrollView+TabView+GridView.builder, TabView will still be scrolled synchronously.
Content
https://github.com/fluttercandies/extended_nested_scroll_view/assets/82579833/ff157b34-35b3-4bfb-b7e8-249d8557d2f6
CodeSegment:
return ExtendedNestedScrollView(
onlyOneScrollInBody: true,
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return [
buildSliverAppBar(),
SliverToBoxAdapter(
child: Container(
color: Colors.red,
width: double.infinity,
height: 150,
child: const Center(child: Text('Carousel')),
),
),
buildSliverPersistentHeader(),
];
},
body: buildTabBarView(),
);
}
Widget buildTabBarView() {
return GetBuilder<FrontpageLogic>(
id: 'body',
builder: (logic) {
return logic.tags.isEmpty
? Center(
child: logic.login,
)
: Padding(
padding: const EdgeInsets.all(6.0),
child: TabBarView(
controller: logic.tabController,
children: logic.tags
.map((e) => ExtendedVisibilityDetector(
uniqueKey: Key(e.tag),
child: FrontCartoonListComponent(
tag: e.tag,
),
))
.toList(),
),
);
});
}
///首页动漫列表组件
class FrontCartoonListComponent extends StatefulWidget {
final String tag;
const FrontCartoonListComponent({Key? key, required this.tag})
: super(key: key);
@override
State<FrontCartoonListComponent> createState() =>
_FrontCartoonListComponentState();
}
class _FrontCartoonListComponentState extends State<FrontCartoonListComponent>
with AutomaticKeepAliveClientMixin {
List<AltagData>? data = [];
Widget login = const Login();
@override
void initState() {
// TODO: implement initState
super.initState();
getData();
}
@override
Widget build(BuildContext context) {
super.build(context);
return data!.isEmpty
? Center(child: login)
: ListView.builder(
itemCount: data?.length,
padding: const EdgeInsets.all(0),
itemBuilder: (ctx, i) {
return Card(
elevation: 1,
color: Colors.white,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
),
child: Column(
children: [
buildRow(i),
buildGridView(i),
const SizedBox(height: 20),
],
),
);
});
}
//分类标题
Widget buildRow(int i) {
var more = data?[i].more;
return Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
data?[i].title ?? '',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
color: Colors.black87,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
if (more != null) ...[
//更多 图标
IconButton(
onPressed: () {},
icon: const Icon(Icons.more_horiz),
),
]
],
),
);
}
//图片网格
Widget buildGridView(int i) {
return GridView.builder(
shrinkWrap: true,
padding: const EdgeInsets.all(0),
physics: const NeverScrollableScrollPhysics(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
childAspectRatio: 0.65,
crossAxisSpacing: 5,
mainAxisSpacing: 5,
// mainAxisExtent: 195,
),
itemCount: data?[i].list?.length ?? 0,
itemBuilder: (ctx, index) {
return Gridimage(
image: data?[i].list?[index].img,
title: data?[i].list?[index].title,
tag: data?[i].list?[index].tag,
url: data?[i].list?[index].url,
chapter: data?[i].list?[index].chapter,
);
},
);
}
//获取数据
Future<void> getData() async {
try {
AltagEntity? res = await Cartoon.getHome(widget.tag ?? '');
if (res != null) {
if (res.code != Cartoon.success) {
login = Text(res.msg ?? '');
}
data = res.data;
}
} catch (e) {
// log(e.toString(), name: 'CartoonErro');
login = Text(e.toString());
} finally {
setState(() {});
}
}
@override
void dispose() {
super.dispose();
}
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
}
FlutterViersion:
Flutter 3.10.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision f468f3366c (2 weeks ago) • 2023-07-12 15:19:05 -0700
Engine • revision cdbeda788a
Tools • Dart 3.0.6 • DevTools 2.23.1
PluginVersion:
extended_nested_scroll_view: ^6.1.2