如何在Flutter Webview中更改日期选择器的颜色

gmol1639  于 2022-12-24  发布在  Flutter
关注(0)|答案(2)|浏览(183)

我遇到过与https://github.com/flutter/flutter/issues/100067中讨论的问题相同的问题
我能够得到flutter Webview。但我需要改变颜色从浅绿色到蓝色,如底部导航栏中的加号按钮所示。请帮助。

gajydyqb

gajydyqb1#

尝试以下代码,参考showDatePicker

builder: (context, child) {
        return Theme(
          data: Theme.of(context).copyWith(
            colorScheme: const ColorScheme.light(
              primary: Colors.indigo,//add your + button color here
             ),
            textButtonTheme: TextButtonThemeData(
              style: TextButton.styleFrom(
                foregroundColor: Colors.indigo, // button text color
              ),
            ),
          ),
          child: child!,
        );
      },

完整代码:

import 'dart:async';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  DateTime selectedDate = DateTime.now();

  Future<void> _selectDate(BuildContext context) async {
    final DateTime? picked = await showDatePicker(
      context: context,
      initialDate: selectedDate,
      firstDate: DateTime(2015, 8),
      lastDate: DateTime(2101),
      builder: (context, child) {
        return Theme(
          data: Theme.of(context).copyWith(
            colorScheme: const ColorScheme.light(
              primary: Colors.indigo,
             ),
            textButtonTheme: TextButtonThemeData(
              style: TextButton.styleFrom(
                foregroundColor: Colors.indigo, // button text color
              ),
            ),
          ),
          child: child!,
        );
      },
    );
    if (picked != null && picked != selectedDate) {
      setState(() {
        selectedDate = picked;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Datepicker'),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text("${selectedDate.toLocal()}".split(' ')[0]),
            SizedBox(
              height: 20.0,
            ),
            ElevatedButton(
              onPressed: () => _selectDate(context),
              child: Text('Select date'),
            ),
          ],
        ),
      ),
    );
  }
}

结果-〉

c9qzyr3d

c9qzyr3d2#

参考issue和配置
1.打开应用程序的build.gradle文件。
1.确保存储库部分包含Google的Maven存储库google()。例如:

allprojects {
   repositories { 
   google() 
   centre() 
  }
}

将库添加到依赖项部分:

dependencies {
    // ...
    implementation 'com.google.android.material:material:1.7'
    // ...
  }

1.在/android/app/src/main/res/values/styles.xml中设置灯光主题:内容_副本更改

<style name="NormalTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

1.在/android/app/src/main/res/values-night/styles. xml中设置黑暗主题content_copy change

<style name="NormalTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">

相关问题