Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Created March 7, 2019 12:19
Show Gist options
  • Save angelabauer/76043b339eeee1142b8d02ee05bde15c to your computer and use it in GitHub Desktop.
Save angelabauer/76043b339eeee1142b8d02ee05bde15c to your computer and use it in GitHub Desktop.
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> {
@override
Widget build(BuildContext context) {
return Center(
child: FlatButton(
onPressed: () {
print('I got clicked');
},
child: Image.asset('images/ball1.png'),
),
);
}
}
@mohamedabdelshafy
Copy link

thinks youuuuuuuuuuuuuuuuuuuuuuuuu
^_^
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 ballCounter = 1;

void counterBall() {
setState(() {
ballCounter = Random().nextInt(5) + 1;
});
}

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

@ibushraabbasi
Copy link

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 {
@OverRide
Widget build(BuildContext context) {
return Center(
child: FlatButton(
onPressed: () {
print('I got Clicked');
},
child: Image.asset('images/ball1.png'),
),
);
}
}

@prateek-aher
Copy link

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

class _BallState extends State<Ball> {
@OverRide
Widget build(BuildContext context) {
return Center(
child: FlatButton(
onPressed: () {
print('I got Clicked');
},
child: Image.asset('images/ball1.png'),
),
);
}
}

@ibushraabbasi You forgot to add type to the state object. In fact, everyone except Angela forgot that.

@Ruveera
Copy link

Ruveera commented Mar 8, 2021

Android studio says FlatButton is deprecated use TextButton instead anybody notice that?

@keymistress
Copy link

Android studio says FlatButton is deprecated use TextButton instead anybody notice that?

Yes, it's been deprecated. Use TextButton instead. Seems to work fine.

import 'package:flutter/material.dart';

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

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

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

class _BallState extends State<Ball> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: TextButton(
        onPressed: () {
          print('I got clicked');
        },
        child: Image.asset('images/ball1.png'),
      ),
    );
  }
}

@sameeranand12
Copy link

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

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

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

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

class _BallPageState extends State {
int ballNumber = 1;
void changeface(){
setState(() {
ballNumber = Random().nextInt(4) +1;
print('I got clicked');
});

}
@OverRide
Widget build(BuildContext context) {

return Scaffold(
  backgroundColor: Colors.white,
  appBar: AppBar(
    backgroundColor: Colors.black87,
    title: Center(child: Text(' MAGIC BALL')),
  ),
  body: SafeArea(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Expanded(
          child: TextButton(
              onPressed: () {
                changeface();
              },
              child: Image.asset('images/ball$ballNumber.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')),
),
),
);
}
}

@bookwarmdev
Copy link

// FlatButton is now outdate Inkwell will be a grate help.

class _BallState extends State {
@OverRide
Widget build(BuildContext context) {
return Center(
child: InkWell(
onTap: (){
print('It get click');
},
child: Image.asset('images/ball1.png'),
),
);
}
}

@KhuloodBatis
Copy link

2021-12-28-09-29-19

@mhbm
Copy link

mhbm commented Jan 17, 2022

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> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: <Widget>[
          Expanded(
              child: FlatButton(
            onPressed: () => {print('I got clicked')},
            child: Image.asset('images/ball1.png'),
          ))
        ],
      ),
    );
  }
}

My solution

@prakaasnike
Copy link

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Magic Ball';
// This widget is the root of your application.
@OverRide
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
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: const Text('Ask Me Anything'),
backgroundColor: Colors.blue.shade900,
),
body: const Ball(),
);
}
}

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

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

class _BallState extends State {
@OverRide
Widget build(BuildContext context) {
return Center(
child: TextButton(
onPressed: () {
print('I got clicked');
},
child: Image.asset('images/ball1.png'),
),
);
}
}
mb4 sol

@Synzu
Copy link

Synzu commented Jun 22, 2022

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: BallPage (

        ),
    ),
  ),
);

class BallPage extends StatefulWidget {

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

class _BallPageState extends State {
@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: () {
                 print('I got pressed ');
            },
                child: Image.asset('images/ball1.png')),
            
          ),
        ),
      ],
    ),
  ),
);

}
}

@Synzu
Copy link

Synzu commented Jun 22, 2022

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) +1 ;
                
               });
                 print (ballNumber);
            },
                child: Image.asset('images/ball$ballNumber.png')),
            
          ),
        ),
      ],
    ),
  ),
);

}
}

@wkzmrj
Copy link

wkzmrj commented Aug 10, 2022

class _BallState extends State {
@OverRide
Widget build(BuildContext context) {
return Center(
child: TextButton(
onPressed: () {
print('I got clicked');
},
child: Image.asset('images/ball1.png'),
),
);
}
}

@Rocktopus101
Copy link

The code provided should be updated as FlatButton is now deprecated and can be replaced by TextButton or ElevatedButton.

@wkzmrj
Copy link

wkzmrj commented Nov 30, 2022 via email

@MiladZarour
Copy link

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> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: TextButton(
        onPressed: () {
          print('I got clicked');
        },
        child: Image.asset('images/ball1.png'),
      ),
    );
  }
}

@wkzmrj
Copy link

wkzmrj commented Mar 18, 2023 via email

@Hackathonwave
Copy link

import 'package:flutter/material.dart';

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
Widget build(BuildContext context) {
return Center(
child: TextButton(
onPressed: () {
print('I got clicked');
},
child: Image.asset('images/ball1.png'),
),
);
}
}

@wkzmrj
Copy link

wkzmrj commented Jul 28, 2023 via email

@t-tatarski
Copy link

import 'package:flutter/material.dart';

void main() {
runApp(const MaterialApp(
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 {
@override
Widget build(BuildContext context) {
return TextButton(
onPressed: () {
print('i got clicked');
},
child: Center(
child: Image.asset('images/ball1.png'),
),
);
}
}

@wkzmrj
Copy link

wkzmrj commented Sep 23, 2023 via email

@AdityaAmara
Copy link

I couldn't find FlatButton in the latest Flutter SDK. I have used TextButton instead of FlatButton. The code as follows:

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[100],
      appBar: AppBar(
        title: Text('Ask Me Anything'),
      ),
      body: Ball()
    );
  }
}

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

class _BallState extends State<Ball> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: TextButton(
          onPressed: () {
            print("I got clicked");
          },
          child: Image.asset('images/ball1.png')
      ),
    );
  }
}

@xmkhatshwa
Copy link

xmkhatshwa commented May 11, 2024

This is how I got my code to work:

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> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: TextButton(
          onPressed: () {
              print('Button clicked!');
            },
          child: Image.asset('images/ball1.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

Thank you @angelabauer

@wkzmrj
Copy link

wkzmrj commented May 11, 2024 via email

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