aboutsummaryrefslogtreecommitdiffstats
path: root/lib/homescreen.dart
blob: dce45cda4e9ffc360426a3c80e3b89752f13b587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import 'package:flutter/material.dart';
import 'package:flutter_homescreen/page_dashboard.dart';
import 'package:flutter_homescreen/page_home.dart';
import 'package:flutter_homescreen/page_hvac.dart';
import 'package:flutter_homescreen/page_media.dart';
import 'package:flutter_homescreen/widget_clock.dart';

enum PageIndex { home, dashboard, hvac, media, demo3d }

class Homescreen extends StatefulWidget {
  Homescreen({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _HomescreenState createState() => _HomescreenState();
}

class _HomescreenState extends State<Homescreen> with TickerProviderStateMixin {
  int _selectedIndex = 0;
  int _previousIndex = 0;

  setNavigationIndex(int index) {
    setState(() {
      _previousIndex = _selectedIndex;
      _selectedIndex = index;
    });
  }

  Widget _childForIndex(int selectedIndex) {
    switch (PageIndex.values[selectedIndex]) {
      case PageIndex.home:
        return HomePage(
            key: ValueKey(selectedIndex),
            onSetNavigationIndex: setNavigationIndex);
      case PageIndex.dashboard:
        return DashboardPage(key: ValueKey(selectedIndex));
      case PageIndex.hvac:
        return HVACPage(key: ValueKey(selectedIndex));
      case PageIndex.media:
        return MediaPage(key: ValueKey(selectedIndex));
      case PageIndex.demo3d:
        return Text('3D demo');
      default:
        return Text('Undefined');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.deepPurple.shade50,
        child: Center(
            child: LayoutBuilder(
          builder: _buildLayout,
        )));
  }

  Widget _buildLayout(BuildContext context, BoxConstraints constraints) {
    // size the icons so they cover the left edge of the screen
    var iconSize = constraints.maxHeight / (PageIndex.values.length + 2);
    var railSize = constraints.maxHeight / (PageIndex.values.length + 1);

    return Scaffold(
      body: Row(
        children: <Widget>[
          Container(
            decoration: BoxDecoration(
                gradient: LinearGradient(
                    begin: Alignment.centerLeft,
                    end: Alignment.centerRight,
                    colors: [
                  Colors.blueGrey.shade800,
                  Colors.blueGrey.shade900
                ])),
            child: Stack(
              children: [
                NavigationRail(
                  backgroundColor: Colors.transparent,
                  selectedIndex: _selectedIndex,
                  groupAlignment: -1.0,
                  minWidth: railSize,
                  // leading widget?
                  // leading: Icon(Icons.house_outlined, size: iconSize),
                  // trailing widget does not expand to bottom
                  onDestinationSelected: (int index) {
                    setNavigationIndex(index);
                  },
                  selectedIconTheme: IconTheme.of(context).copyWith(
                    size: iconSize,
                    color: Colors.orangeAccent.shade100,
                  ),
                  unselectedIconTheme: IconTheme.of(context).copyWith(
                    size: iconSize,
                    color: Colors.blueGrey.shade400,
                  ),
                  labelType: NavigationRailLabelType.none,
                  destinations: <NavigationRailDestination>[
                    NavigationRailDestination(
                      icon: Icon(Icons.house),
                      selectedIcon: Icon(Icons.house),
                      label: Text('Home'),
                    ),
                    NavigationRailDestination(
                      icon: Icon(Icons.drive_eta),
                      selectedIcon: Icon(Icons.drive_eta),
                      label: Text('Dashboard'),
                    ),
                    NavigationRailDestination(
                      icon: Icon(Icons.thermostat),
                      selectedIcon: Icon(Icons.thermostat),
                      label: Text('HVAC'),
                    ),
                    NavigationRailDestination(
                      icon: Icon(Icons.music_note),
                      selectedIcon: Icon(Icons.music_note),
                      label: Text('Media'),
                    ),
                    NavigationRailDestination(
                      icon: Icon(Icons.view_in_ar),
                      selectedIcon: Icon(Icons.view_in_ar),
                      label: Text('3D example'),
                    ),
                  ],
                ),
                Positioned(
                  bottom: 0,
                  left: 0,
                  right: 0,
                  // This is the info widget with time, date, etc.
                  child: ClockWiddget(
                    size: railSize,
                    textColor: Colors.blueGrey.shade100,
                  ),
                )
              ],
            ),
          ),
          VerticalDivider(
            thickness: 1,
            width: 1,
            color: Colors.grey.shade900,
          ),
          // This is the main content.
          Expanded(
            // TODO This solution adds a nice animation but loses the state
            // of the old page whenever a new one comes in. We could use
            // IndexedStack to keep the state of each page, at the cost of not
            // having nice animations between pages. Another option could be to
            // move the state of each page upwards in the tree.
            // See also: https://docs.flutter.dev/development/data-and-backend/state-mgmt/options
            child: AnimatedSwitcher(
              duration: const Duration(milliseconds: 500),
              reverseDuration: const Duration(milliseconds: 500),
              switchInCurve: Curves.easeInOut,
              switchOutCurve: Curves.easeInOut,
              transitionBuilder: (Widget child, Animation<double> animation) {
                if (child.key != ValueKey(_selectedIndex)) {
                  return FadeTransition(
                    opacity:
                        Tween<double>(begin: 1.0, end: 1.0).animate(animation),
                    child: child,
                  );
                }
                Offset beginOffset = new Offset(
                    0.0, (_selectedIndex > _previousIndex ? 1.0 : -1.0));
                return SlideTransition(
                  position: Tween<Offset>(begin: beginOffset, end: Offset.zero)
                      .animate(animation),
                  child: FadeTransition(
                    opacity: Tween<double>(begin: 0.0, end: 1.0).animate(
                      CurvedAnimation(
                        parent: animation,
                        curve: Interval(0.5, 1.0),
                      ),
                    ),
                    child: child,
                  ),
                );
              },
              child: _childForIndex(_selectedIndex),
            ),
          )
        ],
      ),
    );
  }
}