Create two widgets independent of each other - flutter

I am not sure if I am missing something here, but in Flutter I want to crate two widgets that can be moved around independently of each other.
My current code is this:
class SurpriseReveal extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
margin: EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Hi",
style: TextStyle(fontSize: 20, color: Colors.pink[700]),
textAlign: TextAlign.center,
),
InkWell(
onTap: null,
splashColor: Colors.pink[900],
child: Container(
// set your desired height here
height: 150,
// set your desired width here
width: 150,
// decoration property gives it a color and makes it round like a floating action button
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.pink[300],
),
// add your desired padding here
padding: const EdgeInsets.symmetric(vertical: 10.0),
// add the child element
child: Center(
child: Text(
'Hi',
// style of the text
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 22.0,
color: Colors.pink[900],
),
),
),
),
),
],
),
);
}
}
This creates something that looks like this:
What I would like to be able to do is choose the positioning of these widgets on the screen independently of each other. For example, If I wanted to have a gap between the text and the button, or the button off to one side.
Would I need to create two separate functions that return Widget?

To add space between your widgets you can wrap them with a Padding() widget. However, if you want to dispose your widgets independantly wherever on the screen wrap them in a stack widget and use Positionned() widget to set their constrainst.

Related

Getting a Container inside an Expanded inside a Row to expand vertically to fit the space available in the Row

I have a row containing four Expanded widgets. Its code looks as follows:
Row(
children: [
Expanded(
child: Container(
width: double.infinity,
color: Colors.blueGrey,
padding: const EdgeInsets.all(10),
child: (Text(
lessonData.language,
style: const TextStyle(
color: Colors.white,
),
)),
),
),
Expanded(
flex: 1,
child: Container(
width: double.infinity,
color: Colors.blueGrey,
padding: const EdgeInsets.all(10),
child: (Text(
lessonData.cEFRLevelName,
style: const TextStyle(
color: Colors.white,
),
)),
),
),
Expanded(
flex: 1,
child: Container(
width: double.infinity,
color: Colors.blueGrey,
padding: const EdgeInsets.all(10),
child: (Text(
lessonData.lessonTopic,
style: const TextStyle(
color: Colors.white,
),
)),
),
),
Expanded(
child: Container(
width: double.infinity,
color: Colors.blueGrey,
padding: const EdgeInsets.all(10),
child: (
Text(
lessonData.lessonHeading,
style: const TextStyle(
color: Colors.white,
),
)),
),
)
],
),
The resulting display is unsatisfactory if any of the texts in the Containers inside the Expanded widgets are forced to wrap. As in this image:
Click here to display an image of my problem when text has wrapped in one Container but not in the other three.
This image shows something like what I'd like the Row to look like.
I simply can't get the Container widgets containing text without any wrap to expand out to the height of the row.
Among other candidate solutions, I've tried setting the height to double.infinity and double.maxFinite, setting Constraints in the Containers to Constraints: BoxConstraints.expand(). All these options either do nothing or generate an error on hot reload.
I'm reluctant to try an IntrinsicHeight widget because I'm warned that it's very hungry.
I'd be very grateful for any solution and/or comment you might have!
TIA
Jaime
If I understood what you want, I may have a suggestion. It's more of a workaround, but you can move the Container with the colored backgroud up a level and remove each other Container, making the new Container the parent of the whole Row: I put the code in a DartPad, and this is how it looks.
Just an excerpt of the code:
Container(color: Colors.red,child: Row(
children: [
Expanded(...),
Expanded(...),
//and so on...
This works only on an aesthetic level: each Text will still get resized, but you won't notice.

Row alignment in flutter

I want to align my some widgets in a row in a particular way. I want the Icon to be at the beginning of the row and the text to be at the center, I tried wrapping the text widget in a center widget but that didn't work. Please how can I carry out something like that with a row that only has two children?
Wrap the Center in an Expanded. So something like this:
Widget build(BuildContext context) {
return Row(children: const [
Icon(Icons.abc),
Expanded(child: Center(child: Text('abc')))
]);
}
You could use a Stack for the same
Sample code
Container(
padding: const EdgeInsets.symmetric(vertical: 18),
color: Colors.black,
child: Stack(
children: const [
Center(
child: Text(
"Login",
style: TextStyle(fontSize: 18, color: Colors.white,fontWeight: FontWeight.w600),
),
),
Positioned(
left: 12,
child: Icon(
Icons.close,
color: Colors.white,
),
),
],
),
);

Flutter set container colors depends on state

i have a problem in my flutter project.
i have a task list screen and i want to change widget colors when task is completed, like disabled buttons(pale colors) but it can be tapped
this is my screen:
codes:
this is my widget
return Container(
margin: EdgeInsets.only(bottom: 10),
padding: EdgeInsets.only(left: 15, top: 7, bottom: 7),
height: GeneralConstants.instance.popupBoxHeight,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7),
color: ColorConstants.instance.mainColor),
child: InkWell(
onTap: () {
print("${baslik} Clicked");
},
child: Column(
children: [
SizedBox(
height: 25,
width: double.infinity,
child: AutoSizeText(
baslik,
style: GoogleFonts.oswald(
color: ColorConstants.instance.headerColor, fontSize: 18),
),
),
Expanded(
child: IgnorePointer(
ignoring: true,
child: ListView(
children: [
AutoSizeText(
govde,
style: GoogleFonts.openSans(
color: ColorConstants.instance.bodyColor, fontSize: 10),
),
],
),
),
)
],
),
),
);
}
I have two solutions to solve it:
You can send the row color from outside and if the task is done send a pale color. For more information please read this article.
If you return null on InkWell onTap it becomes disabled and shows a pale color. But I'm sure InkWell shows a shadow when you click it, I suggest you use GestureDetector widget for a clickable widget.

