Skip to content

Instantly share code, notes, and snippets.

@FrazeColder
Created March 16, 2024 13:52
Show Gist options
  • Select an option

  • Save FrazeColder/486578f48833dd3d53b1f76080ac6f23 to your computer and use it in GitHub Desktop.

Select an option

Save FrazeColder/486578f48833dd3d53b1f76080ac6f23 to your computer and use it in GitHub Desktop.
CustomScrollView with multiple silvers, expand first Widget to bottom with key set
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: TestPage(),
));
}
class TestPage extends StatefulWidget {
const TestPage({super.key});
@override
State<TestPage> createState() => _TestPageState();
}
class _TestPageState extends State<TestPage> {
List<Widget> newList = List.generate(
20,
(index) => Text('Upper ${index.toString()}'),
);
List<Widget> myList = List.generate(
20,
(index) => Text('Lower ${index.toString()}'),
);
final Key centerKey = const ValueKey('second-sliver-list');
final scrollController = ScrollController();
final keyTop = GlobalKey();
@override
void dispose() {
scrollController.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
}
bool _isExpanded = false;
@override
Widget build(BuildContext context) {
String productDescription =
"Peanut butter, a creamy concoction crafted from roasted peanuts, is a timeless culinary delight cherished by millions worldwide. Its rich, nutty flavor and smooth texture make it a versatile ingredient that transcends traditional boundaries, finding its way into sandwiches, desserts, sauces, and even savory dishes. This delectable spread has captured the hearts and palates of food enthusiasts for generations, evolving from a humble pantry staple to a beloved icon of gastronomy. In this exploration, we delve deep into the captivating world of peanut butter, uncovering its history, culinary uses, nutritional benefits, and enduring appeal. Peanut butter, a creamy concoction crafted from roasted peanuts, is a timeless culinary delight cherished by millions worldwide. Its rich, nutty flavor and smooth texture make it a versatile ingredient that transcends traditional boundaries, finding its way into sandwiches, desserts, sauces, and even savory dishes. This delectable spread has captured the hearts and palates of food enthusiasts for generations, evolving from a humble pantry staple to a beloved icon of gastronomy. In this exploration, we delve deep into the captivating world of peanut butter, uncovering its history, culinary uses, nutritional benefits, and enduring appeal.";
return Scaffold(
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton.extended(
onPressed: () {
setState(() {
newList.add(Text('Upper ${newList.length}'));
});
},
label: const Text('Add to Upper'),
),
const SizedBox(height: 10),
FloatingActionButton.extended(
onPressed: () {
setState(() {
myList.add(Text('Lower ${newList.length}'));
});
},
label: const Text('Add to Lower'),
),
],
),
appBar: AppBar(),
body: CustomScrollView(
center: centerKey,
controller: scrollController,
slivers: [
SliverToBoxAdapter(
key: keyTop,
child: Column(
children: [
Image.network(
"https://m.media-amazon.com/images/I/719NQBiqh9L.jpg"),
SizedBox(height: 16.0),
Text(
'Product Name',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
SizedBox(height: 8.0),
Text(
'\$100', // Replace with actual price
style: TextStyle(fontSize: 16),
),
SizedBox(height: 16.0),
AnimatedContainer(
duration: Duration(milliseconds: 500),
curve: Curves.easeInOut,
height: _isExpanded
? null
: 85.0, // Adjust height to show around 3-4 lines
child: Text(
productDescription,
style: TextStyle(fontSize: 16),
overflow: TextOverflow.fade,
),
),
SizedBox(height: 16.0),
GestureDetector(
onTap: () {
setState(() {
_isExpanded = !_isExpanded;
});
},
child: Text(
_isExpanded ? 'Read less' : 'Read more',
style: TextStyle(color: Colors.blue),
),
),
],
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return ListTile(
title: newList[index],
);
},
childCount: newList.length,
),
),
SliverList(
key: centerKey,
delegate: SliverChildBuilderDelegate(
(context, index) {
return ListTile(title: myList[index]);
},
childCount: myList.length,
),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment