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 [Scaffold.floatingActionButtonAnimator].
8
9void main() => runApp(const ScaffoldFloatingActionButtonAnimatorApp());
10
11class ScaffoldFloatingActionButtonAnimatorApp extends StatelessWidget {
12 const ScaffoldFloatingActionButtonAnimatorApp({super.key});
13
14 @override
15 Widget build(BuildContext context) {
16 return const MaterialApp(
17 home: ScaffoldFloatingActionButtonAnimatorExample(),
18 );
19 }
20}
21
22enum FabAnimator { defaultStyle, none }
23const List<(FabAnimator, String)> fabAnimatoregments = <(FabAnimator, String)>[
24 (FabAnimator.defaultStyle, 'Default'),
25 (FabAnimator.none, 'None'),
26];
27
28enum FabLocation { centerFloat, endFloat, endTop }
29const List<(FabLocation, String)> fabLocationegments = <(FabLocation, String)>[
30 (FabLocation.centerFloat, 'centerFloat'),
31 (FabLocation.endFloat, 'endFloat'),
32 (FabLocation.endTop, 'endTop'),
33];
34
35class ScaffoldFloatingActionButtonAnimatorExample extends StatefulWidget {
36 const ScaffoldFloatingActionButtonAnimatorExample({super.key});
37
38 @override
39 State<ScaffoldFloatingActionButtonAnimatorExample> createState() => _ScaffoldFloatingActionButtonAnimatorExampleState();
40}
41
42class _ScaffoldFloatingActionButtonAnimatorExampleState extends State<ScaffoldFloatingActionButtonAnimatorExample> {
43 Set<FabAnimator> _selectedFabAnimator = <FabAnimator>{FabAnimator.defaultStyle};
44 Set<FabLocation> _selectedFabLocation = <FabLocation>{FabLocation.endFloat};
45 FloatingActionButtonAnimator? _floatingActionButtonAnimator;
46 FloatingActionButtonLocation? _floatingActionButtonLocation;
47 bool _showFab = false;
48
49 @override
50 Widget build(BuildContext context) {
51 return Scaffold(
52 floatingActionButtonLocation: _floatingActionButtonLocation,
53 floatingActionButtonAnimator: _floatingActionButtonAnimator,
54 appBar: AppBar(title: const Text('FloatingActionButtonAnimator Sample')),
55 body: Center(
56 child: Column(
57 mainAxisAlignment: MainAxisAlignment.center,
58 children: <Widget>[
59 SegmentedButton<FabAnimator>(
60 selected: _selectedFabAnimator,
61 onSelectionChanged: (Set<FabAnimator> styles) {
62 setState(() {
63 _floatingActionButtonAnimator = switch (styles.first) {
64 FabAnimator.defaultStyle => null,
65 FabAnimator.none => FloatingActionButtonAnimator.noAnimation,
66 };
67 _selectedFabAnimator = styles;
68 });
69 },
70 segments: fabAnimatoregments
71 .map<ButtonSegment<FabAnimator>>(((FabAnimator, String) fabAnimator) {
72 final FabAnimator animator = fabAnimator.$1;
73 final String label = fabAnimator.$2;
74 return ButtonSegment<FabAnimator>(value: animator, label: Text(label));
75 })
76 .toList(),
77 ),
78 const SizedBox(height: 10),
79 SegmentedButton<FabLocation>(
80 selected: _selectedFabLocation,
81 onSelectionChanged: (Set<FabLocation> styles) {
82 setState(() {
83 _floatingActionButtonLocation = switch (styles.first) {
84 FabLocation.centerFloat => FloatingActionButtonLocation.centerFloat,
85 FabLocation.endFloat => FloatingActionButtonLocation.endFloat,
86 FabLocation.endTop => FloatingActionButtonLocation.endTop,
87 };
88 _selectedFabLocation = styles;
89 });
90 },
91 segments: fabLocationegments
92 .map<ButtonSegment<FabLocation>>(((FabLocation, String) fabLocation) {
93 final FabLocation location = fabLocation.$1;
94 final String label = fabLocation.$2;
95 return ButtonSegment<FabLocation>(value: location, label: Text(label));
96 })
97 .toList(),
98 ),
99 const SizedBox(height: 10),
100 FilledButton.icon(
101 onPressed: () {
102 setState(() {
103 _showFab = !_showFab;
104 });
105 },
106 icon: Icon(_showFab ? Icons.visibility_off : Icons.visibility),
107 label: const Text('Toggle FAB'),
108 ),
109 ],
110 ),
111 ),
112 floatingActionButton: !_showFab
113 ? null
114 : FloatingActionButton(
115 onPressed: () {},
116 child: const Icon(Icons.add),
117 ),
118 );
119 }
120}
121

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com