静态成员变量颜色未绘制

问题描述 投票:0回答:1

我在扑朔迷离中有一个StatefulWidget State类。

我已经声明了一些静态const Color成员变量。当我尝试使用这些颜色时,它们不会被绘制。当我使用相同的Color ctor并将其内联到使用静态成员变量的位置时,它可以工作。

我是否缺少有关dart中成员变量初始化的内容?

观察build方法中的局部变量color。如果三元运算符使用内联ctor颜色,则可以使用。如果它使用静态成员var DARK_COLOR。...什么也没画。

import 'package:flutter/material.dart';
import 'chess.dart' as chesslib;

class ChessBoard extends StatefulWidget {
    @override
    _ChessBoardState createState() => _ChessBoardState();
}

class _ChessBoardState extends State<ChessBoard> {

    Color LIGHT_COLOR = Color(0xffeeeebb);
    Color LIGHT_COLOR_SELECTED = Color(0xffaaaaaa);
    Color DARK_COLOR = Color(0xffffffff);
    Color DARK_COLOR_SELECTED = Color(0xff119911);

    chesslib.Chess _chess;

    @override
    void initState() {
        super.initState();
        _chess = new chesslib.Chess();
    }

    @override
    Widget build(BuildContext context) {

        Widget result;

        result = GridView.count(
            crossAxisCount: 8,
            children: List.generate(64, (index) {
                int row = index ~/ 8;
                int col = index % 8;
                int modulo = (row % 2 == 0) ? 0 : 1;

                Color color  = (index % 2 == modulo) ? Color(0xffeeeebb) : DARK_COLOR;

                return Container(
                    color: color,
                    child: Text (display_str)
                );
            }),
        );

        return result;
    }
}
flutter dart
1个回答
1
投票

您的代码效果很好,可能不是您想要的颜色FFFFFFFF见下面的色板

enter image description here

enter image description here

完整的测试代码深色使用0xff666666

    import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  Color specialColor = Color(0xffbfeb91);
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(flex: 1, child: ChessBoard()),
            Text(
              'Hello World',
              style: TextStyle(
                  backgroundColor: specialColor), // or 'bfeb91', or 'ffbfeb91'
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class ChessBoard extends StatefulWidget {
  @override
  _ChessBoardState createState() => _ChessBoardState();
}

class _ChessBoardState extends State<ChessBoard> {

  Color LIGHT_COLOR = Color(0xffeeeebb);
  Color LIGHT_COLOR_SELECTED = Color(0xffaaaaaa);
  Color DARK_COLOR = Color(0xff666666);
  Color DARK_COLOR_SELECTED = Color(0xff119911);

  //chesslib.Chess _chess;

  @override
  void initState() {
    super.initState();
    //_chess = new chesslib.Chess();
  }

  @override
  Widget build(BuildContext context) {

    Widget result;

    result = GridView.count(
      crossAxisCount: 8,
      children: List.generate(64, (index) {
        int row = index ~/ 8;
        int col = index % 8;
        int modulo = (row % 2 == 0) ? 0 : 1;

        Color color  = (index % 2 == modulo) ? Color(0xffeeeebb) : DARK_COLOR;

        return Container(
            color: color,
            child: Text ('display_str')
        );
      }),
    );

    return result;
  }
}

enter image description here

热门问题
推荐问题
最新问题