Horizontal scroll Text in flutter

I am trying to display a long text on one line without clipping or ellipsing on a flutter web application. To do so, I want to make it horizontally scrollable. I have found this code from https://www.geeksforgeeks.org/flutter-scrollable-text/
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Scaffold(
//adding App Bar
appBar: AppBar(
backgroundColor: Color.fromRGBO(15, 157, 88, 1),
title: Text(
"GeeksForGeeks",
style: TextStyle(
color: Colors.white,
),
),
),
body: MyApp(),
),
));
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Center(
child: Container(
// adding margin
margin: const EdgeInsets.all(15.0),
// adding padding
padding: const EdgeInsets.all(3.0),
decoration: BoxDecoration(
// adding borders around the widget
border: Border.all(
color: Colors.blueAccent,
width: 5.0,
),
),
// SingleChildScrollView should be
// wrapped in an Expanded Widget
child: Expanded(
//contains a single child which is scrollable
child: SingleChildScrollView(
//for horizontal scrolling
scrollDirection: Axis.horizontal,
child: Text(
"GeeksForGeeks is a good platform to learn programming."
" It is an educational website.",
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.bold,
fontSize: 20.0,
letterSpacing: 3,
wordSpacing: 3,
),
),
),
),
),
);
}
}
But it didn't work for me.The text was clipped but not scrollable. I tried to add overflow: TextOverflow.visible to the Text widget but without result.I don't have any other idea because wrapping a Text in a SingleChildScrollView is the easiest thing I can think about for this problem.
The issue is happening because of Expanded widget. Remove this and it will work just fine,
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Center(
child: Container(
// adding margin
margin: const EdgeInsets.all(15.0),
// adding padding
padding: const EdgeInsets.all(3.0),
decoration: BoxDecoration(
// adding borders around the widget
border: Border.all(
color: Colors.blueAccent,
width: 5.0,
),
),
child: SingleChildScrollView(
//for horizontal scrolling
scrollDirection: Axis.horizontal,
child: Text(
"GeeksForGeeks is a good platform to learn programming."
" It is an educational website.",
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.bold,
fontSize: 20.0,
letterSpacing: 3,
wordSpacing: 3,
),
),
),
),
);
}
}
First of all, for flutter to detect items under a scroll, you have to use ListView as of now. Add a listview widget and under that keep your text or other widgets and essentially flutter will detect your scroll widgets perfectly.
in ListView widget theres a property called scrollDirection: and you can set the value to Axis.horizontal, that does the trick.
For further references please check this out : How to create a horizontal list in flutter
tip : avoid geeksforgeeks on the go for learning flutter, learned it the hard way

Dont know how to lock the center of a widget and separate it from the others

I'm creating an app with the main menu and different minigames, I'm starting with Flutter, so I don't understand properly how to manage the widgets properly, in this case, I have simplified the code because what I want is to understand the hierarchy, and how to place the widgets properly
child: Scaffold(
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Flexible(
flex: 1,
child: Container(
child: Text(_element, style: new TextStyle( fontSize: 28.0, color: Colors.black), textAlign: TextAlign.center,
),
),
),
Flexible(
flex: 1,
child: Container(
child: ElevatedButton(
child: Text('Next', style: new TextStyle( fontSize: 100.0, color: Colors.black)),
onPressed: () {
getNextElement();
},
),
),
),
],
),
),
)
With this code I get this:
What I have
I want to understand how to use widgets properly and in which order.
And my problem is that I want the button at the bottom and the text in the center, and when I press the button the text changes so when it is a longer phrase the size of the text container changes and it expands to the top of it but without changing the width, but I want it to be centered and when the text is bigger I want it to expand up and down like this:
Objective: