Skip to content

Instantly share code, notes, and snippets.

@kherel
Created August 9, 2020 12:14
Show Gist options
  • Save kherel/94da2af1400266fc47aea2abf061f7f3 to your computer and use it in GitHub Desktop.
Save kherel/94da2af1400266fc47aea2abf061f7f3 to your computer and use it in GitHub Desktop.
Scrollable + ViewPort + SliverList
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: Colors.orange[50],
),
home: Scaffold(
appBar: AppBar(
title: Text('All products'),
),
body: SafeArea(
child: MyHomePage(),
),
),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return ProductsGrid();
}
}
class ProductsGrid extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(40),
bottomRight: Radius.circular(40),
),
child: Scrollable(
viewportBuilder: (_, viewportOffset) => Container(
color: Theme.of(context).primaryColor,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Flexible(
flex: 1,
child: Viewport(
cacheExtent: 1,
axisDirection: AxisDirection.down,
offset: viewportOffset,
slivers: [
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return Container(
color: Color((Random().nextDouble() * 0xFFFFFF)
.toInt())
.withOpacity(1.0),
height: 150.0);
},
),
),
],
),
),
Flexible(
flex: 1,
child: Viewport(
axisDirection: AxisDirection.down,
offset: viewportOffset,
slivers: [
SliverToBoxAdapter(
child: SizedBox(
height: 40,
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, int index) {
return Container(
color: Color((Random().nextDouble() * 0xFFFFFF)
.toInt())
.withOpacity(1.0),
height: 150.0);
},
),
),
],
),
),
],
),
),
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment