Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Last active February 6, 2025 16:02
Show Gist options
  • Select an option

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

Select an option

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'),
),
);
}
}
@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'),
),
),
],
),
);
}
}

@FarmanAliDevdynamos
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

@Janvierscode
Copy link

main dart

you should all try https://ray.so/ , its awesome sharing code snippets. you will thank me later. cheers

@EdouardKamole
Copy link

Dear Dr Angela,
i'm kamole burume edouard
I wanted to inform you that while I'm studying this course in 2024, I’ve noticed that some elements in the code provided, such as FlatButton, have been deprecated and replaced by newer widgets like TextButton. Flutter has undergone updates, and some older components are no longer in use. I’ve updated my code accordingly to ensure compatibility with the current Flutter framework. thank you for the docs you always provide are the one helping to move well and advanced i really love the course i'm always greatfull to have you, coz you've been there for me again for Web Development and here were together again .
so this is my work also

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

void main() => runApp(
MaterialApp(
home: BallPage(),
theme: ThemeData(
primaryColor: Colors.deepPurple,
),
),
);

class BallPage extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.deepPurple[100],
appBar: AppBar(
backgroundColor: Colors.deepPurple,
title: Text('Ask Anything Magic Ball'),
centerTitle: true,
elevation: 5,
),
body: Ball(),
);
}
}

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

class _BallState extends State {
int ballNumber = 1;

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

@OverRide
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Ask a Question & Tap the Ball!',
style: TextStyle(
fontSize: 24,
color: Colors.deepPurple[900],
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 20),
GestureDetector(
onTap: shakeBall,
child: Image.asset('images/ball$ballNumber.png', height: 150),
),
],
),
);
}
}

@SYeameen
Copy link

SYeameen commented Feb 6, 2025

Uploading ray-so-export (1).png…

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