Extract Sample code into examples/api (#87280)
This extracts the sample code out from the API doc comments, and places them in separate files on disk, allowing running of the examples locally, testing them, and building of slightly larger examples.
This commit is contained in:
132
examples/api/lib/material/date_picker/show_date_picker.0.dart
Normal file
132
examples/api/lib/material/date_picker/show_date_picker.0.dart
Normal file
@@ -0,0 +1,132 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Template: dev/snippets/config/templates/stateful_widget_restoration_material.tmpl
|
||||
//
|
||||
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
|
||||
// of samples, and may be ignored if you are just exploring the sample.
|
||||
|
||||
// Flutter code sample for showDatePicker
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// This sample demonstrates how to create a restorable Material date picker.
|
||||
// This is accomplished by enabling state restoration by specifying
|
||||
// [MaterialApp.restorationScopeId] and using [Navigator.restorablePush] to
|
||||
// push [DatePickerDialog] when the button is tapped.
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//***************************************************************************
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
|
||||
/// This is the main application widget.
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
static const String _title = 'Flutter Code Sample';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
restorationScopeId: 'app',
|
||||
title: _title,
|
||||
home: MyStatefulWidget(restorationId: 'main'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// This is the stateful widget that the main application instantiates.
|
||||
class MyStatefulWidget extends StatefulWidget {
|
||||
const MyStatefulWidget({Key? key, this.restorationId}) : super(key: key);
|
||||
|
||||
final String? restorationId;
|
||||
|
||||
@override
|
||||
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
|
||||
}
|
||||
|
||||
/// This is the private State class that goes with MyStatefulWidget.
|
||||
/// RestorationProperty objects can be used because of RestorationMixin.
|
||||
class _MyStatefulWidgetState extends State<MyStatefulWidget>
|
||||
with RestorationMixin {
|
||||
// In this example, the restoration ID for the mixin is passed in through
|
||||
// the [StatefulWidget]'s constructor.
|
||||
@override
|
||||
String? get restorationId => widget.restorationId;
|
||||
|
||||
//********************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
final RestorableDateTime _selectedDate =
|
||||
RestorableDateTime(DateTime(2021, 7, 25));
|
||||
late final RestorableRouteFuture<DateTime?> _restorableDatePickerRouteFuture =
|
||||
RestorableRouteFuture<DateTime?>(
|
||||
onComplete: _selectDate,
|
||||
onPresent: (NavigatorState navigator, Object? arguments) {
|
||||
return navigator.restorablePush(
|
||||
_datePickerRoute,
|
||||
arguments: _selectedDate.value.millisecondsSinceEpoch,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
static Route<DateTime> _datePickerRoute(
|
||||
BuildContext context,
|
||||
Object? arguments,
|
||||
) {
|
||||
return DialogRoute<DateTime>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return DatePickerDialog(
|
||||
restorationId: 'date_picker_dialog',
|
||||
initialEntryMode: DatePickerEntryMode.calendarOnly,
|
||||
initialDate: DateTime.fromMillisecondsSinceEpoch(arguments! as int),
|
||||
firstDate: DateTime(2021, 1, 1),
|
||||
lastDate: DateTime(2022, 1, 1),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
|
||||
registerForRestoration(_selectedDate, 'selected_date');
|
||||
registerForRestoration(
|
||||
_restorableDatePickerRouteFuture, 'date_picker_route_future');
|
||||
}
|
||||
|
||||
void _selectDate(DateTime? newSelectedDate) {
|
||||
if (newSelectedDate != null) {
|
||||
setState(() {
|
||||
_selectedDate.value = newSelectedDate;
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
content: Text(
|
||||
'Selected: ${_selectedDate.value.day}/${_selectedDate.value.month}/${_selectedDate.value.year}'),
|
||||
));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: OutlinedButton(
|
||||
onPressed: () {
|
||||
_restorableDatePickerRouteFuture.present();
|
||||
},
|
||||
child: const Text('Open Date Picker'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Template: dev/snippets/config/templates/stateful_widget_restoration_material.tmpl
|
||||
//
|
||||
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
|
||||
// of samples, and may be ignored if you are just exploring the sample.
|
||||
|
||||
// Flutter code sample for showDateRangePicker
|
||||
//
|
||||
//***************************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
// This sample demonstrates how to create a restorable Material date range picker.
|
||||
// This is accomplished by enabling state restoration by specifying
|
||||
// [MaterialApp.restorationScopeId] and using [Navigator.restorablePush] to
|
||||
// push [DateRangePickerDialog] when the button is tapped.
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//***************************************************************************
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
|
||||
/// This is the main application widget.
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
static const String _title = 'Flutter Code Sample';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
restorationScopeId: 'app',
|
||||
title: _title,
|
||||
home: MyStatefulWidget(restorationId: 'main'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// This is the stateful widget that the main application instantiates.
|
||||
class MyStatefulWidget extends StatefulWidget {
|
||||
const MyStatefulWidget({Key? key, this.restorationId}) : super(key: key);
|
||||
|
||||
final String? restorationId;
|
||||
|
||||
@override
|
||||
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
|
||||
}
|
||||
|
||||
/// This is the private State class that goes with MyStatefulWidget.
|
||||
/// RestorationProperty objects can be used because of RestorationMixin.
|
||||
class _MyStatefulWidgetState extends State<MyStatefulWidget>
|
||||
with RestorationMixin {
|
||||
// In this example, the restoration ID for the mixin is passed in through
|
||||
// the [StatefulWidget]'s constructor.
|
||||
@override
|
||||
String? get restorationId => widget.restorationId;
|
||||
|
||||
//********************************************************************
|
||||
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
||||
|
||||
final RestorableDateTimeN _startDate =
|
||||
RestorableDateTimeN(DateTime(2021, 1, 1));
|
||||
final RestorableDateTimeN _endDate =
|
||||
RestorableDateTimeN(DateTime(2021, 1, 5));
|
||||
late final RestorableRouteFuture<DateTimeRange?>
|
||||
_restorableDateRangePickerRouteFuture =
|
||||
RestorableRouteFuture<DateTimeRange?>(
|
||||
onComplete: _selectDateRange,
|
||||
onPresent: (NavigatorState navigator, Object? arguments) {
|
||||
return navigator
|
||||
.restorablePush(_dateRangePickerRoute, arguments: <String, dynamic>{
|
||||
'initialStartDate': _startDate.value?.millisecondsSinceEpoch,
|
||||
'initialEndDate': _endDate.value?.millisecondsSinceEpoch,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
void _selectDateRange(DateTimeRange? newSelectedDate) {
|
||||
if (newSelectedDate != null) {
|
||||
setState(() {
|
||||
_startDate.value = newSelectedDate.start;
|
||||
_endDate.value = newSelectedDate.end;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
|
||||
registerForRestoration(_startDate, 'start_date');
|
||||
registerForRestoration(_endDate, 'end_date');
|
||||
registerForRestoration(
|
||||
_restorableDateRangePickerRouteFuture, 'date_picker_route_future');
|
||||
}
|
||||
|
||||
static Route<DateTimeRange?> _dateRangePickerRoute(
|
||||
BuildContext context,
|
||||
Object? arguments,
|
||||
) {
|
||||
return DialogRoute<DateTimeRange?>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return DateRangePickerDialog(
|
||||
restorationId: 'date_picker_dialog',
|
||||
initialDateRange:
|
||||
_initialDateTimeRange(arguments! as Map<dynamic, dynamic>),
|
||||
firstDate: DateTime(2021, 1, 1),
|
||||
currentDate: DateTime(2021, 1, 25),
|
||||
lastDate: DateTime(2022, 1, 1),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
static DateTimeRange? _initialDateTimeRange(Map<dynamic, dynamic> arguments) {
|
||||
if (arguments['initialStartDate'] != null &&
|
||||
arguments['initialEndDate'] != null) {
|
||||
return DateTimeRange(
|
||||
start: DateTime.fromMillisecondsSinceEpoch(
|
||||
arguments['initialStartDate'] as int),
|
||||
end: DateTime.fromMillisecondsSinceEpoch(
|
||||
arguments['initialEndDate'] as int),
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: OutlinedButton(
|
||||
onPressed: () {
|
||||
_restorableDateRangePickerRouteFuture.present();
|
||||
},
|
||||
child: const Text('Open Date Range Picker'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
||||
//********************************************************************
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user