在Flutter(带有动态主题)中为暗模式更改文本颜色?

时间:2020-03-17 15:41:56

标签: flutter colors themes

当我选择暗模式时,文本变成白色,但我想使所有文本变成白色70或类似的东西(包括按钮和常规文本)。如何为暗模式定义默认文本颜色?

我的主题数据现在是这样的:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DynamicTheme(
        defaultBrightness: Brightness.light,
        data: (brightness) => ThemeData(
      primarySwatch: Colors.blueGrey,
      brightness: brightness,
        ),

1 个答案:

答案 0 :(得分:1)

您可以执行类似的操作(随意更改):

首先转到ios / Runner文件夹。接下来打开info.plist,并将以下几行添加到Dict部分。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="49dp"
        android:layout_marginLeft="49dp"
        android:layout_marginEnd="49dp"
        android:layout_marginRight="49dp"
        android:text="Search"
        android:textColor="#ffffff"
        android:textSize="@dimen/_20ssp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <EditText
        android:id="@+id/search_field"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/search_layout"
        android:ems="10"
        android:fontFamily="@font/nunito_bold"
        android:hint="Search here"
        android:inputType="textPersonName"
        android:paddingLeft="20dp"
        android:paddingTop="10dp"
        android:paddingRight="20dp"
        android:paddingBottom="10dp"
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary"
        android:textSize="16sp"
        app:layout_constraintEnd_toStartOf="@+id/search_btn"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/search_btn" />

    <ImageButton
        android:id="@+id/search_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:background="@color/cardview_shadow_end_color"
        android:paddingTop="17dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/search_field"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:srcCompat="@mipmap/search_button" />


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/popular_rc_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_field">

    </androidx.recyclerview.widget.RecyclerView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:scaleType="center"
        android:src="@drawable/recent_icon"
        android:visibility="invisible"
        app:fab_label="Most Recent"
        app:backgroundTint="@color/colorFabIcon"
        app:elevation="6dp"
        app:maxImageSize="35dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/fab2"
        app:pressedTranslationZ="12dp" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="18dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:scaleType="center"
        app:fab_label="Most Rated"
        android:src="@drawable/rated_icon"
        android:visibility="invisible"
        app:backgroundTint="@color/colorFabIcon"
        app:elevation="6dp"
        app:maxImageSize="35dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/fab1"
        app:pressedTranslationZ="12dp" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:scaleType="center"
        android:src="@drawable/pop_icon"
        android:visibility="invisible"
        app:backgroundTint="@color/colorFabIcon"
        app:elevation="6dp"
        app:maxImageSize="35dp"
        app:fab_label="Most Popular"
        app:layout_constraintBottom_toTopOf="@+id/fab"
        app:layout_constraintEnd_toEndOf="parent"
        app:pressedTranslationZ="12dp" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="@dimen/_23sdp"
        android:layout_marginBottom="@dimen/_20sdp"
        android:scaleType="centerInside"
        android:src="@drawable/filter"
        app:backgroundTint="@color/colorPrimary"
        app:elevation="6dp"
        app:maxImageSize="35dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:pressedTranslationZ="12dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

下一步。确保在MaterialApp的主题设置中包含以下行:

<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>