1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/material.dart';
6
7/// Flutter code sample for [SliverAnimatedOpacity].
8
9void main() => runApp(const SliverAnimatedOpacityExampleApp());
10
11class SliverAnimatedOpacityExampleApp extends StatelessWidget {
12 const SliverAnimatedOpacityExampleApp({super.key});
13
14 static const Duration duration = Duration(milliseconds: 500);
15 static const Curve curve = Curves.easeInOut;
16
17 @override
18 Widget build(BuildContext context) {
19 return MaterialApp(
20 home: Scaffold(
21 appBar: AppBar(title: const Text('SliverAnimatedOpacity Sample')),
22 body: const Center(
23 child: SliverAnimatedOpacityExample(duration: duration, curve: curve),
24 ),
25 ),
26 );
27 }
28}
29
30class SliverAnimatedOpacityExample extends StatefulWidget {
31 const SliverAnimatedOpacityExample({required this.duration, required this.curve, super.key});
32
33 final Duration duration;
34
35 final Curve curve;
36
37 @override
38 State<SliverAnimatedOpacityExample> createState() => _SliverAnimatedOpacityExampleState();
39}
40
41class _SliverAnimatedOpacityExampleState extends State<SliverAnimatedOpacityExample>
42 with SingleTickerProviderStateMixin {
43 bool _visible = true;
44
45 @override
46 Widget build(BuildContext context) {
47 return CustomScrollView(
48 slivers: <Widget>[
49 SliverAnimatedOpacity(
50 opacity: _visible ? 1.0 : 0.0,
51 duration: widget.duration,
52 curve: widget.curve,
53 sliver: SliverFixedExtentList(
54 itemExtent: 100.0,
55 delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
56 return Container(color: index.isEven ? Colors.indigo[200] : Colors.orange[200]);
57 }, childCount: 5),
58 ),
59 ),
60 SliverToBoxAdapter(
61 child: FloatingActionButton(
62 onPressed: () {
63 setState(() {
64 _visible = !_visible;
65 });
66 },
67 tooltip: 'Toggle opacity',
68 child: const Icon(Icons.flip),
69 ),
70 ),
71 ],
72 );
73 }
74}
75