Flutter test: How to check the background color of a widget during integration test

I have a simple flutter app available on github having both Light and dark themes with a "Theme switch" button. On the main page, I have a Scaffold widget that takes it's background color property from the theme. Is there a way to check the background color of the Scaffold before and after switching themes during integration test ?

Here's what the code looks like:

import 'package:flutter/material.dart';
import 'package:sample/Themes.dart';

import 'constants.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool isDark = false;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Color test',
      theme: isDark ? darkTheme : lightTheme,
      darkTheme: darkTheme,
      home: Builder(
        builder: (context) => Scaffold(
            backgroundColor: Theme.of(context).backgroundColor,
            appBar: AppBar(
              title: Text('Background Color Test'),
              centerTitle: true,
              backgroundColor: Theme.of(context).primaryColor,
            ),
            body: SafeArea(
              child: Center(
                child: Column(
                  mainAxisAlignment: mainAxisAlignment,
                  children: [
                    RoundButton(
                      key: ValueKey('Button'),
                      onTap: () {
                        setState(() {
                          isDark = !isDark;
                        });
                      },
                      icon: isDark ? Icons.light_mode : Icons.dark_mode,
                    )
                  ],
                ),
              ),
            )),
      ),
    );
  }
}


Read more here: https://stackoverflow.com/questions/68477166/flutter-test-how-to-check-the-background-color-of-a-widget-during-integration-t

Content Attribution

This content was originally published by Joshua Erinosho at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: