Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Last active May 21, 2024 01:54
Show Gist options
  • Save angelabauer/3b3919bc59100d06997aec2c3d4c57ab to your computer and use it in GitHub Desktop.
Save angelabauer/3b3919bc59100d06997aec2c3d4c57ab 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: () {
setState(() {
ballNumber = Random().nextInt(5) + 1;
});
},
child: Image.asset('images/ball$ballNumber.png'),
),
);
}
}
@Harsh-ux007
Copy link

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 = 1;
@OverRide
Widget build(BuildContext context) {
return Center(
child: Container(
child: FlatButton(
onPressed: () {
setState(() {
ballnumber = Random().nextInt(5) + 1;
});
},
child: Image.asset('images/ball.png'),
)));
}
}

my code is above the app is running but id does not show any image in my device(running the app in physical device).is is showing me this message which i am posting below this.please help me if you found something wrong.

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

When the exception was thrown, this was the stack
#0 PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:225

#1 AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:668

@faruking
Copy link

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 = 1;
@OverRide
Widget build(BuildContext context) {
return Center(
child: Container(
child: FlatButton(
onPressed: () {
setState(() {
ballnumber = Random().nextInt(5) + 1;
});
},
child: Image.asset('images/ball.png'),
)));
}
}

my code is above the app is running but id does not show any image in my device(running the app in physical device).is is showing me this message which i am posting below this.please help me if you found something wrong.

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

When the exception was thrown, this was the stack
#0 PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:225

#1 AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:668

i hope you have solved this but if you haven't, your mistake lies where you failed to add the string interpolation 'ballnumber' to your assetimage i.e, change 'images/ball.png' to 'images/ball$ballnumber.png'.

@xnjit
Copy link

xnjit commented Jun 16, 2021

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

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

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

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

class _BallState extends State {

int ballNumber = 1;
void RandomImage() {
setState(() {
ballNumber = Random().nextInt(5) + 1;
print(ballNumber);
});
}

@OverRide
Widget build(BuildContext context) {
return Center(
child: Container(
child: FlatButton(
onPressed: () {
RandomImage();
},
child: Image.asset('assets/ball$ballNumber.png')),
),
);
}
}

@xnjit
Copy link

xnjit commented Jun 16, 2021

child: Image.asset('images/ball.png'),

change this to child: Image.asset('images/ball$ballNumber.png')

@faruking
Copy link

child: Image.asset('images/ball.png'),

change this to child: Image.asset('images/ball$ballNumber.png')

his variable name(ballnumber) is different from yours(ballNumber). if he uses your solution without changing the variable name, the code wont still work.

@jhonsnow456
Copy link

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

void main(){
  runApp(
    MagicBall(),
  );
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.yellow.shade400,
        appBar: AppBar(
          title: Text('Ask Me Anything'),
          backgroundColor: Colors.green,
        ),
        body: AnswerBall(),
      ),
    );
  }
}

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

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

class _AnswerBallState extends State<AnswerBall> {
  int ballNumber = 1;

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

  @override
  Widget build(BuildContext context) {
    return Center(
      child: IconButton(
        onPressed: () {
          setState(() {
            updateBall();
          });
        },
        icon: Image.asset('images/ball$ballNumber.png'),
        iconSize: 500.0,
      )
    );
  }
}


@rakibmahmudkhan
Copy link

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 {

int ballNumber=1;

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

);

}
}

@MuhammadRahman2
Copy link

import 'dart:math';

import 'package:flutter/material.dart';

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

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

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

class _MyAppState extends State {
int imageNO = 1;

void changeImage() {
setState(() {
imageNO = Random().nextInt(5) +1;
});
}

@OverRide
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
centerTitle: true,
backgroundColor: Colors.blue[800],
title: Text("Ask me Every thing"),
),
body: Center(
child: Container(
child: TextButton(
onPressed: () {
changeImage();
},
child: Image.asset('images/ball$imageNO.png'),
),
),
),
),
);
}
}

@miamadi
Copy link

miamadi commented Aug 27, 2021

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

void main() => runApp(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.grey[300],
appBar: AppBar(
title: Center(
child: Text(
'Ask Me Anything',
style: TextStyle(
fontSize: 45.0,
fontWeight: FontWeight.w300,
),
),
),
backgroundColor: Colors.black12,
),
body: ballPage(),
),
),
);

class ballPage extends StatefulWidget {
@OverRide
_ballState createState() => _ballState();
}

class _ballState extends State {
int ballNumber = 1;
@OverRide
Widget build(BuildContext context) {
return Center(
child: Expanded(
child: FlatButton(
visualDensity: VisualDensity.standard,
focusColor: Colors.white,
onPressed: () {
setState(() {
ballNumber = Random().nextInt(5) + 1;
print('i got clicked');
});
},
child: Image.asset(('images/ball$ballNumber.png')),
),
),
);
}
}

@Armond23
Copy link

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

void main() => runApp(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.blue.shade100,
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text('magic 8 ball'),
),
body: SafeArea(
child: MagicBall(),
),
),
),
);

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

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

class _MagicBallState extends State {
int nextImages = 1;
@OverRide
Widget build(BuildContext context) {
return Center(
child: Column(

    children: [
      Expanded(
        
         
        child: FlatButton(
          onPressed: () {
            setState(() {
              nextImages = Random().nextInt(5) + 1;
            });
          },
          child: Image.asset('images/ball$nextImages.png'),
        ),
      ),
    ],
  ),
);

}
}

@SEAFromJahu
Copy link

import 'dart:math';

import 'package:flutter/material.dart';

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

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

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

class _MagicBallState extends State {
int ballNumber = 1;

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

@OverRide
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
title: Text('Pergunte Me Algo'),
),
body: Padding(
padding: const EdgeInsets.all(36.0),
child: SafeArea(
child: Center(
child: Expanded(
child: TextButton(
onPressed: () {
changeBall();
},
child: Image.asset('images/ball$ballNumber.png'),
),
),
),
),
),
),
);
}
}

@quaku21
Copy link

quaku21 commented Oct 25, 2021

I created a function to do the random number generation and I called it in the "onPresed:(){}" function

@hello-favour
Copy link

//please anything wrong with this
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.blue,
appBar: AppBar(
backgroundColor: Colors.blue.shade900,
title: Text("Ask Me Anything"),
centerTitle: true,
),
body: Ball(),
),
);
}
}

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

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

@KhuloodBatis
Copy link

2021-12-28-09-41-00.mp4

@elinteerie
Copy link

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(
appBar: AppBar(
title: Text('Ask Me Anything'),
backgroundColor: Colors.blue,
),
backgroundColor: Colors.blue[800],
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: OutlinedButton(
onPressed: () {
setState(() {
ballNumber = Random().nextInt(5) + 1;
});
},
child: Image(
image: AssetImage('images/ball$ballNumber.png'),
),
),
);
}
}

@mhbm
Copy link

mhbm commented Jan 17, 2022

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

// 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 = 1;

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

      },
      child: Image.asset('images/ball$ballNumber.png'),
  ),
);

}
}

@uzbekcard
Copy link

``import 'dart:math';

import 'package:flutter/material.dart';

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

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

@OverRide
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
backgroundColor: Colors.blue.shade900,
title: Center(
child: Text("MENDAN SO'RORASAN"),
),
),
body: Asosiy(),
),
);
}
}

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

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

class _AsosiyState extends State {
int raqamalar = 1;

@OverRide
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
onPressed: () {
setState(() {
raqamalar = Random().nextInt(5) + 1;
});
},
child: Image.asset('images/ball$raqamalar.png'),
),
],
);
}
}

@Vatoka
Copy link

Vatoka commented Apr 17, 2022

I added one of my own photos that says "Press to start" that's why there are 6 photos instead of 5.
I see you did it a bit different than me... is one way better than the other?
Mine works as intended...

I thought maybe I can remove the Row on line 45 but then I receive errors in the console even though everything appeared to be functioning.

[root]
└── MaterialApp
└── Scaffold
├── MyApp
│ └── Center
│ └── Row
│ └── Expanded
│ └── TextButton
│ └── Padding
│ └── Image
└── AppBar
└── Text

FlutterMagic8BallDemo

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

void main() {
  return runApp(
    MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.blueAccent,
        appBar: AppBar(
          centerTitle: true, // Needed for web
          title: Text(
            'Ask me a Yes or No Question',
            style: GoogleFonts.anton(
              textStyle: const TextStyle(
                color: Colors.white,
                letterSpacing: 1.0,
              ),
            ),
          ),
          backgroundColor: Colors.blue,
        ),
        body: const Main(),
      ),
    ),
  );
}

class Main extends StatefulWidget {
  const Main({Key? key}) : super(key: key);
  @override
  State<Main> createState() => _MainState();
}

class _MainState extends State<Main> {
  int magicResponse = 6;
  void regenMagicResponse() {
    magicResponse = Random().nextInt(5) + 1;
    //print('Response is $magicResponse');
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: [
          Expanded(
            child: TextButton(
              onPressed: () {
                setState(() {
                  regenMagicResponse();
                });
              },
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Image.asset('images/ball$magicResponse.png'),
              ),
            ),
          )
        ],
      ),
    );
  }
}

@MathManiaTD
Copy link

MathManiaTD commented Jul 22, 2022

