flutterflutter-form-builder

Change date time picker header color in Flutter


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?


Solution

  • 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'));
            }),
          ),
        );
      }
    }