Flutter日期选择器插件textfield_datepicker的使用

Flutter日期选择器插件textfield_datepicker的使用

标题

flutter日期选择器插件textfield_datepicker的使用

内容

A collection of three flutter widget TextfieldDatePicker, TextFieldDateAndTimePicker and TextFieldTimePicker. These widgets gives you access to the various platform date and time pickers based on your device platform.

The date or time picked is shown in a Material TextFormField widget. These three widgets earlier mentioned gives you access to most of Material TextFormField parameters allowing you to design your textfield based on your preference

Screenshots of how it works For screenshots of how this package works, visit this link See example for details of how this package works

Installation Add the textfield_datepicker package to your pubspec.yml file.

Import the package into your dart file

Usage

TextfieldDatePicker

Property Default Description
materialDatePickerInitialEntryMode DatePickerEntryMode.calendar In DatePickerEntryMode.calendar mode, a calendar grid is displayed and the user taps the day they wish to select. In DatePickerEntryMode.input mode a TextField is displayed and the user types in the date they wish to select…
materialDatePickerFirstDate required The earliest allowable date.
materialDatePickerInitialDate required When the date picker is first displayed, it will show the month of materialDatePickerInitialDate, with materialDatePickerInitialDate selected.
materialDatePickerLastDate required The latest allowable date.
preferredDateFormat required The parameter is for formatting and parsing dates in a locale-sensitive manner.
materialDatePickerBuilder optional The parameter can be used to wrap the dialog widget to add inherited widgets like Theme.
materialDatePickerLocale optional The optional locale argument can be used to set the locale for the date picker. It defaults to the ambient locale provided by Localizations.
materialDatePickerSelectableDayPredicate optional An optional function can be passed in to only allow certain days for selection.
cupertinoDatePickerMaximumDate required The maximum selectable date that the picker can settle on.
cupertinoDatePickerMinimumDate required The minimum selectable date that the picker can settle on.
cupertinoDatePickerMinimumYear optional The minimum year that the picker can be scrolled to in CupertinoDatePickerMode.date mode. Defaults to 1 and must not be null.
cupertinoDatePickerMaximumYear required The minimum year that the picker can be scrolled to in CupertinoDatePickerMode.date mode. Defaults to 1 and must not be null.
cupertinoDatePickerBackgroundColor required Defaults to null, which disables background painting entirely. Background color of cupertinoDatePicker.
cupertinoDatePickerKey optional The is an identifier.
cupertinoDateInitialDateTime required Defaults to the present date and time and must not be null.
cupertinoDateOrder optional Determines the order of the columns inside CupertinoDatePicker in date mode. Defaults to the locale’s default date format/order.
textFieldDatePickerWidth optional Gives you the option to adjust the width of the TextfieldDatePicker widget.
textFieldDatePickerMargin optional Allows you to add some margin to the TextfieldDatePicker.
textFieldDatePickerPadding optional Allows you to add some padding to the TextfieldDatePicker.

TextFieldDateAndTimePicker

Property Default Description
materialDatePickerInitialEntryMode DatePickerEntryMode.calendar In DatePickerEntryMode.calendar mode, a calendar grid is displayed and the user taps the day they wish to select. In DatePickerEntryMode.input mode a TextField is displayed and the user types in the date they wish to select…
materialDatePickerFirstDate required The earliest allowable date.
materialDatePickerInitialDate required When the date picker is first displayed, it will show the month of materialDatePickerInitialDate, with materialDatePickerInitialDate selected.
materialDatePickerLastDate required The latest allowable date.
preferredDateFormat required The parameter is for formatting and parsing dates in a locale-sensitive manner.
materialDatePickerBuilder optional The parameter can be used to wrap the dialog widget to add inherited widgets like Theme.
materialDatePickerLocale optional The optional locale argument can be used to set the locale for the date picker. It defaults to the ambient locale provided by Localizations.
materialDatePickerSelectableDayPredicate optional An optional function can be passed in to only allow certain days for selection.
materialDatePickerUse24hrFormat optional Defaults to false.
materialDatePickerInitialEntryMode optional The parameter can be used to determine the initial time entry selection of the picker (either a clock dial or text input).
materialDatePickerBuilder optional The parameter can be used to wrap the dialog widget to add inherited widgets like Localizations.override, Directionality, or MediaQuery.
cupertinoDatePickerMaximumDate required The maximum selectable date that the picker can settle on.
cupertinoDatePickerMinimumDate required The minimum selectable date that the picker can settle on.
cupertinoDatePickerMinimumYear optional The minimum year that the picker can be scrolled to in CupertinoDatePickerMode.date mode. Defaults to 1 and must not be null.
cupertinoDatePickerMaximumYear required The minimum year that the picker can be scrolled to in CupertinoDatePickerMode.date mode. Defaults to 1 and must not be null.
cupertinoDatePickerBackgroundColor required Defaults to null, which disables background painting entirely. Background color of cupertinoDatePicker.
cupertinoDatePickerKey optional The is an identifier.
cupertinoDateInitialDateTime required Defaults to the present date and time and must not be null.
cupertinoDateOrder optional Determines the order of the columns inside CupertinoDatePicker in date mode. Defaults to the locale’s default date format/order.
cupertinoTimePickerUse24hFormat optional Defaults to false.
cupertinoTimePickerMinuteInterval optional The granularity of the minutes spinner, if it is shown in the current mode. Must be an integer factor of 60.
textFieldDateTimePickerWidth optional Gives you the option to adjust the width of the TextfieldDateAndTimePicker widget.
textFieldDateTimePickerMargin optional Allows you to add some margin to the TextfieldDateAndTimePicker.
textFieldDateTimePickerPadding optional Allows you to add some padding to the TextfieldDateAndTimePicker.

