我想更改flutter上showDatePicker的日期字符串

问题描述 投票:0回答:2

我想用material3自定义这个showDatePicker的颜色和文本,在

flutter

我想定制成这样

我需要这样的文本“Seg, 17 de jul。”

flutter datepicker material3 flutter-datetime-picker flutter-date-range-picker
2个回答
0
投票

这是一个简单的例子

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Date Picker Theme Example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.blueGrey),
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Date Picker Theme Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            // Show the date picker
            showDatePicker(
              context: context,
              initialEntryMode: DatePickerEntryMode.calendar,
              initialDate: DateTime.now(),
              firstDate: DateTime.now(),
              lastDate: DateTime.now(),
              builder: (context, child) {
                return Theme(
                  data: Theme.of(context).copyWith(
                    datePickerTheme: DatePickerTheme.of(context).copyWith(
                      backgroundColor: Colors.grey,
                      shadowColor: Colors.blue,
                      headerHelpStyle: const TextStyle(
                        color: Colors.white,
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                      headerHeadlineStyle: const TextStyle(
                        color: Colors.white,
                        fontSize: 18,
                      ),
                      yearStyle: const TextStyle(
                        color: Colors.white,
                        fontSize: 18,
                      ),
                      dayStyle: const TextStyle(
                        color: Colors.white,
                        fontSize: 18,
                      ),
                      rangePickerHeaderHelpStyle: const TextStyle(
                        color: Colors.white,
                        fontSize: 18,
                      ),
                      weekdayStyle: const TextStyle(
                        color: Colors.white,
                        fontSize: 22,
                      ),
                    ),
                  ),
                  child: child!,
                );
              },
            );
          },
          child: const Text('Open Date Picker'),
        ),
      ),
    );
  }
}

0
投票

您可以使用这个flutter包adoptive_calendar以简单的方式选择具有完整主题定制的日期和时间,请参阅演示图片

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Example Adoptive Calendar',
      theme: ThemeData(
        useMaterial3: true,
      ),
      home: const ExampleAdoptiveCalendar(),
    );
  }
}

class ExampleAdoptiveCalendar extends StatefulWidget {
  const ExampleAdoptiveCalendar({super.key});

  @override
  State<ExampleAdoptiveCalendar> createState() =>
      _ExampleAdoptiveCalendarState();
}

class _ExampleAdoptiveCalendarState extends State<ExampleAdoptiveCalendar> {
  DateTime? pickedDate;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.deepPurpleAccent,
        title: const Text(
          "Adoptive Calendar Example",
          style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
        ),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          Flexible(
            child: Center(
                child: ElevatedButton(
              onPressed: () async {
                pickedDate = await showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return AdoptiveCalendar(
                      initialDate: DateTime.now(),
                    );
                  },
                );
                setState(() {});
              },
              child: const Text("Open Calendar"),
            )),
          ),
          const SizedBox(height: 20),
          Center(child: Text(pickedDate.toString())),
          const SizedBox(height: 40),
        ],
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.