Skip to content

Commit 4a453b0

Browse files
Merge pull request #4 from Sasikumar3595/master
FLUT-4313-Sample updated with null safety.
2 parents 86e99cb + 4a1e434 commit 4a453b0

File tree

4 files changed

+96
-208
lines changed

4 files changed

+96
-208
lines changed

README.md

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,11 @@
33
In flutter date range picker, you can get the current month start and end dates using the `onViewChanged` callback.
44

55
## Step 1:
6-
In initState(), initialize the controller for the date range picker and set the default values for start and end dates of the current month.
6+
Inside the state, initialize the controller for the date range picker and set the default values for start and end date of the current month.
77

88
```xml
9-
DateRangePickerController _controller;
10-
String _startDate, _endDate;
11-
12-
@override
13-
void initState() {
14-
// TODO: implement initState
15-
_controller = DateRangePickerController();
16-
_startDate = '';
17-
_endDate = '';
18-
super.initState();
19-
}
9+
final DateRangePickerController _controller = DateRangePickerController();
10+
String _startDate = '', _endDate = '';
2011
```
2112

2213
## Step 2:
@@ -48,12 +39,12 @@ Using the `onViewChanged` callback of the date picker get the start and dates of
4839
void viewChanged(DateRangePickerViewChangedArgs args) {
4940

5041
_startDate = DateFormat('dd, MMMM yyyy')
51-
.format(args.visibleDateRange.startDate)
42+
.format(args.visibleDateRange.startDate!)
5243
.toString();
5344
_endDate=DateFormat('dd, MMMM yyyy')
54-
.format(args.visibleDateRange.endDate)
45+
.format(args.visibleDateRange.endDate!)
5546
.toString();
56-
SchedulerBinding.instance.addPostFrameCallback((duration) {
47+
SchedulerBinding.instance!.addPostFrameCallback((duration) {
5748
setState(() {});
5849
});
5950
}

lib/main.dart

Lines changed: 49 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -21,77 +21,64 @@ class PickerCurrentMonth extends StatefulWidget {
2121
_PickerCurrentMonthState createState() => _PickerCurrentMonthState();
2222
}
2323

24-
List<String> views = <String>['Month', 'Year', 'Decade', 'Century'];
25-
2624
class _PickerCurrentMonthState extends State<PickerCurrentMonth> {
27-
DateRangePickerController _controller;
28-
String _startDate, _endDate;
29-
30-
@override
31-
void initState() {
32-
// TODO: implement initState
33-
_controller = DateRangePickerController();
34-
_startDate = '';
35-
_endDate = '';
36-
super.initState();
37-
}
25+
final DateRangePickerController _controller = DateRangePickerController();
26+
final List<String> views = <String>['Month', 'Year', 'Decade', 'Century'];
27+
String _startDate = '', _endDate = '';
3828

3929
@override
4030
Widget build(BuildContext context) {
4131
return Scaffold(
42-
appBar: AppBar(
43-
leading: PopupMenuButton<String>(
44-
icon: Icon(Icons.calendar_today),
45-
itemBuilder: (BuildContext context) => views.map((String choice) {
46-
return PopupMenuItem<String>(
47-
value: choice,
48-
child: Text(choice),
49-
);
50-
}).toList(),
51-
onSelected: (String value) {
52-
if (value == 'Month') {
53-
_controller.view = DateRangePickerView.month;
54-
} else if (value == 'Year') {
55-
_controller.view = DateRangePickerView.year;
56-
} else if (value == 'Decade') {
57-
_controller.view = DateRangePickerView.decade;
58-
} else if (value == 'Century') {
59-
_controller.view = DateRangePickerView.century;
60-
}
61-
},
62-
),
63-
),
64-
body: Column(
65-
children: <Widget>[
66-
Container( height:50,
67-
child: Text('StartDate:''$_startDate')),
68-
Container(height:50
69-
,child: Text('EndDate:''$_endDate')),
70-
Card(
71-
margin: const EdgeInsets.fromLTRB(40, 100, 50, 40),
72-
child: SfDateRangePicker(
73-
controller: _controller,
74-
view: DateRangePickerView.month,
75-
onViewChanged: viewChanged,
76-
),
77-
)
78-
],
32+
appBar: AppBar(
33+
leading: PopupMenuButton<String>(
34+
icon: Icon(Icons.calendar_today),
35+
itemBuilder: (BuildContext context) => views.map((String choice) {
36+
return PopupMenuItem<String>(
37+
value: choice,
38+
child: Text(choice),
39+
);
40+
}).toList(),
41+
onSelected: (String value) {
42+
if (value == 'Month') {
43+
_controller.view = DateRangePickerView.month;
44+
} else if (value == 'Year') {
45+
_controller.view = DateRangePickerView.year;
46+
} else if (value == 'Decade') {
47+
_controller.view = DateRangePickerView.decade;
48+
} else if (value == 'Century') {
49+
_controller.view = DateRangePickerView.century;
50+
}
51+
},
7952
),
53+
),
54+
body: Column(
55+
children: <Widget>[
56+
Container(height: 50, child: Text('StartDate:' '$_startDate')),
57+
Container(height: 50, child: Text('EndDate:' '$_endDate')),
58+
Card(
59+
margin: const EdgeInsets.fromLTRB(40, 100, 50, 40),
60+
child: SfDateRangePicker(
61+
controller: _controller,
62+
view: DateRangePickerView.month,
63+
onViewChanged: viewChanged,
64+
),
65+
)
66+
],
67+
),
8068

81-
// This trailing comma makes auto-formatting nicer for build methods.
82-
);
69+
// This trailing comma makes auto-formatting nicer for build methods.
70+
);
8371
}
8472

8573
void viewChanged(DateRangePickerViewChangedArgs args) {
86-
87-
_startDate = DateFormat('dd, MMMM yyyy')
88-
.format(args.visibleDateRange.startDate)
89-
.toString();
90-
_endDate=DateFormat('dd, MMMM yyyy')
91-
.format(args.visibleDateRange.endDate)
92-
.toString();
93-
SchedulerBinding.instance.addPostFrameCallback((duration) {
94-
setState(() {});
95-
});
74+
_startDate = DateFormat('dd, MMMM yyyy')
75+
.format(args.visibleDateRange.startDate!)
76+
.toString();
77+
_endDate = DateFormat('dd, MMMM yyyy')
78+
.format(args.visibleDateRange.endDate!)
79+
.toString();
80+
SchedulerBinding.instance!.addPostFrameCallback((duration) {
81+
setState(() {});
82+
});
9683
}
9784
}

0 commit comments

Comments
 (0)