Most of comments was "In 5 mins DONE" , "It's a piece of cake" bla bla..
I wanna say; So, What then? I have done it with multiple mistakes and in along term. This was my result. It makes me happy to create something virtual.

Anyway;
I used instead of . It was working with FB but there was a warning that says, it was old versions stuff.

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

import 'dart:math';
// imports a library

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: TextButton(
onPressed: () {
setState(() {
ballNumber = Random().nextInt(5) + 1;
print('$ballNumber');
});
},
child: Image.asset('images/ball$ballNumber.png'),
),
);
}
}`

@maniishbhusal
Copy link

`import 'dart:math';

import 'package:flutter/material.dart';

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

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

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

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

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

class _BallState extends State {
int ballNumber = 1;
@OverRide
Widget build(BuildContext context) {
return Container(
color: Colors.blue,
child: Center(
child: TextButton(
onPressed: (() {
setState(() {
ballNumber = Random().nextInt(5) + 1;
print("Button Clicked");
});
}),
child: Image.asset("images/ball$ballNumber.png"),
),
),
);
}
}
`

@goodnewsjames
Copy link

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(
backgroundColor: Colors.blue[900],
title: Text('Ask Me Anything'),
),
body: Ball(),
);
}
}

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

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

class _BallState extends State {
int ballNumber = 1;
@OverRide
Widget build(BuildContext context) {
return Center(
child: TextButton(
onPressed: () {
setState(() {
ballNumber = Random().nextInt(5) + 1;

      });
    },
    child: Image(
      image: AssetImage('images/ball$ballNumber.png'),
    ),
  ),
);

}
}

@MiladZarour
Copy link

MiladZarour commented Dec 10, 2022



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");
    });
  }
}



@adisheikh121
Copy link

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

void main() {
  return runApp(
    MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.blue.shade300,
        appBar: AppBar(
          title: const Text('Ask Me Anything'),
          backgroundColor: Colors.blue.shade900,
        ),
        body: MagicEightBall(),
      ),
    ),
  );
}

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

  @override
  State<MagicEightBall> createState() => _MagicEightBallState();
}

class _MagicEightBallState extends State<MagicEightBall> {
  int ball_number = 1;

  void ChangeBallNumber() {
    setState(() {
      ball_number = Random().nextInt(5) + 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Expanded(
        child: TextButton(
          onPressed: () {
            ChangeBallNumber();
          },
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Image.asset('images/ball$ball_number.png'),
          ),
        ),
      ),
    );
  }
}

@ayushbharshankar
Copy link

Screen.Recording.2023-04-03.at.11.06.30.AM.mov

Done.

@omar-el-samahy
Copy link

I Did It!!
I Tried To Make It More Interactive By Typing The Questions But Didn't Know How Yet

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

void main() {
return runApp(
MaterialApp(
title: 'Ask Me Anything',
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white30,
centerTitle: true,
title: Text(
'Magic 8 Ball',
style: TextStyle(
fontSize: 20.0,
color: Colors.black,
),
),
),
body: SafeArea(child: magic()),
),
),
);
}
class magic extends StatefulWidget {
const magic({super.key});

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

class _magic extends State {
int bn = 1;
void rand() {
setState(
() {
bn = Random().nextInt(5) + 1;
},
);
}

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

@farmanali00742
Copy link

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

void main() {
return runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
backgroundColor: Color.fromARGB(255, 0, 84, 153),
title: Text('Ask Me Anything'),
),
body: Magicball(),
),
));
}

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

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

class _MagicballState extends State {
int BallNumber = 1;
@OverRide
Widget build(BuildContext context) {
return Center(
child: Row(
children: [
Container(
child: Expanded(
child: TextButton(
onPressed: () {
setState(() {
BallNumber = Random().nextInt(5) + 1;
});
},
child: Image.asset(
'images/ball$BallNumber.png',
),
),
),
)
],
),
);
}
}

@Hackathonwave
Copy link

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: () {
setState(
() {
ballNumber = Random().nextInt(5) + 1;
},
);
},
child: Image.asset('images/ball$ballNumber.png'),
),
);
}
}

@Fabilqees
Copy link

Yay, I did it right, we really don't know what we are capable of until we try.

import 'dart:math';

import 'package:flutter/material.dart';

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

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

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

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

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

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

@xmkhatshwa
Copy link

Yes! I did it!

import 'dart:math';

import 'package:flutter/material.dart';

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

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

  @override
  State<Ball> createState() => _BallState();
}

class _BallState extends State<Ball> {

  int ballNumber = 1;

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

      )
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      appBar: AppBar(
        title: Text('Ask Me Anything',
          style: TextStyle(
              color: Colors.white
          ),
        ),
        backgroundColor: Colors.blue.shade900,
      ),
      body: Ball(),
    );
  }
}

image

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