|
1 | 1 | // FLUTTER / DART / THIRD-PARTIES |
| 2 | +import 'package:connectivity_plus/connectivity_plus.dart'; |
2 | 3 | import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; |
| 5 | + |
| 6 | +// UTILS |
| 7 | +import 'package:notredame/core/utils/utils.dart'; |
3 | 8 | import 'package:notredame/ui/utils/loading.dart'; |
| 9 | +import 'package:notredame/ui/utils/app_theme.dart'; |
4 | 10 |
|
5 | 11 | // WIDGETS |
6 | 12 | import 'package:notredame/ui/widgets/bottom_bar.dart'; |
7 | 13 |
|
8 | 14 | /// Basic Scaffold to avoid boilerplate code in the application. |
9 | 15 | /// Contains a loader controlled by [_isLoading] |
10 | | -class BaseScaffold extends StatelessWidget { |
| 16 | +class BaseScaffold extends StatefulWidget { |
11 | 17 | final AppBar appBar; |
12 | 18 |
|
13 | 19 | final Widget body; |
@@ -35,25 +41,77 @@ class BaseScaffold extends StatelessWidget { |
35 | 41 | _isLoading = isLoading, |
36 | 42 | _isInteractionLimitedWhileLoading = isInteractionLimitedWhileLoading; |
37 | 43 |
|
| 44 | + @override |
| 45 | + _BaseScaffoldState createState() => _BaseScaffoldState(); |
| 46 | +} |
| 47 | + |
| 48 | +class _BaseScaffoldState extends State<BaseScaffold> { |
| 49 | + // Displays text under the app bar when offline. |
| 50 | + static bool _isOffline = false; |
| 51 | + |
| 52 | + @override |
| 53 | + void initState() { |
| 54 | + super.initState(); |
| 55 | + _setOfflineValue(); |
| 56 | + _listenToChangeInConnectivity(); |
| 57 | + } |
| 58 | + |
| 59 | + Future _setOfflineValue() async { |
| 60 | + final isOffline = |
| 61 | + await Connectivity().checkConnectivity() == ConnectivityResult.none; |
| 62 | + setState(() { |
| 63 | + _isOffline = isOffline; |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + void _listenToChangeInConnectivity() { |
| 68 | + Connectivity().onConnectivityChanged.listen((event) { |
| 69 | + setState(() { |
| 70 | + _isOffline = event == ConnectivityResult.none; |
| 71 | + }); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
38 | 75 | @override |
39 | 76 | Widget build(BuildContext context) => Scaffold( |
40 | | - appBar: appBar, |
41 | | - body: SafeArea( |
42 | | - top: false, |
43 | | - child: Stack( |
44 | | - children: [ |
45 | | - body, |
46 | | - if (_isLoading) |
47 | | - buildLoading( |
48 | | - isInteractionLimitedWhileLoading: |
49 | | - _isInteractionLimitedWhileLoading) |
50 | | - else |
51 | | - const SizedBox() |
52 | | - ], |
| 77 | + body: Scaffold( |
| 78 | + appBar: widget.appBar, |
| 79 | + body: SafeArea( |
| 80 | + top: false, |
| 81 | + child: Stack( |
| 82 | + children: [ |
| 83 | + widget.body, |
| 84 | + if (widget._isLoading) |
| 85 | + buildLoading( |
| 86 | + isInteractionLimitedWhileLoading: |
| 87 | + widget._isInteractionLimitedWhileLoading) |
| 88 | + else |
| 89 | + const SizedBox() |
| 90 | + ], |
| 91 | + ), |
53 | 92 | ), |
| 93 | + bottomNavigationBar: widget._showBottomBar ? BottomBar() : null, |
| 94 | + floatingActionButton: widget.fab, |
| 95 | + floatingActionButtonLocation: widget.fabPosition, |
54 | 96 | ), |
55 | | - bottomNavigationBar: _showBottomBar ? BottomBar() : null, |
56 | | - floatingActionButton: fab, |
57 | | - floatingActionButtonLocation: fabPosition, |
| 97 | + bottomNavigationBar: _isOffline ? buildOfflineBar(context) : null, |
58 | 98 | ); |
| 99 | + |
| 100 | + Widget buildOfflineBar(BuildContext context) { |
| 101 | + return Stack( |
| 102 | + alignment: Alignment.center, |
| 103 | + children: [ |
| 104 | + Container( |
| 105 | + color: Utils.getColorByBrightness(context, |
| 106 | + AppTheme.lightThemeBackground, AppTheme.darkThemeBackground), |
| 107 | + width: MediaQuery.of(context).size.width, |
| 108 | + height: MediaQuery.of(context).size.height / 30, |
| 109 | + ), |
| 110 | + Text( |
| 111 | + AppIntl.of(context).no_connectivity, |
| 112 | + textAlign: TextAlign.center, |
| 113 | + ), |
| 114 | + ], |
| 115 | + ); |
| 116 | + } |
59 | 117 | } |
0 commit comments