我正在尝试解决如何将此按钮居中的问题。
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
color: Colors.pinkAccent,
home:new Scaffold(
appBar: new AppBar(backgroundColor: Colors.white,),
body: new Column(
children: <Widget>[
new Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
),
new RaisedButton(
splashColor: Colors.pinkAccent,
color: Colors.black,
child: new Text("Scan",style: new TextStyle(fontSize: 20.0,color: Colors.white),),
onPressed: scan,
),
new Padding(padding: const EdgeInsets.symmetric(vertical: 10.0), ),
new Text('$_reader',softWrap: true, style: new TextStyle(fontSize: 30.0,color: Colors.black),),
],
),
),
);
}
这是从这里开始的:https://pub.dartlang.org/packages/barcode_scan#-example-tab-。
请帮我解决这个问题,谢谢。
编辑:我怎样才能将所有内容集中在“身体”中?
答案 0 :(得分:3)
您可以在列中传递轴对齐
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
主轴是列中的垂直轴,横轴是水平的。 它会将此对齐应用于所有孩子。
编辑:
如果您希望孩子在垂直轴上均匀分布,就像您的图片所描述的那样,您可以使用Expanded
。
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Barcode Scanner Example'),
),
body: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(child: Container(),),
new RaisedButton(
splashColor: Colors.pinkAccent,
color: Colors.black,
child: new Text(
"Scan",
style: new TextStyle(fontSize: 20.0, color: Colors.white),
),
onPressed: () {},
),
new Expanded(
child: Container(),
),
new Text(
"Reader",
softWrap: true,
style: new TextStyle(fontSize: 30.0, color: Colors.black),
),
new Expanded(
child: Container(),
),
],
),
));
答案 1 :(得分:0)
您可以使用mainAxisAlignment
和crossAxisAlignment
属性。或者您可以使用Column
小部件包装Center
窗口小部件,但这只会使交叉轴居中,因为Column
已扩展为其父级,因此您还必须使用mainAxisAlignment
}。当您想要对齐Column
/ Row
的孩子时,您还可以获得其他元素,例如:
//[MainAxisAlignment][1].start
//MainAxisAlignment.center
//MainAxisAlignment.end
//MainAxisAlignment.spaceAround
//MainAxisAlignment.spaceEvenly
//MainAxisAlignment.spaceBetween
//....
//[CrossAxisAlignment][1].baseline
//CrossAxisAlignment.start
//CrossAxisAlignment.center
//CrossAxisAlignment.end
//CrossAxisAlignment.stretch
另一件事是,我看到你使用Padding
小部件在RaisedButton
小部件和Text
小部件之间添加边距。 Padding
也只是一个像Container
这样的小部件。因此,相反Padding
,您可以简单地使用Container
或SizedBox
与height
来获取余额。
简单示例:
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new RaisedButton(
child: new Text("Click"),
onPressed: (){},
),
Container(height: 20.0),//SizedBox(height: 20.0),
new Text('Hello'),
],
),
由于您直接添加Scaffold
正文,Wrapp Column
使用Center
小部件并删除crossAxisAlignment
。这必须奏效。
如果您尝试我的代码示例(使用Container
),则无需使用Center
小部件进行换行,因为我没有为width
添加Container
答案 2 :(得分:0)
您可以将列包装在中心中并将 mainAxisAlignment:MainAxisAlignment.center,设置为列。检查以下代码:
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Barcode Scanner Example'),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new RaisedButton(
splashColor: Colors.pinkAccent,
color: Colors.black,
child: new Text(
"Scan",
style: new TextStyle(fontSize: 20.0, color: Colors.white),
),
onPressed: () {},
),
new Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
),
new Text(
"Reader",
softWrap: true,
style: new TextStyle(fontSize: 30.0, color: Colors.black),
),
],
),
),
));
}
答案 3 :(得分:0)
您可以将其主体包裹在新Container()中,以获取水平中心,取消垂直中心的注释mainAxisAlignment: MainAxisAlignment.center和crossAxisAlignment:CrossAxisAlignment.center, 在“中心”内的列中。
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
color: Colors.pinkAccent,
home:new Scaffold(
appBar: new AppBar(backgroundColor: Colors.white,),
body: new Center(
child: new Column(
//mainAxisAlignment: MainAxisAlignment.center,
//crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
),
new RaisedButton(
splashColor: Colors.pinkAccent,
color: Colors.black,
child: new Text("Scan",style: new TextStyle(fontSize: 20.0,color: Colors.white),),
onPressed: (){},
),
new Padding(padding: const EdgeInsets.symmetric(vertical: 10.0), ),
new Text('_reader',softWrap: true, style: new TextStyle(fontSize: 30.0,color: Colors.black),),
],
),
),
),
);
}
}
希望能有所帮助。
答案 4 :(得分:-1)
需要三件事
容器
mainAxisAlignment
crossAxisAlignment
class MyStatelessWidget extends StatelessWidget {
MyStatelessWidget({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
color: Colors.pinkAccent,
home: new Scaffold(
appBar: new AppBar(
backgroundColor: Colors.white,
),
body: Container(
width: double.infinity,
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
),
new RaisedButton(
splashColor: Colors.pinkAccent,
color: Colors.black,
child: new Text(
"Scan",
style: new TextStyle(fontSize: 20.0, color: Colors.white),
),
onPressed: () => {},
),
new Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
),
new Text(
'Some random text',
softWrap: true,
style: new TextStyle(fontSize: 30.0, color: Colors.black),
),
],
),
),
),
);
}
}