Skip to content

Commit fd80e37

Browse files
committed
feat: Page setup. #1
1 parent 482348d commit fd80e37

File tree

2 files changed

+126
-3
lines changed

2 files changed

+126
-3
lines changed

README.md

+122-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
- [_How_? 👩‍💻](#how-)
2121
- [Prerequisites? 📝](#prerequisites-)
2222
- [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)
2325

2426

2527
# Why? 🤷‍
@@ -84,4 +86,123 @@ this output on the terminal.
8486
```
8587

8688
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+

lib/main.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import 'package:flutter/material.dart';
22

3-
/// Flutter code sample for [BottomNavigationBar].
4-
53
void main() => runApp(const App());
64

5+
/// App class
76
class App extends StatelessWidget {
87
const App({super.key});
98

@@ -15,6 +14,7 @@ class App extends StatelessWidget {
1514
}
1615
}
1716

17+
/// HomePage with BottomBarNavigation
1818
class HomePage extends StatefulWidget {
1919
const HomePage({super.key});
2020

@@ -25,6 +25,7 @@ class HomePage extends StatefulWidget {
2525
class _HomePageState extends State<HomePage> {
2626
int _selectedIndex = 0;
2727

28+
/// List of pages
2829
final List<Widget> _pages = <Widget>[
2930
const Text(
3031
'Material widget',
@@ -36,6 +37,7 @@ class _HomePageState extends State<HomePage> {
3637
)
3738
];
3839

40+
/// Callback function that changes the index to show the selected page
3941
void _onItemTapped(int index) {
4042
setState(() {
4143
_selectedIndex = index;

0 commit comments

Comments
 (0)