首页 > 解决方案 > 从表日历颤动中获取选定日期的值数据表

问题描述

我想如果我们在日历表上按下某一天,那么文本将自动设置被点击的日期。

在此处输入图像描述

这是使用实现的代码TableCalendar

     @override
          Widget build(BuildContext context) {
            return Scaffold(
              body: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.only(top:10.0,bottom: 5.0,left: 10.0,right: 10.0),
                      child: Text(tgl,style: TextStyle(color: Colors.green, fontSize: 16.0,fontWeight: FontWeight.w500),), 
                    ),//for the text I use datetimeNow
                    Padding(
                      padding: const EdgeInsets.only(bottom:10.0,left: 10.0,right: 10.0),
                      child: TableCalendar(
                        initialCalendarFormat: CalendarFormat.month,
                        calendarStyle: CalendarStyle(
                            todayColor: Colors.green,
                            selectedColor: Theme.of(context).primaryColor,
                            todayStyle: TextStyle(
                                fontWeight: FontWeight.bold,
                                fontSize: 22.0,
                                color: Colors.white)
                        ),
                        headerStyle: HeaderStyle(
                          centerHeaderTitle: true,
                          formatButtonDecoration: BoxDecoration(
                            color: Colors.brown,
                            borderRadius: BorderRadius.circular(22.0),
                          ),
                          formatButtonTextStyle: TextStyle(color: Colors.white),
                          formatButtonShowsNext: false,
                        ),
                        startingDayOfWeek: StartingDayOfWeek.monday,
                        onDaySelected: (date, events,e) {
                          print(date.toUtc());
                        },

                        builders: CalendarBuilders(
                          selectedDayBuilder: (context, date, events) => Container(
                              margin: const EdgeInsets.all(5.0),
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                  color: Theme.of(context).primaryColor,
                                  borderRadius: BorderRadius.circular(8.0)),
                              child: Text(
                                date.day.toString(),
                                style: TextStyle(color: Colors.white),
                              )),
                          todayDayBuilder: (context, date, events) => Container(
                              margin: const EdgeInsets.all(5.0),
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                  color: Colors.black26,
                                  borderRadius: BorderRadius.circular(8.0)),
                              child: Text(
                                date.day.toString(),
                                style: TextStyle(color: Colors.white),
                              )),
                        ),
                        calendarController: _controller,

                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left:15.0,top: 10.0,right: 15.0),
                      child:
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text("Report __ ", style: TextStyle(
                            color: Colors.green, fontSize: 16.0,fontWeight: FontWeight.w500)
                          ),.................

标签: flutterdatedartmobilecalendar

解决方案


您可以使用最初设置为 的变量DateTime.now(),然后在用户单击日期时更新该值。像这样的东西:

DateTime _chosenDate = DateTime.now();

// ... other lines

TableCalendar(
     initialSelectedDay: _chosenDate, // Set initial date
     onDaySelected: (date, events, e) {
        setState(() {
           _chosenDate = date; // Update the chosen date here
        });
     },
     ...

对于格式,使用这个:

final dateFormat = DateFormat('EEEE yyyy-MMMM-dd');

完整示例代码:

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:table_calendar/table_calendar.dart';

void main() {
  runApp(MaterialApp(
    home: SampleScreen(),
  ));
}
class SampleScreen extends StatefulWidget {
  @override
  _SampleScreenState createState() => _SampleScreenState();
}

class _SampleScreenState extends State<SampleScreen> {
  final _controller = CalendarController();
  final dateFormat = DateFormat('EEEE yyyy-MMMM-dd');
  DateTime _chosenDate = DateTime.now();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(
                    top: 10.0, bottom: 5.0, left: 10.0, right: 10.0),
                child: Text(
                  dateFormat.format(_chosenDate),
                  style: TextStyle(
                      color: Colors.green,
                      fontSize: 16.0,
                      fontWeight: FontWeight.w500),
                ),
              ), //for the text I use datetimeNow
              Padding(
                padding:
                const EdgeInsets.only(bottom: 10.0, left: 10.0, right: 10.0),
                child: TableCalendar(
                  initialCalendarFormat: CalendarFormat.month,
                  initialSelectedDay: _chosenDate,
                  calendarStyle: CalendarStyle(
                      todayColor: Colors.green,
                      selectedColor: Theme
                          .of(context)
                          .primaryColor,
                      todayStyle: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 22.0,
                          color: Colors.white)),
                  headerStyle: HeaderStyle(
                    centerHeaderTitle: true,
                    formatButtonDecoration: BoxDecoration(
                      color: Colors.brown,
                      borderRadius: BorderRadius.circular(22.0),
                    ),
                    formatButtonTextStyle: TextStyle(color: Colors.white),
                    formatButtonShowsNext: false,
                  ),
                  startingDayOfWeek: StartingDayOfWeek.monday,
                  onDaySelected: (date, events, e) {
                    setState(() {
                      _chosenDate = date;
                    });
                  },
                  builders: CalendarBuilders(
                    selectedDayBuilder: (context, date, events) =>
                        Container(
                            margin: const EdgeInsets.all(5.0),
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Theme
                                    .of(context)
                                    .primaryColor,
                                borderRadius: BorderRadius.circular(8.0)),
                            child: Text(
                              date.day.toString(),
                              style: TextStyle(color: Colors.white),
                            )),
                    todayDayBuilder: (context, date, events) =>
                        Container(
                            margin: const EdgeInsets.all(5.0),
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Colors.black26,
                                borderRadius: BorderRadius.circular(8.0)),
                            child: Text(
                              date.day.toString(),
                              style: TextStyle(color: Colors.white),
                            )),
                  ),
                  calendarController: _controller,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

在此处输入图像描述


推荐阅读