繁体   English   中英

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

[英]RaisedButton not change background and text color

安卓工作室 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());
  }
}

在 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)
                ]))));
  }
}

这里的结果:

在此处输入图片说明

为什么 raisebutton 的背景不是红色的。 为什么 raisbutton 的文本颜色不是蓝色?

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

new RaisedButton(
  child: Text('Tap me'),
  color: Colors.red,
  textColor: Colors.blue,
  onPressed: () {},
)

示例图像

这是 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,那么它将处于禁用状态,并将采用默认禁用按钮颜色和禁用文本颜色,而忽略您明确提供的颜色。 我希望这能澄清。

如果有人也在寻找正确启用/禁用 RaisedButton 的方法,您可以阅读如何正确启用/禁用颤动按钮中的答案。 更新 setState() 中相应的布尔变量并实现类似于此处示例代码的内容。 希望这会有所帮助。

RaisedButton(
    disabledColor: Colors.grey,
    child: const Text('DONE'),
    onPressed: (DISABLED_IS_TRUE) ? null : () => DO_SOMETHING_IF_ENABLED(),
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM