我是新手,今天我正在测试诸如更改文本和功能之类的东西。但是现在我有一个问题,如何更改所有文本的颜色?我已经将背景更改为黑色,但是无法更改颜色字体并且不想在每个Text()中进行更改。
这是我现在的代码:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'El Grandioso Countador e Parificador',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'El Grandioso Countador e Parificador'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
var _color = Colors.white;
String _pi = "0 não é par nem ímpar";
void _incrementCounter() {
setState(() {
_pi = _counter % 2 == 0 ? "par" : "ímpar";
_color = _pi == "par" ? Colors.blue : Colors.red;
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black, //Here I'm changing the entire background to black, but the text are black too, so I can't read anything on my app.
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Nusha, sabe quantas vezes tu clicou no botão lá? Olha ae:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
Text(
_pi == '0 não é par nem ímpar'
? '$_pi'
: 'Rapash, teu número é $_pi',
style: TextStyle(color: _color),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Clicador',
child: Icon(Icons.add),
),
);
}
}
我已经搜索过,但没有任何东西可以解决我的问题。有人有提示吗?
答案 0 :(得分:0)
ThemeData
类具有您可以设置的textTheme
属性。
尝试一下:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'El Grandioso Countador e Parificador',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
textTheme: TextTheme(headline4: TextStyle(color: Colors.purple)),
),
home: MyHomePage(title: 'El Grandioso Countador e Parificador'),
);
}
}
答案 1 :(得分:0)
您可以在TextTheme.apply
中使用textTheme
并为bodyColor和displayColor设置颜色,如下代码:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'El Grandioso Countador e Parificador',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
textTheme: Theme.of(context).textTheme.apply(
bodyColor: Colors.red,
displayColor: Colors.red,
)),
home: MyHomePage(title: 'El Grandioso Countador e Parificador'),
);
}
}