在Flutter中更改日期时间选择器标题颜色。

huangapple go评论62阅读模式
英文:

Change date time picker header color in Flutter

问题

我正在使用Flutter的Form Builder包,似乎无法直接更改FormBuilderDateTimePicker类的颜色,因此我在ThemeData中使用了datePickerTheme,

        datePickerTheme: const DatePickerThemeData(
          headerBackgroundColor: Color(0xFF2296F3),
          headerForegroundColor: Colors.white,
          backgroundColor: Color(0xFFE6F3FD),
        ),

但头部部分并未完全更改背景颜色,下方有空白,请参见下图

结果

如何更改特定空间的颜色或去掉它?或者可以去掉分隔线吗?或者改变分隔线的颜色?

英文:

I'm using Flutter Form Builder package and it seems I can't change the color directly using the FormBuilderDateTimePicker class, so I used the datePickerTheme in the ThemeData,

        datePickerTheme: const DatePickerThemeData(
          headerBackgroundColor: Color(0xFF2296F3),
          headerForegroundColor: Colors.white,
          backgroundColor: Color(0xFFE6F3FD),
        ),

But the header part does not fully change the background color, there is a space below, please see the image below

Result

How to change color on that specific space or can I get rid of it?, or can I get rid the divider? or change the color of the divider?

答案1

得分: 0

You can't get rid of divider but you can set its color to transparent.

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: 'Flutter Demo',
      theme: ThemeData(
        useMaterial3: true,
      ),
      home: const MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

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

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          title: const Text('Demo'),
        ),
        //actual code starts here
        body: const _ShowDateTime());
  }
}

class _ShowDateTime extends StatelessWidget {
  const _ShowDateTime();

  @override
  Widget build(BuildContext context) {

    // The context passed in showDatePicker should have information about
    // changed datePickerTheme and dividerTheme. Hence ElevatedButton is 
    // child of builder which context is aware of updated theme data.
    // Same builder's context is also passed as context to `showDatePicker`
    // so that you could get the desired result.

    return Center(
      child: Theme(
        data: Theme.of(context).copyWith(
          datePickerTheme: const DatePickerThemeData(
            headerBackgroundColor: Color(0xFF2296F3),
            headerForegroundColor: Colors.white,
            backgroundColor: Color(0xFFE6F3FD),
          ),
          dividerTheme: const DividerThemeData(
            color: Colors.transparent,
          ),
        ),
        child: Builder(builder: (context) {
          return ElevatedButton(
              onPressed: () {
                showDatePicker(
                  context: context,
                  initialDate: DateTime.now(),
                  firstDate: DateTime(2023),
                  lastDate: DateTime(2026).subtract(
                    const Duration(days: 1),
                  ),
                );
              },
              child: const Text('Change Date'));
        }),
      ),
    );
  }
}

在Flutter中更改日期时间选择器标题颜色。

英文:

You can't get rid of divider but you can set its color to transparent.

import &#39;package:flutter/material.dart&#39;;

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: &#39;Flutter Demo&#39;,
      theme: ThemeData(
        useMaterial3: true,
      ),
      home: const MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

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

  @override
  State&lt;MyHomePage&gt; createState() =&gt; _MyHomePageState();
}

class _MyHomePageState extends State&lt;MyHomePage&gt; {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          title: const Text(&#39;Demo&#39;),
        ),
        //actual code starts here
        body: const _ShowDateTime());
  }
}

class _ShowDateTime extends StatelessWidget {
  const _ShowDateTime();

  @override
  Widget build(BuildContext context) {

    // The context passed in showDatePicker should have information about
    // changed datePickerTheme and dividerTheme. Hence ElevatedButton is 
    // child of builder which context is aware of updated theme data.
    // Same builder&#39;s context is also passed as context to `showDatePicker`
    // so that you could get the desired result.

    return Center(
      child: Theme(
        data: Theme.of(context).copyWith(
          datePickerTheme: const DatePickerThemeData(
            headerBackgroundColor: Color(0xFF2296F3),
            headerForegroundColor: Colors.white,
            backgroundColor: Color(0xFFE6F3FD),
          ),
          dividerTheme: const DividerThemeData(
            color: Colors.transparent,
          ),
        ),
        child: Builder(builder: (context) {
          return ElevatedButton(
              onPressed: () {
                showDatePicker(
                  context: context,
                  initialDate: DateTime.now(),
                  firstDate: DateTime(2023),
                  lastDate: DateTime(2026).subtract(
                    const Duration(days: 1),
                  ),
                );
              },
              child: const Text(&#39;Change Date&#39;));
        }),
      ),
    );
  }
}

在Flutter中更改日期时间选择器标题颜色。

huangapple
  • 本文由 发表于 2023年6月13日 18:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定