RaisedButton不会更改背景和文本颜色

时间:2020-03-23 10:49:01

标签: android flutter

android studio 3.6

main.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:flutter_sample/signinform.dart';
import 'constants.dart' as Constants;

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: new ThemeData(
            primaryColor: new Color(Constants.COLOR_PRIMARY),
            primaryTextTheme: TextTheme(headline6: TextStyle(color: Colors.white))),
        home: new SignInForm());
  }
}

in signinform.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'constants.dart' as Constants;

class SignInForm extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _SignInFormState();
  }
}

class _SignInFormState extends State {
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: new AppBar(
            centerTitle: true,
            title: new Text('Sign in',
                style: TextStyle(fontWeight: FontWeight.bold))),
        body: new Container(
            margin: const EdgeInsets.only(
                left: Constants.DEFAULT_MARGIN,
                right: Constants.DEFAULT_MARGIN),
            child: new Form(
                key: _formKey,
                child: new Column(children: [
                  new RaisedButton(
                      child: Text('Tap me'),
                      color: Colors.red,
                      textColor: Colors.blue,
                      onPressed: null)
                ]))));
  }
}

此处结果:

enter image description here

为什么升高按钮的背景不是红色。为什么raisbutton的文本颜色不是蓝色?

3 个答案:

答案 0 :(得分:2)

您的按钮已禁用。 您需要设置“ onPressed”回调。

name_en_US

sample image

答案 1 :(得分:1)

这是MaterialButton(FlatButton的父级)的实现。

  /// Whether the button is enabled or disabled.
  ///
  /// Buttons are disabled by default. To enable a button, set its [onPressed]
  /// or [onLongPress] properties to a non-null value.
  bool get enabled => onPressed != null || onLongPress != null;

如果不提供onPress,则它将处于禁用状态,并且将采用默认的禁用按钮颜色和禁用文本颜色,而忽略您显式提供的颜色。我希望这可以澄清。

答案 2 :(得分:0)

如果有人也在寻找正确启用/禁用RaisedButton的方法,则可以从How to properly enable/disable flutter button阅读答案。在setState()中更新相应的布尔变量,并在此处实现类似于示例代码的内容。希望这会有所帮助。

soup.findAll