【问题标题】:Change color of Theme更改主题颜色
【发布时间】:2020-04-01 08:40:16
【问题描述】:

我想更改应用中计数器的颜色。我想这样做:当计数器大于 0 时将计数器的颜色更改为蓝色。如果计数器小于 0,则将计数器的颜色更改为红色。如果计数器等于 0,将计数器的颜色更改为绿色。可能吗?我只做了2种颜色。

这是我的代码:

    import 'package:flutter/material.dart';

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

class Myapp extends StatefulWidget {
  @override
  _MyappState createState() => _MyappState();
}

class _MyappState extends State<Myapp> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Myhomepage(
        title: "My Home Page",
      ),
    );
  }
}

class Myhomepage extends StatefulWidget {
  final String title;

  Myhomepage({this.title});

  @override
  _MyhomepageState createState() => _MyhomepageState();
}

class _MyhomepageState extends State<Myhomepage> {
  int counter = 0;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        centerTitle: true,
      ),
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          backgroundColor: Colors.grey[850],
          onPressed: () {
            setState(() {
              counter++;
            });
          }),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text(
                "Increase",
              ),
              color: Colors.green,
              onPressed: () {
                setState(() {
                  counter++;
                });
              },
            ),
            Text("The count of press button:"),
            Text(
              "$counter",
              style: Theme.of(context).textTheme.display2.copyWith(color: counter<=0 ? Colors.red : Colors.blue)

            ),
            RaisedButton(
              child: Text(
                "Decrease",
              ),
              color: Colors.red,
              onPressed: () {
                setState(() {
                  counter--;
                });
              },
            ),
          ],
        ),
      ),
    );
  }


}

这是我的结果:

【问题讨论】:

    标签: flutter flutter-layout flutter-theme


    【解决方案1】:

    这是实现所需系统的一种方法。我刚刚做了一个返回所需颜色的函数。

    class _MyhomepageState extends State<Myhomepage> {
      int counter = 0;
    
      Color _getCounterColor() {
        if (counter > 0) return Colors.blue;
        else if (counter < 0) return Colors.red;
        else return Colors.green;
      }
    
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
            centerTitle: true,
          ),
          floatingActionButton: FloatingActionButton(
              child: Icon(Icons.add),
              backgroundColor: Colors.grey[850],
              onPressed: () {
                setState(() {
                  counter++;
                });
              }),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                RaisedButton(
                  child: Text(
                    "Increase",
                  ),
                  color: Colors.green,
                  onPressed: () {
                    setState(() {
                      counter++;
                    });
                  },
                ),
                Text("The count of press button:"),
                Text(
                  "$counter",
                  style: Theme.of(context).textTheme.display2.copyWith(color: _getCounterColor()),
                ),
                RaisedButton(
                  child: Text(
                    "Decrease",
                  ),
                  color: Colors.red,
                  onPressed: () {
                    setState(() {
                      counter--;
                    });
                  },
                ),
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 2016-08-31
      • 2014-07-29
      • 1970-01-01
      相关资源
      最近更新 更多