|
20 | 20 | - [_How_? 👩💻](#how-)
|
21 | 21 | - [Prerequisites? 📝](#prerequisites-)
|
22 | 22 | - [0. Project setup](#0-project-setup)
|
| 23 | + - [1. Setting up both pages](#1-setting-up-both-pages) |
| 24 | + - [2. `Material` Date/Time Pickers](#2-material-datetime-pickers) |
23 | 25 |
|
24 | 26 |
|
25 | 27 | # Why? 🤷
|
@@ -84,4 +86,123 @@ this output on the terminal.
|
84 | 86 | ```
|
85 | 87 |
|
86 | 88 | This means everything is correctly setup!
|
87 |
| -We are ready to start implementing! |
| 89 | +We are ready to start implementing! |
| 90 | + |
| 91 | + |
| 92 | +## 1. Setting up both pages |
| 93 | + |
| 94 | +Before adding our date and time pickers, |
| 95 | +we are going to add two different pages: |
| 96 | + |
| 97 | +- the first page will showcase the implementation |
| 98 | +of the official `DatePicker` and `TimePicker`, |
| 99 | +the official Material widgets that are commonly used in `Flutter` apps. |
| 100 | +- the second page will pertain to **inline pickers**, |
| 101 | +meaning there won't be any dialogue/modals showing up |
| 102 | +and hijacking the screen |
| 103 | +whenever a person wishes to change date/time. |
| 104 | + |
| 105 | +> **Note:** |
| 106 | +> |
| 107 | +> If you want to know *why* we're wanting to |
| 108 | +> **not** use modals, |
| 109 | +> visit https://github.com/dwyl/product-ux-research/issues/38 for more context. |
| 110 | +
|
| 111 | +With this in mind, |
| 112 | +let's set up a [`BottomNavigationBar`](https://api.flutter.dev/flutter/material/BottomNavigationBar-class.html) |
| 113 | +to toggle between these two scenarios. |
| 114 | +In `lib/main.dart`, |
| 115 | +change it to: |
| 116 | + |
| 117 | +```dart |
| 118 | +import 'package:flutter/material.dart'; |
| 119 | +
|
| 120 | +void main() => runApp(const App()); |
| 121 | +
|
| 122 | +/// App class |
| 123 | +class App extends StatelessWidget { |
| 124 | + const App({super.key}); |
| 125 | +
|
| 126 | + @override |
| 127 | + Widget build(BuildContext context) { |
| 128 | + return const MaterialApp( |
| 129 | + home: HomePage(), |
| 130 | + ); |
| 131 | + } |
| 132 | +} |
| 133 | +
|
| 134 | +/// HomePage with BottomBarNavigation |
| 135 | +class HomePage extends StatefulWidget { |
| 136 | + const HomePage({super.key}); |
| 137 | +
|
| 138 | + @override |
| 139 | + State<HomePage> createState() => _HomePageState(); |
| 140 | +} |
| 141 | +
|
| 142 | +class _HomePageState extends State<HomePage> { |
| 143 | + int _selectedIndex = 0; |
| 144 | +
|
| 145 | + /// List of pages |
| 146 | + final List<Widget> _pages = <Widget>[ |
| 147 | + const Text( |
| 148 | + 'Material widget', |
| 149 | + style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold), |
| 150 | + ), |
| 151 | + const Text( |
| 152 | + 'Inline widget', |
| 153 | + style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold), |
| 154 | + ) |
| 155 | + ]; |
| 156 | +
|
| 157 | + /// Callback function that changes the index to show the selected page |
| 158 | + void _onItemTapped(int index) { |
| 159 | + setState(() { |
| 160 | + _selectedIndex = index; |
| 161 | + }); |
| 162 | + } |
| 163 | +
|
| 164 | + @override |
| 165 | + Widget build(BuildContext context) { |
| 166 | + return Scaffold( |
| 167 | + appBar: AppBar( |
| 168 | + title: const Text('Flutter Date Time Pickers'), |
| 169 | + ), |
| 170 | + body: Center( |
| 171 | + child: _pages.elementAt(_selectedIndex), |
| 172 | + ), |
| 173 | + bottomNavigationBar: BottomNavigationBar( |
| 174 | + items: const <BottomNavigationBarItem>[ |
| 175 | + BottomNavigationBarItem( |
| 176 | + icon: Icon(Icons.adjust), |
| 177 | + label: 'Material', |
| 178 | + ), |
| 179 | + BottomNavigationBarItem( |
| 180 | + icon: Icon(Icons.border_color), |
| 181 | + label: 'Inline', |
| 182 | + ), |
| 183 | + ], |
| 184 | + currentIndex: _selectedIndex, |
| 185 | + selectedItemColor: Colors.amber[800], |
| 186 | + onTap: _onItemTapped, |
| 187 | + ), |
| 188 | + ); |
| 189 | + } |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +Whenever the person taps on either `BottomNavigationBar` item, |
| 194 | +they change the index of the page being shown |
| 195 | +(this is handled by `_onItemTapped`). |
| 196 | +If you run the app, |
| 197 | +you should see this in action. |
| 198 | + |
| 199 | +<p align='center'> |
| 200 | + <img width="250" src="https://github.com/dwyl/flutter-date-time-tutorial/assets/17494745/9d960e8d-0814-412e-9e9b-7f150683cc6d"> |
| 201 | +</p> |
| 202 | + |
| 203 | +Awesome! |
| 204 | +Now we're ready to rock 🎸! |
| 205 | + |
| 206 | + |
| 207 | +## 2. `Material` Date/Time Pickers |
| 208 | + |
0 commit comments