Flutter add Text styling to a custom class - flutter

I want to add text styling to the Score class in this widget. I have another widget that uses this widget to generate a circle with a number in it.
Center(
child: Container(
width: 100.0,
height: 100.0,
decoration: BoxDecoration(
color: Colors.grey[600],
shape: BoxShape.circle,
),
child: Score(score: 'score',

// Are you looking for something like this
class CenterClass extends StatelessWidget {
const CenterClass({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
width: 100.0,
height: 100.0,
decoration: BoxDecoration(
color: Colors.grey[600],
shape: BoxShape.circle,
));
}
}
YOu can achieve same via Widgets
Widget circleContainer() {
return Container(
width: 100.0,
height: 50.0,
decoration: BoxDecoration(
color: Colors.grey[600],
shape: BoxShape.circle,
));
}

I think you wanted to like A BALL that show the score of ball like below image
If I Am Right then code is below
Container(
width: 27,
height: 27,
child: Padding(
padding: const EdgeInsets.all(1),
child: Container(
child: Center(
child: Text(
'4',
style: TextStyle(
color: Colors.white,
fontWeight:
FontWeight.bold,
fontSize: 11,
fontFamily: 'Tahoma',
),
),
),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xffffc11c), // inner circle color
),
),
),
),

Related

There is a space between my ListView and the Tiles

I am making and app using flutter and I was using a ListView togenerate the items but there is a gap between the List and the first Tile.
Already tried
Changing the size of the container that wraps around the listview
Removing the "header" with the title "ExercĂ­cios"
Removing the ClipRRect
class ExerciseList extends StatelessWidget {
final List<String> exercises;
const ExerciseList({Key? key, required this.exercises}) : super(key: key);
#override
Widget build(BuildContext context) {
return Padding(
padding:
const EdgeInsets.only(top: 30, bottom: 20, right: 30, left: 30),
child: ClipRRect(
borderRadius: BorderRadius.circular(30),
child: Column(
children: [
Container(
width: MediaQuery.of(context).size.width,
decoration:
const BoxDecoration(
color: Color.fromARGB(255, 60, 60, 60)
),
child: const Padding(
padding: EdgeInsets.only(top: 8, bottom: 8),
child: Text(
"ExercĂ­cios",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 32,
color: Colors.white
),
),
),
),
Container(
height: 300,
decoration:
const BoxDecoration(
color: Color.fromARGB(255, 65, 65, 65)
),
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return Container(
decoration: const BoxDecoration(
border: Border(
top: BorderSide(
color: Colors.black,
width: 0.6
)
)
),
child: ListTile(
title: Text(
exercises[index],
style: const TextStyle(
fontSize: 22,
color: Colors.white,
),
),
),
);
},
itemCount: exercises.length,
),
),
],
),
)
);
}
}```
Based on the code you provided, not able to re-create the issue on my end.
Check your data List for any empty entries.

Align bottom center a text inside a Container of stack

It's the first time I use Flutter and I'm having some problem aligning containers.
Here is my code:
#override
Widget build(BuildContext context) {
return const MyItem(
imgSrc: 'https://images.unsplash.com/photo-1610655012457-9cbd66fe510b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2940&q=80',
text: 'Lorem ipsum');
}
class MyItem extends StatelessWidget {
const MyItem(
{Key? key,
required this.imgSrc,
required this.text})
: super(key: key);
final String imgSrc;
final String text;
#override
Widget build(BuildContext context) {
return Container(
decoration:
BoxDecoration(border: Border.all(color: Colors.purple, width: 3)),
margin: const EdgeInsets.symmetric(horizontal: 3),
child: Stack(children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent, width: 3)),
child: Image.network(imgSrc, fit: BoxFit.cover),
),
Container(
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
border: Border.all(color: Colors.orange, width: 3)),
child: Text(
text.toUpperCase(),
style: const TextStyle(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.bold),
),
),
]));
}
}
The result is:
But I want the text lorem ipsum at the bottom side, not near the top side.
Seems like alignment: Alignment.bottomCenter doesn't work.
What am I missing?
You just have to wrap the container widget with the Positioned widget, just tell the bottom: 0,
import 'package:flutter/material.dart';
class MyItem extends StatelessWidget {
const MyItem ({Key? key}) : super(key: key);
final imgSrc =
'https://images.unsplash.com/photo-1610655012457-9cbd66fe510b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2940&q=80';
final String text = 'Lorem ipsum';
#override
Widget build(BuildContext context) {
return Container(
decoration:
BoxDecoration(border: Border.all(color: Colors.purple, width: 3)),
margin: const EdgeInsets.symmetric(horizontal: 3),
child: Stack(
children: [
Container(
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent, width: 3)),
child: Image.network(imgSrc, fit: BoxFit.cover),
),
Positioned(
bottom: 0,
width: MediaQuery.of(context).size.width,
child: Container(
height: 50,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.orange, width: 3),
),
child: Text(
'$text'.toUpperCase(),
//text.toUpperCase(),
style: const TextStyle(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.bold),
),
),
),
],
),
);
}
}
You also can use below codes
class MyItem extends StatelessWidget {
const MyItem({Key? key, required this.imgSrc, required this.text})
: super(key: key);
final String imgSrc;
final String text;
#override
Widget build(BuildContext context) {
return Container(
decoration:
BoxDecoration(border: Border.all(color: Colors.purple, width: 3)),
margin: const EdgeInsets.symmetric(horizontal: 3),
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
imgSrc,
),
fit: BoxFit.cover),
border: Border.all(color: Colors.blueAccent, width: 3)),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.orange, width: 3)),
child: Text(
text.toUpperCase(),
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.bold),
),
),
));
}
}
You must fix the height and width of Stack as it is like a graph, where the centre is (0,0). There x & y has a value from -1 to 1 in the container. You go up from the centre value goes nearer to -1 and down +1 same as If you go left -1 right +1.
After that, you can use AlignmentDirectional to tell where exactly you want to put chid of the stack. in your case as below.
Align(
alignment: const AlignmentDirectional(0, 1),
child: SizedBox()....
==Now coming to your code must be like this... review comments in the same too.
#override
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width * 1,
//Master container size of mobile screen and limits of stack.
height: MediaQuery.of(context).size.height * 1,
decoration: BoxDecoration(
border: Border.all(color: Colors.purple, width: 3),
),
margin: const EdgeInsets.symmetric(horizontal: 3),
child: Stack(
children: [
Align(
alignment: const AlignmentDirectional(0, 0),
child: Container(
width:
MediaQuery.of(context).size.width * 1, //You can fix your own
height: MediaQuery.of(context).size.height * 1,
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent, width: 3),
),
child: Image.network(imgSrc, fit: BoxFit.cover),
),
),
Align(
alignment: const AlignmentDirectional(0, 1),
child: Container(
width: MediaQuery.of(context).size.width * 1,
//You can fix your own
height: MediaQuery.of(context).size.height * 0.1,
/*alignment: Alignment.bottomCenter,*/
//This will only work inside of container.
decoration: BoxDecoration(
border: Border.all(color: Colors.orange, width: 3),
),
child: Text(
text.toUpperCase(),
style: const TextStyle(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.bold,
),
),
),
),
],
),
);
}
You should use the Align widget. with assign value to the alignment attribute the container will expand to fill its parent and position its child within itself according to the given value.
here is the sample :
class MyItem extends StatelessWidget {
const MyItem(
{Key? key,
required this.imgSrc,
required this.text})
: super(key: key);
final String imgSrc;
final String text;
#override
Widget build(BuildContext context) {
return Container(
decoration:
BoxDecoration(border: Border.all(color: Colors.purple, width: 3)),
margin: const EdgeInsets.symmetric(horizontal: 3),
child: Stack(children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent, width: 3)),
child: Image.network(imgSrc, fit: BoxFit.cover),
),
Align(
alignment: Alignment.bottomCenter,
child:Container(
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
border: Border.all(color: Colors.orange, width: 3)),
child: Text(
text.toUpperCase(),
style: const TextStyle(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.bold),
),
),),
]));
}
}

Create circle around a letter in flutter

I was trying to create a circle around a letter here is my code, but it was not creating a perfect circle
Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border.all(color: Colors.white, width: 2)),
child: InkWell(
child: Text(
"A",
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 15),
),
),
),
I've also tried with height and width of the Container then some portion of letter is hidden.
Container(
height: 30,
width: 30,
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border.all(color: Colors.white, width: 2)),
child: InkWell(
child: Text(
"A",
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 15),
),
),
)
But if I use Icon Widget instead of text widget it was creating perfect circle,
Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border.all(color: Colors.white, width: 2)),
child: InkWell(
child: Icon(
Icons.add,
color: Colors.white,
),
),
),
Use this:
CircleAvatar(
backgroundColor: Colors.blue,
child: Text("A"),
);
You can also make use of the many other flags in CircleAvatar, like maxRadius or foregroundImage, like this:
CircleAvatar(
backgroundColor: Colors.blue,
child: Text("A"),
maxRadius: 30,
foregroundImage: NetworkImage("enterImageUrl"),
);
Looks like this:
For Containers, you can use the shape property to give it a circular shape.
It would be something like this :
Container(
decoration: BoxDecoration (
shape: BoxShape.circle,
),
child: ....
)
If I face multiple widget I prefer by creating a widget and wrapping others.
Container BoxShape.circle, depends on height
class WrapWithCircle extends StatelessWidget {
final Widget child;
late final EdgeInsets edgeInsets;
late final Color color;
WrapWithCircle({
Key? key,
required this.child,
this.edgeInsets = const EdgeInsets.all(8),
this.color = Colors.white,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
padding: edgeInsets,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
width: 2,
color: color,
),
),
child: child,
);
}
}

Custom card or container with border, shape and three parts

How can i create following flutter widget.
I want to use it in a column to navigate to the next page.
I've already tried a card or a container consisting of a row with three expandables. But either the red area with the icon overlaps the container with the rounded edges, or it is smaller than the container. What can I do or maybe someone has a code example?
You could create your own custom widget.
Here's a code sample you could use:
class MyWidget extends StatelessWidget {
final double height;
final Function onTap;
MyWidget({this.height = 40.0, this.onTap});
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => onTap(),
child: Container(
decoration: BoxDecoration(
color: Colors.red,
border: Border.all(color: Colors.red),
borderRadius: BorderRadius.circular(10),
),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(8),
child: Icon(Icons.inbox, color: Colors.white),
),
Expanded(
child: Container(
padding: const EdgeInsets.only(left: 8),
alignment: Alignment.centerLeft,
height: height,
decoration: BoxDecoration(color: Colors.white),
child: Text(
'ARCHIV',
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold,
),
),
),
),
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius:
BorderRadius.horizontal(right: Radius.circular(10)),
),
height: height,
child: Icon(Icons.arrow_forward_ios, color: Colors.red),
),
],
),
),
);
}
}
Try full code on DartPad
Result:

Flutter: Container color overflowing border

I need to make a container with rounded borders, color, and an outline, but the background color is overflowing the outline color.
How can I fix this?
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 21,
constraints: BoxConstraints(
minWidth: 21,
),
decoration: BoxDecoration(
border: Border.all(color: Colors.white, width: 2),
borderRadius: BorderRadius.circular(21),
color: Colors.pink,
),
child: Align(
child: Text(
"1",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold, fontSize: 12),
)));
}
}
Result: (most visible on the left side)
It... looks like a bug? I think you can report the issue to flutter github.
If you just want a workaround solution, you can try to move the background color (pink) to the lower level of your widget.
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 21,
constraints: BoxConstraints(
minWidth: 21,
),
decoration: BoxDecoration(
border: Border.all(color: Colors.white, width: 2),
borderRadius: BorderRadius.circular(21),
),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(21),
color: Colors.pink,
),
child: Align(
child: Text(
"1",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold, fontSize: 12,
),
),
),
),
);
}
}