Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Last active May 2, 2026 11:58
Show Gist options
  • Select an option

  • Save angelabauer/cdf727bed6e5d82fb45e3b24b8c19c35 to your computer and use it in GitHub Desktop.

Select an option

Save angelabauer/cdf727bed6e5d82fb45e3b24b8c19c35 to your computer and use it in GitHub Desktop.
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(
MaterialApp(
home: BallPage(),
),
);
class BallPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
backgroundColor: Colors.blue.shade900,
title: Text('Ask Me Anything'),
),
body: Ball(),
);
}
}
class Ball extends StatefulWidget {
@override
_BallState createState() => _BallState();
}
class _BallState extends State<Ball> {
int ballNumber = 1;
@override
Widget build(BuildContext context) {
return Center(
child: FlatButton(
onPressed: () {
ballNumber = Random().nextInt(5);
print(ballNumber);
},
child: Image.asset('images/ball1.png'),
),
);
}
}
@Aladex

Aladex commented Apr 25, 2020

Copy link
Copy Markdown
import 'dart:math';

import 'package:flutter/material.dart';

void main() => runApp(
      MaterialApp(
        home: BallPage(),
      ),
    );

class BallPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.blue.shade900,
        title: Center(
          child: Text('Ask Me Anything'),
        ),
      ),
      body: Ball(),
    );
  }
}

class Ball extends StatefulWidget {
  @override
  _BallState createState() => _BallState();
}

class _BallState extends State<Ball> {

  int variantBall = 1;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.blue,
      child: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Expanded(
              child: FlatButton(
                onPressed: (){
                  setState(() {
                    variantBall = Random().nextInt(5) + 1;
                  });
                },
                child: Image.asset("images/ball$variantBall.png"),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

@bingli224

Copy link
Copy Markdown

setState ( ( ) => ballNumber = Random ( ).nextInt ( 4 ) + 1 );

It works.

setState ( ( ) => ballNumber = Random ( ).nextInt ( 5 ) + 1 );

I can get error:

════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: images/ball6.png

The document (https://api.dart.dev/stable/2.8.2/dart-math/Random/nextInt.html) shows it's max exclusive.

What's wrong?

@Oneknight

Copy link
Copy Markdown

setState ( ( ) => ballNumber = Random ( ).nextInt ( 4 ) + 1 );

It works.

setState ( ( ) => ballNumber = Random ( ).nextInt ( 5 ) + 1 );

I can get error:

════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: images/ball6.png

The document (https://api.dart.dev/stable/2.8.2/dart-math/Random/nextInt.html) shows it's max exclusive.

What's wrong?

Check your syntax:

setState(() {ballNumber = Random().nextInt(5) + 1;});

@poeteo

poeteo commented Jul 14, 2020

Copy link
Copy Markdown

My comment about setting the ballnumber variable to zero (0) and the solution containing the line "int ballnumber = 1" :

In earlier example, we had already seen which errors happen when the resulting random is out of bounds and how we should fix that.

I think Angela gave us an out of bounds condition intentionally, trusting us to correct it to make it work. Which we did in the end.

So, from this little surprise she prepared for us, we should have learned that "design documents and instructions are not equal to working solutions".

@gaetanosestito

Copy link
Copy Markdown
import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(
      MaterialApp(
        home: BallPage(),
      ),
    );

class BallPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      appBar: AppBar(
        backgroundColor: Colors.blue.shade900,
        title: Text('Ask Me Anything'),
      ),
      body: Ball(),
    );
  }
}

class Ball extends StatefulWidget {
  @override
  _StateBall createState() => _StateBall();
}

class _StateBall extends State<Ball> {
  int ballNumber = 1;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: FlatButton(
        onPressed: () {
          ballNumber = Random().nextInt(5) + 1;
          print('I got clicked and number is $ballNumber');
        },
        child: Image.asset('images/ball$ballNumber.png'),
      ),
    );
  }
}

@AshraF3a

Copy link
Copy Markdown

using setstate

import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(
      MaterialApp(
        home: BallPage(),
      ),
    );

class Ball extends StatefulWidget {
  @override
  _BallState createState() => _BallState();
}

class _BallState extends State<Ball> {
  int ballNumber = 0;
  @override
  Widget build(BuildContext context) {
    setState(() {
      ballNumber = Random().nextInt(5)+1;
    });
    return Center(
        child: FlatButton(
      onPressed: () {
        print("$ballNumber");
      },
      child: Image.asset("images/ball$ballNumber.png"),
    ));
  }
}

class BallPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Ask Me Anything"),
        backgroundColor: Colors.blue.shade900,
      ),
      backgroundColor: Colors.blue,
      body: Ball(),
    );
  }
}

@shubhaam13

