Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Created March 7, 2019 12:18
Show Gist options
  • Save angelabauer/83ff2026724618c4815f986743f6d70e to your computer and use it in GitHub Desktop.
Save angelabauer/83ff2026724618c4815f986743f6d70e 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: Image.asset('images/ball1.png'),
);
}
}
@EdBasaldua
Copy link

I tried to put a Row inside the center, but the image do not appeared.

@isabia Remember that Row has children. If you put the image inside the children, the image will appear, but it will be cut vertically because the Row tries to adjust the content to the full height, but if you use Column, the image will be displayed correctly, although it is not necessary to use Column or Row for this project.

@ismamil
Copy link

ismamil commented Mar 10, 2020

I used a container and add alignment instead.

Container( alignment: Alignment.center, child: Image.asset('images/ball1.png'), );

@BasitAmin
Copy link

@OverRide
Widget build(BuildContext context) {
return Center(child: Container(
child:Image.asset('images/ball1.png'),
));
}
i use this it give same result

@christopheleray
Copy link

I personnally add a SafeArea then a Center widget a row and an Expanded widget :

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Center(
          child: Row(
        children: <Widget>[
          Expanded(
            child: Image.asset('images/ball1.png'),
          )
        ],
      )),
    );
  }
} ```
I guess it is fine too, maybe some useless code ??

@midotaha
Copy link

Hi Dears Friend i used My Own Code and i get this result also

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

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

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

class _BallState extends State {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue,
appBar: AppBar(
backgroundColor: Colors.blue.shade200,
title: Center(
child: Text(
'My App',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blueGrey.shade900,
fontFamily: 'Pacifico'),
),
),
),
body: Center(
child: Container(
child: Row(
children: [
Expanded(
child: Image.asset('images/ball1.png'),
),
],
),
),
),
);
}
}
`
Screenshot (101)

@Aladex
Copy link

Aladex commented Apr 25, 2020

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

@eqrxjz
Copy link

eqrxjz commented Apr 30, 2020

I made it with a container, but in the solution just used the Center widget, is there any difference if use a container in between?

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        child: Image.asset('images/ball1.png'),
      ),
    );
  }
}```

@nksas
Copy link

nksas commented May 6, 2020

I tried to put a Row inside the center, but the image do not appeared.

Check if you have called the stateful widget as body of scaffold in stless

@usedToBe
Copy link

usedToBe commented May 6, 2020

There are, of course, many ways to reach your goal.
However, I think it makes sense to use as few widgets as possible, and to clearly distinguish whether these are stateless or stateful.

@alvi11
Copy link

alvi11 commented May 30, 2020

For the first time all my code is totally same as Angela. I used this :
import 'package:flutter/material.dart';

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

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

class _BallState extends State {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[400],
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text("Ask Me Anything"),
centerTitle: true,
),
body: Center (
child: Image.asset("images/ball1.png"),
),
);
}
}

@memomiliano
Copy link

I used a container and a center widget, but container is useless.

@Sujon94
Copy link

Sujon94 commented Aug 27, 2020

I tried to put a Row inside the center, but the image do not appeared.

return Center(
      child: Row(
        children: <Widget>[
          Expanded(child: Image.asset('images/ball1.png')),
        ],
      ),
    );
It works for me.

@ketanc32
Copy link

ketanc32 commented Sep 1, 2020

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

@pratikthorat8
Copy link

i centered the statefull widget instead. it gave the same result or so i think lol
body: Center(
child: Ball(),
),

I used the exact same method.

@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: Image.asset('images/ball1.png'),
);
}
}

@savithacv
Copy link

Yes, Got it Working

image

@jenis2001
Copy link

Ball

how is this possible ???

Display a image without touch the yaml file it's this posible ?

@Rezwan009
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(
title: Center(
child: Text('Ask Me Anything'),
),
backgroundColor: Colors.blue.shade900,
),
body: Ball(),
);
}
}

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

class _BallState extends State {
@OverRide
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Image.asset('images/ball1.png'),
),
],
);
}
}

I did it this way.

@KhuloodBatis
Copy link

2021-12-28-09-26-48

@ahmed-bik
Copy link

hi everyone
I try that look

Capture d’écran 2022-01-17 à 20 22 43

@Scientist265
Copy link

Worked perfectly!
Screenshot (201)

@Scientist265
Copy link

@mounaimb
Copy link

`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(
backgroundColor: Colors.blue,
appBar: AppBar(
backgroundColor: Colors.blue.shade900,
title: Center(child: Text('Ask Me anything!')),
),
body: Center(child: 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 Image(image: AssetImage('images/ball1.png'));
}
}

`
is it cool to just return the image without creating a child?

@Synzu
Copy link

Synzu commented Jun 22, 2022

Screenshot 2022-06-22 at 01 59 59

@joaoantunesguimaraes
Copy link

What is the diference? AssetImage vs Image.asset

body: const Center(
child: Image(
image: AssetImage('images/ball1.png'),
)));

or

return Center(
  child: Image.asset('images/ball1.png'),
);

@Scientist265
Copy link

What is the diference? AssetImage vs Image.asset

body: const Center( child: Image( image: AssetImage('images/ball1.png'), )));

or

return Center(
  child: Image.asset('images/ball1.png'),
);

https://stackoverflow.com/questions/53309622/what-is-the-difference-between-assetimage-and-image-asset-flutter
check ur answer here

@Synzu
Copy link

Synzu commented Aug 31, 2022 via email

@c1c3ru
Copy link

c1c3ru commented Mar 3, 2023

simple like that

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

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

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

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

class _EightBallState extends State {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue.shade800,
appBar: AppBar(
backgroundColor: Colors.indigo.shade900,
title: Text('magic ball - ask me anything'),
),
body: Container(
child: Center(
child: Image.asset('images/ball1.png'),
),
),
);
}
}

`

@flutdev007
Copy link

Below is my solution and it is working as expected.

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

void main() {
return runApp(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.blueAccent,
appBar: AppBar(
backgroundColor: Colors.indigo.shade800,
centerTitle: true,
title: const Text(
'Ask Me Anything',
style: TextStyle(
fontSize: 35.0,
),
),
),
body: const BallPage(),
),
),
);
}

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

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

class _BallPageState 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'),
),
),
);
}
}

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

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