我想制作一個外部 dart 檔案(我們稱之為 color_palette.dart),它有一些顏色類別。這些類將能夠通過應用程式另一個頁面上的選單進行更改。這些顏色將在所有頁面中用于定義 Appbar、按鈕、圖示等的顏色……我不想為此使用 theme()。我想使用一些可以在按鈕中使用的功能(onPress:功能)來更改顏色。
這是一個例子:
color_palette.dart
class myColor {
return Colors.red;
}
主頁.dart
Scaffold(
appbar : AppBar(
color : MyColor(),
)
)
uj5u.com熱心網友回復:
很容易做到
void main() {
//change color A from red to purple
MyColors.colorA = Colors.purple;
}
class MyColors {
static Color colorA = Colors.red;
static Color colorB = Colors.green;
static Color colorC = Colors.blue;
}
Scaffold(
appbar : AppBar(
color : MyColors.colorA,
)
)
uj5u.com熱心網友回復:
有很多方法可以做到這一點:
第一種方法:
// in the file where you define constants
import 'package:flutter/material.dart';
const Color color_red = Colors.red;
// in the file where you want to use it
Scaffold(
appbar : AppBar(
color : color_red,
)
)
第二種方法,使用顏色類。
class Palette {
static const Color color_red = Colors.red;
}
//in the file where you want to use it:
import "package:app_name/folder_name/palette.dart";
Scaffold(
appbar : AppBar(
color : Palette.color_red,
)
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/477356.html