Copy link
Copy Markdown

import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(
MaterialApp(
home: BallPage(),
),
);
class BallPage extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
backgroundColor: Colors.blue.shade900,
title: Text('Ask Me Anything'),
),
body: Ball(),
);
}
}
class Ball extends StatefulWidget {
@OverRide
_BallState createState() => _BallState();
}

class _BallState extends State {
int ballNumber = 1;

@OverRide
Widget build(BuildContext context) {
return Center(
child : FlatButton(
onPressed: () {
setState(() {
ballNumber = Random().nextInt(5)+1;
print('$ballNumber');
});
},
child: Image.asset('images/ball$ballNumber.png'),

),
);

}
}

@jasuthe

jasuthe commented Aug 18, 2020

Copy link
Copy Markdown

import 'package:flutter/material.dart';

void main() => runApp(
MaterialApp(
home: Ballpage(),
),
);

class Ballpage extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.teal,
appBar: AppBar(title: Text("Magic Ball")),
body: Ball(),
);
}
}

class Ball extends StatefulWidget {
@OverRide
_BallState createState() => _BallState();
}

class _BallState extends State {
int ballNumber = 1;
@OverRide
Widget build(BuildContext context) {
return Center(
child: FlatButton(
onPressed: () {},
child: Image.asset('images/ball1.png'),
),
);
}
}

@BatuhanAydoner

Copy link
Copy Markdown

This is mine.

import 'dart:math';

import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: BallPage(),
));
}

class BallPage extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
title: Text("Ask Me Anything"),
backgroundColor: Colors.blue[900],
),
body: Ball(),
);
}
}

class Ball extends StatefulWidget {
@OverRide
_BallState createState() => _BallState();
}

class _BallState extends State {
int ballNumber = 1;
@OverRide
Widget build(BuildContext context) {
return Center(
child: FlatButton(
onPressed: () {
setState(() {
ballNumber = 1 + Random().nextInt(5);
});
},
child: Image.asset("images/ball$ballNumber.png")),
);
}
}

@petyaas

petyaas commented Aug 30, 2020

Copy link
Copy Markdown

import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal[700],
appBar: AppBar(
title: Text('Sorag!!'),
backgroundColor: Colors.teal[900],
centerTitle: true,
),
body: Myapp(),

),),
);

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

class _MyappState extends State {
int i=1;

void cheng(){
setState(() {
i=Random().nextInt(5)+1;
});
}

@OverRide
Widget build(BuildContext context) {
return
Center(
child: Expanded(
child: FlatButton(
onPressed: (){
cheng();
},
child: Image.asset('images/ball$i.png'),
),
),
);
}
}

@EngAbdalrhman

Copy link
Copy Markdown

what about that
import 'package:flutter/material.dart';
import 'dart:math';

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

class mball extends StatefulWidget {
mball({Key key}) : super(key: key);
@OverRide
_mballState createState() => _mballState();
}

class _mballState extends State {
var bnum = 2;
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
backgroundColor: Colors.blue[900],
title: Text('Ask Me Anything'),
),
body: Container(
child: Center(
child: FlatButton(
onPressed: () {
setState(() {
bnum = Random().nextInt(5) + 1;
});
},
child: Image.asset('images/ball$bnum.png')),
),
),
),
);
}
}

@ipvoodoo

ipvoodoo commented Sep 10, 2020

Copy link
Copy Markdown

import 'dart:math';

import 'package:flutter/material.dart';

void main() => runApp(
      MaterialApp(
        home: BallPage(),
      ),
    );
class BallPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var title = 'Ask Me Anything';
    return Scaffold(
      backgroundColor: Colors.blue,
      appBar: AppBar(
        backgroundColor: Colors.blue.shade900,
        title: Text(title),
      ),
      body: Ball(),
    );
  }
}
class Ball extends StatefulWidget {
  @override
  _BallState createState() => _BallState();
}
class _BallState extends State<Ball> {
  var ballNumber = 1;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: FlatButton(
        onPressed: () {
          setState(() {
            ballNumber = Random().nextInt(5) + 1;
          });
          print('I got clicked $ballNumber');
        },
        child: Image.asset('images/ball$ballNumber.png'),
      ),
    );
  }
}

everything works

@ozoguzhan

Copy link
Copy Markdown

import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(
MaterialApp(
home: BallPage(),
),
);

class BallPage extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
backgroundColor: Colors.blue.shade900,
title: Text('Ask Me Anything'),
),
body: Ball(),
);
}
}

class Ball extends StatefulWidget {
@OverRide
_BallState createState() => _BallState();
}

