aboutsummaryrefslogtreecommitdiff
path: root/lib/widgets/custom_drawer_widget.dart
blob: 671ff704b6fd6445f8d4d06ad51d4cc253a56957 (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
import 'package:flutter/material.dart';

import 'package:kulinar_app/constants.dart';
import 'package:kulinar_app/views/info_view.dart';
import 'package:kulinar_app/views/main_view.dart';
import 'package:kulinar_app/views/vote_view.dart';
import 'package:kulinar_app/views/week_view.dart';
import 'package:kulinar_app/views/shoplist_view.dart';
import 'package:kulinar_app/views/settings_view.dart';
import 'package:kulinar_app/views/favorites_view.dart';
import 'package:kulinar_app/models/data/settings_data_class.dart';
import 'package:kulinar_app/widgets/page_route_transitions.dart';

import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class CustomDrawer extends StatefulWidget {
  const CustomDrawer({Key? key, required this.initialIndex}) : super(key: key);

  final int initialIndex;

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

class CustomDrawerState extends State<CustomDrawer> {
  int? _index;

  @override
  void initState() {
    super.initState();
    _index = widget.initialIndex;
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: Column(
        children: [
          Expanded(
            child: Column(
              children: [
                _buildDrawerHeader(),
                _buildDrawerItem(0, Icons.receipt_rounded, AppLocalizations.of(context)!.category1, () {
                  _navigateTo(const MainView(), 0);
                }),
                _buildDrawerItem(1, Icons.favorite_rounded, AppLocalizations.of(context)!.category4, () {
                  _navigateTo(const FavoritesView(), 1);
                }),
                _buildDrawerItem(2, Icons.calendar_today_rounded, AppLocalizations.of(context)!.category5, () {
                  _navigateTo(const WeekView(), 2);
                }),
                _buildDrawerItem(3, Icons.how_to_vote_rounded, AppLocalizations.of(context)!.category6, () {
                  _navigateTo(const VoteView(), 3);
                }),
                _buildDrawerItem(4, Icons.shopping_cart_rounded, AppLocalizations.of(context)!.category7, () {
                  _navigateTo(const ShoplistView(), 4);
                }),
              ],
            ),
          ),
          _buildDrawerItem(5, Icons.settings_rounded, AppLocalizations.of(context)!.category8, () {
            _navigateTo(const SettingsView(), 5);
          }),
          _buildDrawerItem(6, Icons.info_rounded, AppLocalizations.of(context)!.category9, () {
            _navigateTo(const InfoView(), 6);
          }),
        ],
      ),
    );
  }

  Widget _buildDrawerHeader() {
    return const SizedBox(
      width: double.infinity,
      child: DrawerHeader(
        margin: EdgeInsets.zero,
        decoration: BoxDecoration(color: cPrimaryColor),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.only(bottom: 12.0),
              child: Icon(
                Icons.restaurant_menu_rounded,
                color: cIconColor,
                size: 100.0,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildDrawerItem(int index, IconData icon, String title, Function callback) {
    Color color = cPassiveDrawerColor;

    if (index == _index) {
      color = cPrimaryColor;
    }

    if ((index == 2 || index == 3) && SettingsData.settings["serverURL"] == "") return Container();

    return ListTile(
      leading: Icon(icon, color: color),
      title: Text(title, style: cDrawerTextStyle.copyWith(color: color)),
      onTap: () {
        callback();
      },
    );
  }

  void _navigateTo(Widget route, int index) async {
    _index = index;

    Navigator.pop(context);

    await Navigator.pushReplacement(
      context,
      FadeRoute(child: route),
    );
  }
}