0
votes

I have a StatefulWidget State class in flutter.

I have declared some static const Color member variables. When I try to use these Colors, they are not being drawn. When I take the same Color ctor, and inline it where the static member variables were used, it works.

Am I missing something about member variable initialization in dart?

Observe the local variable color in the build method. If the ternary operator uses the inline ctor color, it works. If it uses the static member var DARK_COLOR....nothing draws.

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;
    }
}
1
I imagine it's drawing fine... its just that the color code #ffffffff refers to solid white. I imagine that's not the color you want to represent your "dark color". - Abion47

1 Answers

0
votes

You code works greate, may be the color FFFFFFFF is not what you want
see color plate below

enter image description here

enter image description here

full test code
dark color use 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