mercredi 10 mars 2021

Flutter Widget Unit Test

I did the Flutter app and I need to test it. I want to test the background color changing, but I don't know how to implement it. I think it need to looks like: expect(find.text('Hey there'), findsOneWidget); I have tried with await tester.pumpWidget(RainbowApp()); but how to get to backgroundColor or backColor?

main.dart:

import 'dart:math';

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

class RainbowApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Rainbow',
      home: RandColorBackground(),
    );
  }
}

class RandColorBackground extends StatefulWidget {
  @override
  _RandColorBackgroundState createState() => _RandColorBackgroundState();
}

class _RandColorBackgroundState extends State<RandColorBackground> {
  Color backColor = Colors.grey[300];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: backColor,
      body: Stack(
        children: <Widget>[
          Center(
            child: Text(
              'Hey there',
              style: TextStyle(
                fontSize: 35.0,
                fontWeight: FontWeight.bold,
                letterSpacing: 3,
                fontFamily: 'Montserrat',
              ),
            ),
          ),
          GestureDetector(
            onTap: () {
              Feedback.forTap(context);
              setState(() {
                backColor = randomColorGeneration();
              });
            },
          ),
        ],
      ),
    );
  }

  Color randomColorGeneration() {
    return Colors.primaries[Random().nextInt(Colors.primaries.length)];
  }

  Color get bgColor => backColor;
}

Aucun commentaire:

Enregistrer un commentaire