英文:
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
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'));
}),
),
);
}
}
英文:
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'));
}),
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论