class _BallState extends State {
int ballNumber = 1;
@OverRide
Widget build(BuildContext context) {
return Center(
child: FlatButton(
child: Image.asset('images/ball$ballNumber.png'),
onPressed: () {
ballNumber = Random().nextInt(5) + 1;
print(ballNumber);
},
),
);
}
}

here's my version.

@tonyjoshydev

Copy link
Copy Markdown

I had done this like the dicee app:

import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(
MaterialApp(
home: BallPage(),
),
);

class BallPage extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
backgroundColor: Colors.blue.shade900,
title: Text('Ask Me Anything'),
),
body: Ball(),
);
}
}

class Ball extends StatefulWidget {
@OverRide
_BallState createState() => _BallState();
}

class _BallState extends State {
int ballnumber = 1;
@OverRide
Widget build(BuildContext context) {
return Center(
child: FlatButton(
onPressed: () {
ballnumber = Random().nextInt(5) + 1;
print('ballnumber');
},
child: Image.asset('images/ball$ballnumber.png')),
);
}
}

@EngMohamed3382

Copy link
Copy Markdown

import 'dart:math';
import 'package:flutter/material.dart';

void main() {
return runApp(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.black12,
appBar: AppBar(
title: Center(child: Text('Boss')),
backgroundColor: Colors.black,
),
body: MagicBall(),
),
),
);
}

class MagicBall extends StatefulWidget {
@OverRide
_MagicBallState createState() => _MagicBallState();
}

class _MagicBallState extends State {
int Facenumber = 1;

void changeFace() {
setState(() {
Facenumber = Random().nextInt(5) + 1;
});
}

@OverRide
Widget build(BuildContext context) {
return Center(
child: Row(
children: [
Expanded(
child: FlatButton(
onPressed: () {
changeFace();
},
child: Image.asset('images/ball$Facenumber.png'),
),
),
],
),
);
}
}

@ibushraabbasi

ibushraabbasi commented Jan 11, 2021

Copy link
Copy Markdown

import 'dart:math';
import 'package:flutter/material.dart';

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

class BallPage extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.blueAccent,
appBar: AppBar(
backgroundColor: Colors.blue,
title: Center(
child: Text('Ask me everything'),
),
),
body: Ball(),
),
);
}
}

class Ball extends StatefulWidget {
@OverRide
_BallState createState() => _BallState();
}

class _BallState extends State {
int ballNumber = 0;
@OverRide
Widget build(BuildContext context) {
return Center(
child: FlatButton(
onPressed: () {
ballNumber = Random().nextInt(5) + 1;
print('ball number: $ballNumber');
},
child: Image.asset('images/ball$ballNumber.png'),
),
);
}
}

@thibaudet81

Copy link
Copy Markdown

The Angela Yu's solution is perfect

@Harsh-ux007

Copy link
Copy Markdown

import 'dart:math';
import 'package:flutter/material.dart';

void main() => runApp(
MaterialApp(
home: BallPage(),
),
);

class BallPage extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[700],
appBar: AppBar(
backgroundColor: Colors.blue[200],
title: Text('Ask Me Anything'),
),
body: Ball());
}
}

class Ball extends StatefulWidget {
Ball({Key key}) : super(key: key);

@OverRide
_BallState createState() => _BallState();
}

class _BallState extends State {
int ballnumber = 0;
@OverRide
Widget build(BuildContext context) {
return Center(
child: Container(
child: FlatButton(
onPressed: () {
ballnumber = Random().nextInt(4);
print('ballnumber');
},
child: Image.asset('images/ball.png'),
)));
}
}
print('ballnumber') it is wrong??

@KhuloodBatis

Copy link
Copy Markdown

2021-12-28-09-34-49

ghost commented Jan 2, 2022

Copy link
Copy Markdown

You guys have to set the state for the images to change. I did'nt see any posts with setstate added in the onPressed.

Screen Shot 2022-01-02 at 13 09 08

@mhbm

mhbm commented Jan 17, 2022

Copy link
Copy Markdown
import 'dart:math';

import 'package:flutter/material.dart';

void main() => runApp(
      MaterialApp(
        home: BallPage(),
      ),
    );

class BallPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      appBar: AppBar(
        title: Text('Ask Me Anything'),
        backgroundColor: Colors.blue.shade900,
      ),
      body: Ball(),
    );
  }
}

class Ball extends StatefulWidget {
  @override
  _BallState createState() => _BallState();
}

class _BallState extends State<Ball> {
  int ballNumber = 1;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: <Widget>[
          Expanded(
              child: FlatButton(
            onPressed: () => {shuffleImage()},
            child: Image.asset('images/ball$ballNumber.png'),
          ))
        ],
      ),
    );
  }

  void shuffleImage()
  {
    setState(() {
      ballNumber = Random().nextInt(5) + 1;
      print("I got clicked");
    });
  }
}

