Unable to center align Column Widgets children - flutter

I want to align the children widgets of my Column to the center of the cross axis.
I tried to do this using the crossAxisAlignment property of Column however its not working. How can i fix this?
#override Widget build(BuildContext context) {
Container buttonView = Container(
padding: EdgeInsets.only(top: 30.0, left: 35.0, right: 35.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
DropdownButton<String>(
...
),
SizedBox(height: 20.0),
DropdownButton<String>(
...
],
));
return Scaffold(
body: buttonView,
);
}

you should try mainAxisAlignment: MainAxisAlignment.center, below is snap,
#override Widget build(BuildContext context) {
Container buttonView = Container(
padding: EdgeInsets.only(top: 30.0, left: 35.0, right: 35.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center, // <---- try this property
children: <Widget>[
DropdownButton<String>(
...
),
SizedBox(height: 20.0),
DropdownButton<String>(
...
],
));
return Scaffold(
body: buttonView,
);
}

Related

How to add title to Container in Flutter?

I want to add a title or image on the home page of the project, how can I do that? I need help. I want to add an image describing the application above the registration or login form.
#override
Widget build(BuildContext context) {
SizeConfig().init(context);
return Scaffold(
appBar: AppBar(
title: _title(),
),body: Container(
height: double.infinity,
width: double.infinity,
padding: const EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_entryField('email', _controllerEmail),
Padding(padding: EdgeInsets.only(bottom: 10)),
_entryField('password', _controllerPassword),
Padding(padding: EdgeInsets.only(bottom: 10)),
_submitButton(),
_loginOrRegisterButton(),
],
),
),
);
}
}
Add items inside Column widget,
#override
Widget build(BuildContext context) {
SizeConfig().init(context);
return Scaffold(
appBar: AppBar(
title: _title(),
),body: Container(
height: double.infinity,
width: double.infinity,
padding: const EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset("YourImagePath",height: 100,width:200 ,fit: BoxFit.cover,), //here
_entryField('email', _controllerEmail),
Padding(padding: EdgeInsets.only(bottom: 10)),
_entryField('password', _controllerPassword),
Padding(padding: EdgeInsets.only(bottom: 10)),
_submitButton(),
_loginOrRegisterButton(),
],
),
),
);
}
}
Find more about assets-and-images
You should be able to put an Image widget in your Column, right before the email entryfield. Either an AssetImage or a NetworkImage.

Flutter TikTokStyleFullPageScroller() not working on empty (non content) screen

I am new to Flutter and trying to add slide functions to videos. It seems to be working as expected but only if slide by tapping on content area. In other words if tapped on video to slide it doesn't work.
Package:
tiktoklikescroller: ^0.1.1
import 'package:tiktoklikescroller/tiktoklikescroller.dart';
Here is my code:
#override
Widget build(BuildContext context) {
final List<Color> colors = <Color>[Colors.red, Colors.blue, Colors.yellow];
return Expanded(
child: TikTokStyleFullPageScroller(
contentSize: colors.length,
swipeThreshold: 0.2,
swipeVelocityThreshold: 2000,
animationDuration: const Duration(milliseconds: 300),
builder: (BuildContext context, int index) {
return Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
VideoDescription(),
ActionToolbar(),
],
);
},
),
);
}
Video Description
class VideoDescription extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Expanded(
child: Align(
alignment: Alignment.bottomLeft,
child: Container(
height: 70.0,
padding: EdgeInsets.only(left: 20.0),
margin: EdgeInsets.only(bottom: 160.0),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Title', style: TextStyle(fontWeight: FontWeight.bold),),
],
),
),
),
);
}
}
Action Toolbar
#override
Widget build(BuildContext context) {
return Container(
width: 100.0,
margin: EdgeInsets.only(bottom: 25.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_getMusicPlayerAction(),
],
),
);
}
Please help.
I wrapped VideoDescription() and ActionToolbar() inside a container and gave it a transparent color.
#override
Widget build(BuildContext context) {
final List<Color> colors = <Color>[Colors.red,
Colors.blue, Colors.yellow];
return Expanded(
child: TikTokStyleFullPageScroller(
contentSize: colors.length,
swipeThreshold: 0.2,
swipeVelocityThreshold: 2000,
animationDuration: const Duration(milliseconds: 300),
builder: (BuildContext context, int index) {
return Container(
color: Colors.transparent,
Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
VideoDescription(),
ActionToolbar(),
],
),
);
},
),
);
}

