首页 > 解决方案 > Flutter Flat Button Color Property Not Working

问题描述

In flutter App I'm trying to set color for the FlatButton But it's not working. find the source code below.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: SafeArea(
      child: Scaffold(
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            FlatButton(
              color: Colors.red,
            ),
            FlatButton(
              color: Colors.green,
            ),
            FlatButton(
              color: Colors.blue,
            ),
          ],
        ),
      ),
    ));
  }
}

Output:

My Code Output

I'm flutter Beginner, Any Idea What is the issue in my code?

Thanks In Advance

标签: androidandroid-studioflutterdartflutter-layout

解决方案


You need to provide child and onPressed parameter in order to render the widget, else it won't render, which results in non working UI.

Check out the code i modified :

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: SafeArea(
      child: Scaffold(
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            FlatButton(
              color: Colors.red,
              onPressed:()=>printData(),
              child: Text("click"),
            ),
            FlatButton(
              color: Colors.green,
              onPressed:()=>printData(),
              child: Text("click"),
            ),
            FlatButton(
              color: Colors.blue,
              onPressed:()=>printData(),
              child: Text("click"),
            ),
          ],
        ),
      ),
    ));
  }
}

void printData(){
  print('Hello');
}

推荐阅读