My solution

@spy-der-web

Copy link
Copy Markdown

//My solution

// ignore_for_file: prefer_const_constructors
import 'dart:math';
import 'package:flutter/material.dart';

void main() => runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: BallPage(),

),
);

class BallPage extends StatelessWidget {
const BallPage({Key? key}) : super(key: key);

@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
title: Text("Ask Me Anything"),
backgroundColor: Colors.blue.shade900,
),
body: Ball(),
);
}
}

class Ball extends StatefulWidget {
const Ball({Key? key}) : super(key: key);

@OverRide
_BallState createState() => _BallState();
}

class _BallState extends State {
int ballNumber = 0;

@OverRide
Widget build(BuildContext context) {
return Center(
child: TextButton(
onPressed: () {
setState(() {
ballNumber = Random().nextInt(5);
});

        print(ballNumber);
      },
      child: Image.asset('images/ball1.png'),
  ),
);

}
}

@Synzu

Synzu commented Jun 22, 2022

Copy link
Copy Markdown

import 'dart:math';
import 'package:flutter/material.dart';

void main() => runApp(
MaterialApp(
home: Scaffold(
backgroundColor:Colors.blue,
appBar: AppBar(
backgroundColor: Color.fromARGB(255, 6, 20, 27),
title: Text('Ask Me Anything'),
),
body: Ball (

        ),
    ),
  ),
);

class Ball extends StatefulWidget {

@OverRide
State createState() => _BallPageState();

}

class _BallPageState extends State {

int ballNumber = 1;

@OverRide

Widget build(BuildContext context) {

return Center(
  
  child: Padding(
    padding: const EdgeInsets.all(16.0),
    child: Row(
     
      children: [
        
        Center (
           
           child: Container(
            
            width: 350,
            child: TextButton(
               onPressed: () {
                 setState(() {
                 ballNumber  = Random().nextInt(5);
                 
               });
                 print (ballNumber);
            },
                child: Image.asset('images/ball1.png')),
            
          ),
        ),
      ],
    ),
  ),
);

}
}

@MiladZarour

Copy link
Copy Markdown
import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(
      MaterialApp(
        home: BallPage(),
      ),
    );

class BallPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      appBar: AppBar(
        backgroundColor: Colors.blue.shade900,
        title: Text('Ask Me Anything'),
      ),
      body: Ball(),
    );
  }
}

class Ball extends StatefulWidget {
  @override
  _BallState createState() => _BallState();
}

class _BallState extends State<Ball> {
  int ballNumber = 1;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: <Widget>[
          Expanded(
              child: TextButton(
            onPressed: () => {shuffleImage()},
            child: Image.asset('images/ball$ballNumber.png'),
          ))
        ],
      ),
    );
  }

  void shuffleImage() {
    setState(() {
      ballNumber = Random().nextInt(5) + 1;
      print("I got clicked");
    });
  }
}

@Hackathonwave

Copy link
Copy Markdown

import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(
MaterialApp(
home: BallPage(),
),
);

class BallPage extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black54,
appBar: AppBar(
backgroundColor: Colors.black,
title: Text('"NOTHING IMPOSSIBLE TO DO"'),
),
body: Ball(),
);
}
}

class Ball extends StatefulWidget {
@OverRide
_BallState createState() => _BallState();
}

class _BallState extends State {
@OverRide
int ballNumber = 1;

Widget build(BuildContext context) {
return Center(
child: TextButton(
onPressed: () {
ballNumber = Random().nextInt(5);
print(ballNumber);
},
child: Image.asset('images/ball1.png'),
),
);
}
}

@souravs3

Copy link
Copy Markdown

import 'dart:math';

import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: MagicBall(),
));
}

class MagicBall extends StatelessWidget {
const MagicBall({super.key});

@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color.fromARGB(255, 174, 29, 18),
appBar: AppBar(
backgroundColor: Color.fromARGB(255, 142, 20, 20),
title: Text(
'Ask me Anything',
style: TextStyle(color: Colors.white),
),
centerTitle: true,
),
body: Ball(),
);
}
}

class Ball extends StatefulWidget {
const Ball({super.key});

@OverRide
State createState() => _BallPage();
}

class _BallPage extends State {
int ballnumber = 1;
@OverRide
Widget build(BuildContext context) {
return Center(
child: TextButton(
onPressed: () {
setState(() {
ballnumber = Random().nextInt(5) + 1;
});
print('Button is pressed!');
},
child: Image.asset('images/ball${ballnumber}.png'),
),
);
}
}

@amgadsokkary

Copy link
Copy Markdown
Screen.Recording.2026-05-02.at.2.57.26.PM.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment