下面是一个带有RaisedButton的干净Flutter示例。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('test'),
onPressed: () {},
)
],
),
),
);
}
}
theme_data.dart中的片段
// Used as the default color (fill color) for RaisedButtons. Computing the
// default for ButtonThemeData for the sake of backwards compatibility.
buttonColor ??= isDark ? primarySwatch[600] : Colors.grey[300];
material_button.dart中的片段
/// The button's fill color, displayed by its [Material], while it
/// is in its default (unpressed, [enabled]) state.
///
/// The default fill color is the theme's button color, [ThemeData.buttonColor].
///
/// See also:
///
/// * [disabledColor] - the fill color of the button when the button is disabled.
final Color color;
基于上述内容,我假设RaisedButton的背景色为蓝色,因为buttonColor将从primarySwatch推断出来。相反,RaisedButton具有灰色背景颜色。 我知道我可以直接通过ThemeData设置buttonColor参数,但是由于源代码指出buttonColor可以从primarySwatch推断出来,所以我很好奇为什么它不起作用。我想念什么?
答案 0 :(得分:0)
您的答案在您引用的代码中:
buttonColor ??= isDark ? primarySwatch[600] : Colors.grey[300];
在同一文件(theme_data.dart)上,您可以看到isDark
被定义为:
final bool isDark = brightness == Brightness.dark;
由于您的主题未定义为Brightness.dark
,因此该按钮将默认为Colors.grey[300]
。