【问题标题】:how to change date picker selected value color in flutter如何在颤动中更改日期选择器选择的值颜色
【发布时间】:2020-05-18 23:15:03
【问题描述】:

我正在使用 showDatePicker() 方法在我的 Flutter 应用程序中显示一个日期选择器。如何自定义日期选择器的颜色?

这是我的主题代码:`

Widget dateOfBirth(String hintText) {
 return Theme(
data: Theme.of(context).copyWith(
  buttonTheme: ButtonThemeData(
      textTheme: ButtonTextTheme
          .accent //Colour of the text in the button "OK/CANCEL"
      ),
),
child: Builder(
  builder: (context) {
    return GestureDetector(
      onTap: () async {
        DateTime initialDate = DateTime(DateTime.now().year - 17,
            DateTime.now().month, DateTime.now().day);

        final picked = await showDatePicker(
          context: context,
          initialDate: initialDate,
          firstDate: DateTime(DateTime.now().year - 100,
              DateTime.now().month, DateTime.now().day),
          lastDate: DateTime(DateTime.now().year - 17, DateTime.now().month,
              DateTime.now().day),
        );

        if (picked != null && picked != dobSelected) {
          setState(() {

          });
        }

        return picked;
      },
      child: Padding(
        //You can use any other widget here
        padding: const EdgeInsets.symmetric(horizontal: 40.0),
        child: Container(
            height: 55,
            width: MediaQuery.of(context).size.width,
            alignment: Alignment.centerLeft,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(3)),
              color: Color(0xFFF2F2F2),
            ),
            padding: const EdgeInsets.symmetric(horizontal: 13),
            child: dobSelected == null
                ? Text(
                    'Date Of Birth',
                    style: TextStyle(
                        color: widget.isLender
                            ? Color(0xFF8B8B8B)
                            : Color(0xFFB3B1B1),
                        fontSize: 15),
                  )
                : Text(DateFormat('yyyy-MM-dd').format(dobSelected))),
      ),
    );
  },
),
);}

我假设您希望自定义日期选择器与您的主题不同。通常,日期选择器遵循您的主题。 这是我在主题中包装页面的代码:

 @override
 Widget build(BuildContext context) {
    [...]
    return new CustomTheme(
       new Scaffold(
          [...]
  )
);}

我想更改日期选择器选定值的颜色

【问题讨论】:

    标签: android ios flutter dart


    【解决方案1】:

    您尚未向我们展示您尝试更改的代码。您应该能够只用 setState 更改您的 isLender 变量来完成它(在传递它的父类中)。

    setState((){
      isLender = true; // or false
    });
    

    【讨论】:

      【解决方案2】:

      如果您在 2021 年更改颜色时仍然面临零安全问题..那么这里是简单的解决方案

            Future<void> _selectDate(BuildContext context) async {
      DateTime? picked = await showDatePicker(
            context: context,
          builder: (BuildContext context, Widget ?child) {
            return Theme(
              data: ThemeData(
                primarySwatch: Colors.grey,
                splashColor: Colors.black,
                textTheme: TextTheme(
                  subtitle1: TextStyle(color: Colors.black),
                  button: TextStyle(color: Colors.black),
                ),
                accentColor: Colors.black,
                colorScheme: ColorScheme.light(
                    primary: Color(0xffffbc00),
                    primaryVariant: Colors.black,
                    secondaryVariant: Colors.black,
                    onSecondary: Colors.black,
                    onPrimary: Colors.white,
                    surface: Colors.black,
                    onSurface: Colors.black,
                    secondary: Colors.black),
                    dialogBackgroundColor: Colors.white,
              ),
              child: child ??Text(""),
            );
          }
          initialDate: selectedDate,
          firstDate: DateTime(1960, 8),
          lastDate: DateTime.now());
      if (picked != null && picked != selectedDate)
        setState(() {
          selectedDate = picked;
           
      
          String da = picked.day.toString() +
              "-" +
              picked.month.toString() +
              "-" +
              picked.year.toString();
          dateOfBirth.text = da;
        });}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-27
        • 1970-01-01
        相关资源
        最近更新 更多