I advise you to create a custom Widget to do such an operation, I will give an example using an image of the internet and you make the necessary adaptations to what you need.
Create the following custom widget:
class BallNumber extends StatelessWidget {
BallNumber(this.text);
final String text;
@override
Widget build(BuildContext context) {
return Container(
height: 100,
width: 100,
margin: EdgeInsets.all(5),
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: NetworkImage("https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTIToUO9YMmuFoc2peIAK5mzk8xUz7K4IkY3A&usqp=CAU")
)
),
child: Center(
child: Text(text)
)
);
}
}
This way your code gets cleaner. As you can see, the Widget has a property called text which is where you will enter the number the ball should receive.
To use the custom Widget that was created, you can do it as follows:
class _MyHomePageState extends State<MyHomePage> {
final minhaLista = {
"primeiroJogo": [
"10",
"23",
"34",
"47",
"59",
],
"segundoJogo": [
"02",
"32",
"45",
"60",
"16",
],
};
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: minhaLista.entries.map((item) =>
ListTile(
title: Text(item.key),
subtitle: Container(
height: 100,
width: 100,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: item.value.length,
itemBuilder: (_, index) {
return BallNumber(item.value[index]);
}
),
)
)
).toList()
)
);
}
}
Explaining
The custom widget we created, the BallNumber
, will return us a Container
who owned a Text
to display the ball number and in the background set the image that will give the circle effect.
As you can see, I used the property shape: BoxShape.circle
that leaves the Container
with a circle effect, which you can take advantage of and just change your color, so you don’t need to use an image that takes further processing to be displayed.
Complete code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
home: MyHomePage(),
);
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final minhaLista = {
"primeiroJogo": [
"10",
"23",
"34",
"47",
"59",
],
"segundoJogo": [
"02",
"32",
"45",
"60",
"16",
],
};
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: minhaLista.entries.map((item) =>
ListTile(
title: Text(item.key),
subtitle: Container(
height: 100,
width: 100,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: item.value.length,
itemBuilder: (_, index) {
return BallNumber(item.value[index]);
}
),
)
)
).toList()
)
);
}
}
class BallNumber extends StatelessWidget {
BallNumber(this.text);
final String text;
@override
Widget build(BuildContext context) {
return Container(
height: 100,
width: 100,
margin: EdgeInsets.all(5),
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: NetworkImage("https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTIToUO9YMmuFoc2peIAK5mzk8xUz7K4IkY3A&usqp=CAU")
)
),
child: Center(
child: Text(text)
)
);
}
}
Note: You can run the code here on Dartpad to test, just copy and paste.
Good evening my friend. Thank you for taking the time to help me.. the code is working perfect, the problem is that I’m not able to adapt it in my code... I’m trying to put it where the part of Subtitle is no more could...I’m still new in dark/flutter...the numbers that the balls will receive comes from a list..
– Di Moura
@Dimoura Ali no Subtitle just remove that one
Text
with numbers and put theListView
of my example, only instead of "fixed child" make a foreach in your list with the numbers.– Matheus Ribeiro
@Dimoura modified my whole example so that I became more like its structure, reread my answer.
– Matheus Ribeiro
Good evening My friend.. Again thank you so much for the help.. it worked perfect.. Thank you very much.. GOD ALWAYS BLESS...
– Di Moura
@Dimoura Opa good that helped you! If possible mark the answer as accepted to close your question.
– Matheus Ribeiro