Reputation: 37
Scaffold(
appBar: AppBar(
leading: Icon(Icons.arrow_back),
centerTitle: true,
title: Text("Select Time Slot"),
),
body: ListView(
children: <Widget>[
SizedBox(
width: 150,
child: Card(
child: Text(
"11 AM - 1 PM",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
),
)
],
),
);
I expect that card occupies width 150, but it is taking complete width available.
What should I do to get the expected Result?
Upvotes: 1
Views: 4609
Reputation: 1615
instead of using ListView
try using Column
and wrap it with SingleChildScrollView
Scaffold(
appBar: AppBar(
leading: Icon(Icons.arrow_back),
centerTitle: true,
title: Text("Select Time Slot"),
),
body: Container(
height:300;
child:SingleChildScrollView(
child:Column(
crossAxisAlignment: CrossAxisAlignment.center
children: <Widget>[
SizedBox(
width: 150,
child: Card(
child: Text(
"11 AM - 1 PM",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
),
)
],
),
),
),
);
Upvotes: 0
Reputation: 1540
if you want to center the card with 150 width wrap it with center widget. center widget will set card width to 150 and will fill the remaining width by centering card to middle.
Center(child:
SizedBox(
width: 150,
child: Card(
child: Text(
"11 AM - 1 PM",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
),
))
],
otherwise you can use Align widget to align the card as you preference.
Upvotes: 5
Reputation: 7222
check this one:
home: Scaffold(
appBar: AppBar(
leading: const Icon(Icons.arrow_back),
centerTitle: true,
title: const Text("Select Time Slot"),
),
body: ListView(
children: <Widget>[
Row(
children: const <Widget>[
SizedBox(
width: 150,
child: Card(
child: Text(
"11 AM - 1 PM",
style:
TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
),
)
],
)
],
),
)
Upvotes: 1