TextFieldTimePicker

Property Default Description
materialInitialTime required When the time picker is first displayed, it will have materialInitialTime, as the time selected.
materialTimePickerUse24hrFormat optional Defaults to false.
materialTimePickerInitialEntryMode optional The parameter can be used to determine the initial time entry selection of the picker (either a clock dial or text input).
materialTimePickerBuilder optional The parameter can be used to wrap the dialog widget to add inherited widgets like Localizations.override, Directionality, or MediaQuery.
cupertinoDatePickerBackgroundColor required Defaults to null, which disables background painting entirely. Background color of cupertinoDatePicker.
cupertinoDatePickerKey optional The is an identifier.
cupertinoDateInitialDateTime required Defaults to the present date and time and must not be null.
cupertinoTimePickerUse24hFormat optional Defaults to false.
cupertinoTimePickerMinuteInterval optional The granularity of the minutes spinner, if it is shown in the current mode. Must be an integer factor of 60.
textFieldTimePickerWidth optional Gives you the option to adjust the width of the TextfieldTimePicker widget.
textFieldTimePickerMargin optional Allows you to add some margin to the TextfieldTimePicker.
textFieldTimePickerPadding optional Allows you to add some padding to the TextfieldTimePicker.

示例代码

import 'package:example/screens/textfieldDatePicker1.dart';
import 'package:example/screens/textfieldDateTimePicker.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Textfield Date Picker Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: WelcomePage.routeName,
      routes: {
        TextFieldDatePickerDemo.routeName: (context) =>
            const TextFieldDatePickerDemo(),
        TextFieldDateTimePicker.routeName: (context) =>
            const TextFieldDateTimePicker()
      },
    );
  }
}

安装

在你的 pubspec.yml 文件中添加 textfield_datepicker 包。

dependencies:
  textfield_datepicker: ^0.2.0

使用

import 'package:textfield_datepicker/textfield_datepicker.dart';
import 'package:textfield_datepicker/textfield_timePicker.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Textfield Date Picker Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: TextFieldDatePickerDemo(),
    );
  }
}

更多关于Flutter日期选择器插件textfield_datepicker的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter日期选择器插件textfield_datepicker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter应用中使用textfield_datepicker插件的示例代码。这个插件允许你在TextField中集成日期选择器。

首先,你需要在你的pubspec.yaml文件中添加依赖:

dependencies:
  flutter:
    sdk: flutter
  textfield_datepicker: ^2.0.0  # 确保使用最新版本,版本号可能有所变动

然后运行flutter pub get来获取依赖。

接下来,你可以在你的Dart文件中使用TextFieldDatePicker组件。以下是一个完整的示例,展示了如何在一个简单的Flutter应用中集成并使用这个插件:

import 'package:flutter/material.dart';
import 'package:textfield_datepicker/textfield_datepicker.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'TextField DatePicker Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  DateTime? selectedDate;

  void _onDateSelected(DateTime date) {
    setState(() {
      selectedDate = date;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('TextField DatePicker Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextFieldDatePicker(
              decoration: InputDecoration(
                labelText: 'Select Date',
                border: OutlineInputBorder(),
              ),
              firstDate: DateTime(1900),
              lastDate: DateTime(2100),
              onChanged: _onDateSelected,
              initialDate: selectedDate ?? DateTime.now(),
              format: DateFormat("yyyy-MM-dd"), // 使用intl包格式化日期
            ),
            SizedBox(height: 20),
            Text(
              selectedDate != null
                  ? 'Selected Date: ${selectedDate!.toLocal().toString()}'
                  : 'No date selected',
              style: TextStyle(fontSize: 18),
            ),
          ],
        ),
      ),
    );
  }
}

注意:

  1. 在这个示例中,我们使用了intl包来格式化日期。因此,你还需要在pubspec.yaml中添加intl依赖:
dependencies:
  intl: ^0.17.0  # 确保使用最新版本,版本号可能有所变动
  1. 确保在文件顶部导入intl包:
import 'package:intl/intl.dart';

这个示例展示了如何创建一个带有日期选择器的TextField,当用户选择一个日期时,日期会被显示在TextField中,并且选中的日期也会被显示在Text组件中。你可以根据需要调整日期范围、初始日期和日期格式。

回到顶部