Skip to content

Instantly share code, notes, and snippets.

@alexcmgit
Created February 7, 2023 19:07
Show Gist options
  • Save alexcmgit/60cb0fa073975f3c80660815ae88af4e to your computer and use it in GitHub Desktop.
Save alexcmgit/60cb0fa073975f3c80660815ae88af4e to your computer and use it in GitHub Desktop.
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late ScrollController _scrollController;
@override
void initState() {
super.initState();
_scrollController = ScrollController();
}
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: const Text('Go to top'),
onPressed: () => _scrollController.animateTo(
0,
duration: const Duration(seconds: 2),
curve: Curves.easeInOut,
),
),
body: CustomScrollView(
// Controller: attached to both:
// - SliverAppBar and SliverList.
// + any other sliver you provide in the [slivers] param.
controller: _scrollController,
slivers: [
const SliverAppBar(expandedHeight: 400),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return Center(child: Text('item $index.'));
},
),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment