navigation_rail.extended_animation.0.dart 3.77 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flutter code sample for [NavigationRail.extendedAnimation].
6 7 8 9 10 11 12 13

import 'dart:ui';

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
14
  const MyApp({super.key});
15 16 17

  @override
  Widget build(BuildContext context) {
18
    return const MaterialApp(
19
      home: Scaffold(
20
        body: MyNavigationRail(),
21
      ),
22 23 24 25
    );
  }
}

26
class MyNavigationRail extends StatefulWidget {
27
  const MyNavigationRail({super.key});
28 29

  @override
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  State<MyNavigationRail> createState() => _MyNavigationRailState();
}

class _MyNavigationRailState extends State<MyNavigationRail> {
  int _selectedIndex = 0;
  bool _extended = false;

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        NavigationRail(
          selectedIndex: _selectedIndex,
          extended: _extended,
          leading: MyNavigationRailFab(onPressed: () {
            setState(() {
              _extended = !_extended;
            });
          }),
          onDestinationSelected: (int index) {
            setState(() {
              _selectedIndex = index;
            });
          },
          labelType: NavigationRailLabelType.none,
          destinations: const <NavigationRailDestination>[
            NavigationRailDestination(
              icon: Icon(Icons.favorite_border),
              selectedIcon: Icon(Icons.favorite),
              label: Text('First'),
            ),
            NavigationRailDestination(
              icon: Icon(Icons.bookmark_border),
              selectedIcon: Icon(Icons.book),
              label: Text('Second'),
            ),
            NavigationRailDestination(
              icon: Icon(Icons.star_border),
              selectedIcon: Icon(Icons.star),
              label: Text('Third'),
            ),
          ],
        ),
        const VerticalDivider(thickness: 1, width: 1),
        // This is the main content.
        Expanded(
          child: Center(
77 78 79 80 81 82 83 84
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text('Tap on FloatingActionButton to expand'),
                const SizedBox(height: 20),
                Text('selectedIndex: $_selectedIndex'),
              ],
            ),
85
          ),
86
        ),
87 88 89 90 91 92
      ],
    );
  }
}

class MyNavigationRailFab extends StatelessWidget {
93
  const MyNavigationRailFab({super.key, this.onPressed});
94 95 96 97

  final VoidCallback? onPressed;

  @override
98
  Widget build(BuildContext context) {
99
    final Animation<double> animation = NavigationRail.extendedAnimation(context);
100 101 102 103 104 105 106 107 108 109 110
    return AnimatedBuilder(
      animation: animation,
      builder: (BuildContext context, Widget? child) {
        // The extended fab has a shorter height than the regular fab.
        return Container(
          height: 56,
          padding: EdgeInsets.symmetric(
            vertical: lerpDouble(0, 6, animation.value)!,
          ),
          child: animation.value == 0
              ? FloatingActionButton(
111
                  onPressed: onPressed,
112
                  child: const Icon(Icons.add),
113 114 115 116 117 118 119 120 121
                )
              : Align(
                  alignment: AlignmentDirectional.centerStart,
                  widthFactor: animation.value,
                  child: Padding(
                    padding: const EdgeInsetsDirectional.only(start: 8),
                    child: FloatingActionButton.extended(
                      icon: const Icon(Icons.add),
                      label: const Text('CREATE'),
122
                      onPressed: onPressed,
123 124 125 126 127 128 129
                    ),
                  ),
                ),
        );
      },
    );
  }
130
}