flutter - Flutter,如何正确使用 TimePickerThemeData()?
问题描述
我现在看到这个https://github.com/flutter/flutter/pull/59191
似乎新添加了一个名为 TimePickerThemeData 的功能,以便轻松自定义。
但我没有看到任何例子。
任何已实现的代码将不胜感激
class AppTheme {
AppTheme._();
static final ThemeData lightTheme = ThemeData(
timePickerTheme: TimePickerThemeData(
dayPeriodTextColor: Colors.red,
dayPeriodColor: Colors.green, //Background of AM/PM.
dialHandColor: Colors.red,
dialTextColor: Colors.red,
),
);
}
class OtherClass{
...
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: AppTheme.lightTheme,
home: Scaffold(
body: ...
child: SomeChild(
onTap: () async{
await showTimePicker(
context: context,
helpText: "",
initialTime:
TimeOfDay(hour: 10, minute: 47),
initialEntryMode:
TimePickerEntryMode.input,
builder:
(BuildContext context, Widget child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(
alwaysUse24HourFormat: true,
),
child: child,
);
},
)
}
)
),);
}
}
解决方案
您可以在下面复制粘贴运行完整代码
您可以使用Theme
wrap Builder
wrapElevatedButton/FloatingActionButton
代码片段
Theme(
data: ThemeData(
primarySwatch: Colors.blue,
timePickerTheme: TimePickerThemeData(
dayPeriodTextColor: Colors.red,
dayPeriodColor: Colors.green, //Background of AM/PM.
dialHandColor: Colors.red,
dialTextColor: Colors.red,
hourMinuteTextColor: Colors.red,
hourMinuteColor: Colors.green,
),
),
child: Builder(builder: (BuildContext context) {
return ElevatedButton(
onPressed: () async {
工作演示
完整代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Theme(
data: ThemeData(
primarySwatch: Colors.blue,
timePickerTheme: TimePickerThemeData(
dayPeriodTextColor: Colors.red,
dayPeriodColor: Colors.green, //Background of AM/PM.
dialHandColor: Colors.red,
dialTextColor: Colors.red,
hourMinuteTextColor: Colors.red,
hourMinuteColor: Colors.green,
),
),
child: Builder(builder: (BuildContext context) {
return ElevatedButton(
onPressed: () async {
print(
"${TimePickerTheme.of(context).dayPeriodTextColor.toString()}");
print(
"${TimePickerTheme.of(context).dayPeriodColor.toString()}");
print(
"${TimePickerTheme.of(context).hourMinuteColor.toString()}");
await showTimePicker(
context: context,
helpText: "",
initialTime: TimeOfDay(hour: 10, minute: 47),
initialEntryMode: TimePickerEntryMode.input,
builder: (BuildContext context, Widget child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(
alwaysUse24HourFormat: true,
),
child: child,
);
},
);
},
child: Text('Submit'));
}),
)
],
),
),
floatingActionButton: Theme(
data: ThemeData(
primarySwatch: Colors.blue,
timePickerTheme: TimePickerThemeData(
dayPeriodTextColor: Colors.red,
dayPeriodColor: Colors.green, //Background of AM/PM.
dialHandColor: Colors.red,
dialTextColor: Colors.red,
hourMinuteTextColor: Colors.purple,
hourMinuteColor: Colors.brown,
),
),
child: Builder(builder: (BuildContext context) {
return FloatingActionButton(
onPressed: () async {
print(
"${TimePickerTheme.of(context).dayPeriodTextColor.toString()}");
print(
"${TimePickerTheme.of(context).dayPeriodColor.toString()}");
print(
"${TimePickerTheme.of(context).hourMinuteColor.toString()}");
await showTimePicker(
context: context,
helpText: "",
initialTime: TimeOfDay(hour: 10, minute: 47),
initialEntryMode: TimePickerEntryMode.input,
builder: (BuildContext context, Widget child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(
alwaysUse24HourFormat: true,
),
child: child,
);
},
);
},
child: Icon(Icons.add),
);
})),
);
}
}
推荐阅读
- php - html2pdf - 为 [table] 找到太多标签闭包
- javascript - Laravel-mix 6 - npm 脚本问题
- docker - 使用 docker compose 运行 selenium hub 和 node
- javascript - 可以使用 Javascript 无法点击按钮吗?
- c# - 您可以将数据从 asp.net web api 或 web 服务发送到 web 表单吗
- node.js - Docker - “缺少脚本:“dev”” - 但它在那里?
- python - 不能用漂亮的汤和硒刮掉班古德网站
- javascript - SCSS 水平显示
- javascript - 有没有办法保持 useContext 数据持久化?
- node.js - 如何为机器人所在的另一台服务器创建 Discord 服务器邀请 (Discord.JS)