Alexei
Alexei

Reputation: 15664

RaisedButton not change background and text color

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

here result:

enter image description here

Why background of raisebutton is not red. And why text color of raisbutton is not blue?

Upvotes: 0

Views: 1933

Answers (3)

1DD
1DD

Reputation: 295

In case anyone that's also searching for a way to properly enable/disable a RaisedButton, you can read the answer from How to properly enable/disable flutter button. Update the corresponding boolean variable in a setState() and implement something similar to the sample code here. Hope this would help.

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

Upvotes: 0

Suman Maharjan
Suman Maharjan

Reputation: 1122

This is the implementation of MaterialButton (parent of 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;

If you do not provide onPress, then it will be in a disabled state and will take default disabled button color and disabled text color ignoring the color you explicitly provide. I hope this clarifies.

Upvotes: 1

Yuu Woods
Yuu Woods

Reputation: 1348

Your button is disabled. You need to set up an "onPressed" callback.

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

sample image

Upvotes: 2

Related Questions