当用户点击任何一天时,如何在日历的日期显示值

问题描述 投票:1回答:1

我想在一个calander的日子里显示一个值(例如那天的价格),当他点击flutter的默认日历时的一天。我怎样才能做到这一点。我希望它显示在下图所示的位置:

enter image description here

这里更新是我现在尝试的,只是我点击按钮后向用户显示的日期和时间选择器:

Future<Null> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: DateTime(2015, 8),
        lastDate: DateTime(2101));
    if (picked != null && picked != selectedDate) {
      setState(() {
        selectedDate = picked;
      });

    }
    final TimeOfDay timePicked = await showTimePicker(
        context: context,
        initialTime: selectedTime,
      );
      if (picked != null && timePicked != selectedTime) {
        setState(() {
          selectedTime = timePicked;

        });
      }
  }
dart flutter
1个回答
1
投票

有一个文件可以绘制dayPicker小部件。它位于这里:

{} flutter_dir /packages/flutter/lib/src/material/date_picker.dart

您可以编辑文件(我不推荐)或分叉包并更改它(或任何其他您喜欢的方式)。

使用下面的dayWidget而不是原始的:

Widget dayWidget = Container(
      decoration: decoration,
      child: Center(
        child: Semantics(
          label: '${localizations.formatDecimal(day)}, ${localizations.formatFullDate(dayToBuild)}',
          selected: isSelectedDay,
          child: ExcludeSemantics(
            child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(localizations.formatDecimal(day), style: itemStyle)
              ] + (isSelectedDay ? <Widget>[Text('Custome text')] : <Widget>[])),
          ),
        ),
      ),
    );

您还需要将变量传递给窗口小部件。

更新:传递参数使用下面的代码:

DayPicker({
    Key key,
    @required this.selectedDate,
    @required this.currentDate,
    @required this.onChanged,
    @required this.firstDate,
    @required this.lastDate,
    @required this.displayedMonth,
    this.selectableDayPredicate,
    this.dragStartBehavior = DragStartBehavior.down,
    this.optionalText //Added
  }) : assert(selectedDate != null),
       assert(currentDate != null),
       assert(onChanged != null),
       assert(displayedMonth != null),
       assert(dragStartBehavior != null),
       assert(!firstDate.isAfter(lastDate)),
       assert(selectedDate.isAfter(firstDate) || selectedDate.isAtSameMomentAs(firstDate)),
       super(key: key);

  /// The currently selected date.
  ///
  /// This date is highlighted in the picker.
  final DateTime selectedDate;

  final String optionalText; // Added
© www.soinside.com 2019 - 2024. All rights reserved.