How to position Widgets on exactly positions in a Column in Flutter?

I want to position the Button a bit lower and the Widget with the Text and the Image a bit higher, and don't know how to do. MainAxisAlignment.spaceBetween is not a solution, because this would be to high for the Widget and too low for the Button.
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
if (isShown == true) SkillJonglieren(),
Flexible(
flex: 4,
child: Container(
padding: EdgeInsets.only(bottom: 60),
width: 230,
height: 130,
child: RaisedButton(
color: Colors.red,
child: Text('Show Skill',
style: TextStyle(fontSize: 30)),
onPressed: () {
setState(() {
isShown = true;
Next thing: the Text and the Image are one stateless Widget. I want a bit of free space between them, but when I implement the MainAxisAlignment.spaceAround for the Column (in which they are), It somehow doesn't work.
class SkillJonglieren extends StatelessWidget {
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text(
'Jonglieren',
style: TextStyle(fontSize: 35),
),
Image.asset('images/jonglieren.jpg')
]);
}
}

Having trouble centering a row widget

I have a Row widget on the bottom of the page that I can't center. I've tried all the mainAxisAlignment's and crossAxisAlignment's there are, as well as wrapping it in an Align widget and setting alignment to alignment.center and it's still on the left edge of the page. I've tried everything and am really stuck. Any help would be appreciated.
The Page (Updated)
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.lightBlue,
centerTitle: true,
elevation: 5,
title: Text("Pathomatic")),
body: Container(
decoration: new BoxDecoration(color: Colors.black),
child: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 2,
child: new Stack(
children: <Widget>[
new Container(
alignment: Alignment.center,
child: _cameraPreviewWidget(context),
),
new Align(
alignment: Alignment.center,
child: GestureDetector(
child: Stack(children: <Widget>[
Positioned(
top: yPosition,
left: xPosition,
child: Container(
// padding: EdgeInsets.only(top: 200.0, left: 20.0),
height: MediaQuery.of(context).size.height / 3,
width: MediaQuery.of(context).size.width / 3,
child:
Image.asset('assets/images/crosshair.png'),
),
),
]),
onPanUpdate: (tapInfo) {
setState(() {
xPosition += tapInfo.delta.dx;
yPosition += tapInfo.delta.dy;
});
}),
),
],
),
),
SizedBox(height: 5),
Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
_cameraTogglesRowWidget(),
_mlTextWidget(),
],
),
_captureControlRowWidget(context),
//SizedBox(width: 50),
],
),
SizedBox(height: 5),
],
),
),
),
);
}
This is usually because the widget you are trying to center is actually stretching its container. Since it's the size of the screen, it's contents stick to the left. You should use the Flutter Inspector with select widget mode to actually see how large your row is. Then everything will make much more sense.
In your code, your Row is stretching. Try
Row(mainAxisSize: MainAxisSize.min
This should make it not grow. Alternatively, you can keep the row large but make it center its children:
Row(mainAxisAlignment: MainAxisAlignment.Center
Of course, with this approach its children will get close to each other in the center. You can use a SizedBox between them, or use some other way of spacing them apart.

How to make a container scroll-able?

The current state of my code:
#override
Widget build(BuildContext context) {
return Container(
decoration: Grad2(),
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// ...
],
),
);
}
Which resulted in this beautiful Login page, but there is a problem (Bottom overflowed by 45 pixels when keyboard is up):
So i tried to use the SingleChildScrollView but then it resulted in a blank screen:
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: BoxConstraints().maxHeight),
child: Container(
decoration: Grad2(),
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// ...
],
),
),
),
);
}
But what I've got instead is a white screen. May I know why?
Try this:
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height,
decoration: Grad2(),
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// ...
],
),
),
);
}
Turns out, I could make the Container the height of the display, which works by setting the height of the Container to MediaQuery.of(context).size.height. In conclusion, use
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height,
decoration: Grad2(),
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// ...
],
),
),
);
}