I am creating a quiz in flutter, and I want to create a Wrap widget, because there are some long questions (Text), which doesn't fit in one row. Where sould i put my Wrap widget? I tried to replace every simple Row widget with the Wrap widget and none of them seems to work. Also there is thelink with the instructions for how to use the Wrap widget that i followed: https://medium.com/flutter-community/flutter-wrap-widget-e1ee0b005b16
Thanks.This is my code:
`
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class questionpage extends StatefulWidget {
#override
_questionpageState createState() => _questionpageState();
}
class _questionpageState extends State<questionpage> {
int qnum = 0;
int score = 0;
int seconds = 10;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.indigoAccent[700],
title: SafeArea(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
child: Text(
'$qnum/10 ',
style: TextStyle(
fontSize: 28,
),
),
),
Container(
child: Text(
'$seconds',
style: TextStyle(
fontSize: 28,
),
),
),
Container(
child: Text(
'Score: $score',
style: TextStyle(
fontSize: 28,
),
),
),
],
),
),
),
body: Column(children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(0, 50, 0, 0),
child: Text(
'Question $qnum:',
style: new TextStyle(
color: Colors.black,
fontSize: 32.0,
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
decorationThickness: 3.0,
),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(0, 5, 0, 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'THIS IS THE LONGGGGGGGGGGG QUESTION',
style: new TextStyle(
color: Colors.black,
fontSize: 28.0,
fontWeight: FontWeight.bold
),
),
],
),
),
],
),
],),
);
}
}
[1]: https://i.stack.imgur.com/U4Aut.png
You have to make the inner Row Wrap and then the outer Row Column or Wrap like this.
Column(// this could be wrap too
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(0, 5, 0, 0),
child: Wrap(
alignment: WrapAlignment.center,
children: <Widget>[
Text(
'THIS IS THE LONGGGGGGGGGGG QUESTION',
softWrap: true,
style: new TextStyle(
color: Colors.black,
fontSize: 28.0,
fontWeight: FontWeight.bold),
),
],
),
),
],
),
Let me know if you need more than this
Related
I have this issue with my Flutter UI. I'm trying to make my text break when it's too long, but the overflow ellipsis doesn't work. Can you help me please?
issue picture:
this is what i'm looking for:
My code:
static Drawer myDrawer (BuildContext context, String mode, String businessName) {
return Drawer(
child: Container(
child: ListView(
padding: EdgeInsets.only(top:0),
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: double.infinity,
height: MediaQuery.of(context).size.height * 0.17,
child: CustomPaint(
painter: DrawerHeaderPaint(),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(width: 25),
CircleAvatar(
backgroundColor: Colors.white,
radius: 30,
child: Image(
height: 40,
image: AssetImage('assets/logo.png')
),
),
SizedBox(width: 10),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'My Companny',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w900,
fontSize: 25
),
),
Container(
color: Colors.red,
child: Row(
children: [
Icon(Icons.apartment, color: Color(0xff263d67)),
Text(
'Business Name tooooo loooooong',
overflow: TextOverflow.ellipsis,
maxLines: 1,
softWrap: false,
style: TextStyle(
fontFamily: 'Poppins',
color: Color(0xffd7d7d7),
fontWeight: FontWeight.w700,
fontSize: 16,
),
),
],
),
)
],
),
],
),
),
),
SizedBox(height: 40),
menuOptions(),
],
)
],
),
)
);
}
Try wrapping Business Info Column with Expanded widget and also wrap Text widget after apartment icon with Expanded widget, like so:
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'My Companny',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w900,
fontSize: 25,
), //TextStyle
), //Text
Container(
color: Colors.red,
child: Row(
children: const [
Icon(Icons.apartment, color: Color(0xff263d67)),
Expanded(
child: Text(
'Business Name tooooo loooooong',
overflow: TextOverflow.ellipsis,
maxLines: 1,
softWrap: false,
style: TextStyle(
fontFamily: 'Poppins',
color: Color(0xffd7d7d7),
fontWeight: FontWeight.w700,
fontSize: 16,
), //TextStyle
), //Text
), //Expanded
],
), //Row
), //Container
],
), //Column
), //Expand
Use a Expanded or Flexible
Expanded(
child:
Text('Business Name tooooo loooooong',...))
//you can use the SizedBox widget and specify your text width it helps to ellipsis the text. Hope this code will help you.Thanks
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class Dermo extends StatefulWidget {
const Dermo({Key? key}) : super(key: key);
#override
_DermoState createState() => _DermoState();
}
class _DermoState extends State<Dermo> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
)
,
drawer: Drawer(
backgroundColor: Colors.green,
child: Container(
child: ListView(
padding: EdgeInsets.only(top: 0),
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: double.infinity,
height: MediaQuery.of(context).size.height * 0.17,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(width: 25),
CircleAvatar(
backgroundColor: Colors.white,
radius: 30,
child: Image(
height: 40, image: AssetImage('assets/logo.png')),
),
SizedBox(width: 10),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'My Companny',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w900,
fontSize: 25),
),
Container(
color: Colors.red,
child: Row(
children: [
Icon(Icons.apartment, color: Color(0xff263d67)),
///if you want to show complete text use can comment
///out overflow,softWrap and maxLines otherwise you can use all
///these properties it ellipsis the text
SizedBox(
width: 160,
child: Text(
'Business Name tooooo loooooong',
/* overflow: TextOverflow.ellipsis,
maxLines: 1,
softWrap: false,*/
style: TextStyle(
fontFamily: 'Poppins',
color: Color(0xffd7d7d7),
fontWeight: FontWeight.w700,
fontSize: 16,
),
),
),
],
),
)
],
),
],
),
),
SizedBox(height: 40),
],
)
],
),
)));
}
}
I am trying to position text to be closer to the left edge, than to the centre.
Upon research using flutter, it appears that the correct way to do so is using const EdgeInserts
However, when using const edge inserts, the text on my page only changes position by a minuscule amount, which is barely noticeable. Am I doing something wrong with the code below?
Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10, bottom: 50),
child: Text(
'Some Text',
style: TextStyle(
fontSize: 24,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
Here is a sample of what is happening, vs what I want
Edit for answer:
class Onboarding1 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Column(
children: [
Image.asset(
"assets/Onboarding1_Photo.png",
height: MediaQuery.of(context).size.height * 0.7,
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 10, bottom: 50),
child: Text(
'Some Text',
style: TextStyle(
fontSize: 24,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
To stretch view, you can add to Column:
crossAxisAlignment: CrossAxisAlignment.stretch,
Full example with two texts with different paddings and alignment:
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
color: Colors.blue,
height: 300,
),
Padding(
padding: const EdgeInsets.only(
left: 10,
top: 30,
),
child: Text(
'Some Text',
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 24,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
),
Padding(
padding: const EdgeInsets.only(
right: 50,
top: 30,
),
child: Text(
'Some Text',
textAlign: TextAlign.right,
style: TextStyle(
fontSize: 24,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
),
],
),
),
);
}
}
Result:
Solution with two nested Columns using CrossAxisAlignment.start on both and a Padding on the inner Column.
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: HomePage(),
),
);
}
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Image.network(
'https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/Colosseo_2020.jpg/2560px-Colosseo_2020.jpg'),
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Some Text',
style: TextStyle(
fontSize: 24,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
const SizedBox(height: 16.0),
Text('This is some text. This is some text.'),
const SizedBox(height: 16.0),
OutlineButton(
onPressed: () {},
child: Text('Continue'),
),
],
),
),
],
),
);
}
}
You can add crossAxisAlignment on Column widget to start left.
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(padding: const EdgeInsets.only(left: 10, bottom: 50),
child: Text(""),
)
],
)
I am struggling to figure out why my divider in my app bar is not appearing. When I increase the height of the divider, I notice my widgets move up. I looked at similar issues and none have worked. My appBar consists of two columns and one row for the user profile information and information on their "Partner". I wanted to use the divider to separate the partner name and username from the win/loss ratio.strong text
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF2430346),
bottom: PreferredSize(
preferredSize: const Size.fromHeight(100.0),
child: Row(
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.only(left:20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_buildUserInfo(user),
Text(
"#username",
style: new TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: Colors.white,
)
)
],
),
),
),
Padding(
padding: const EdgeInsets.only(right:70.0),
child: Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"My Partner",
style: new TextStyle(
fontSize: 24,
fontWeight: FontWeight.w600,
color: Colors.white,
),
),
Text(
"Dorian",
style: new TextStyle(
color: Colors.white,
),
),
Text(
"#Doetheman",
style: new TextStyle(
color: Colors.white,
),
),
// Mfer aint appearing
Padding(
padding: const EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
child: Divider(
height: 2.0,
color: Colors.white,
),
),
],
),
),
),
],
),
),
),
),
Widget _buildUserInfo(User user) {
return Column(
children: [
Avatar(
photoUrl: user.photoUrl,
radius: 40,
borderColor: Colors.black54,
borderWidth: 2.0,
),
const SizedBox(height: 8),
if (user.displayName != null)
Text(
user.displayName,
style: TextStyle(color: Colors.white),
),
const SizedBox(height: 8),
],
);
}
}
What I want it to look in view:
enter image description here
Instead of divider you can use container like this
Container(
alignment: Alignment.center,
width: 250,
height: 1,
color: Colors.black
),
Use IntrinsicWidth widget to wrap Column
IntrinsicWidth(
child: Column(
children: [
// ....
Divider(),
],
),
)
I have the following code, and my text went overflow in the horizontal direction. I tried to use Flexible and Expanded widgets, but no lucks.
import 'package:flutter/material.dart';
import 'widget/options/qty_widget.dart';
import 'widget/options/option_widget.dart';
import 'package:provider/provider.dart';
import '../session/data.dart';
import '../cart/cart.dart';
class AddItemPage extends StatefulWidget {
final String title = 'Add';
final dynamic data;
final dynamic itemOption;
AddItemPage(this.data, this.itemOption);
#override
State<StatefulWidget> createState() => AddItemPageState();
}
class AddItemPageState extends State<AddItemPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.amber[800],
title: Text(widget.title,
style: TextStyle(color: Colors.amberAccent[800])),
),
body: Builder(builder: (BuildContext context) {
List<Widget> listWidget = <Widget>[];
if (widget.data.containsKey('image')) {
//String imagePath = "assets/images/menu/" + widget.data['image'];
String imagePath = "assets/images/lobster_tray.png";
listWidget.add(
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: Image.asset(
imagePath,
width: 100,
)),
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(widget.data['dish'],
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.w900,
fontSize: 22,
color: Colors.black,
)),
Text(widget.data['description'],
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 16,
color: Colors.black,
)),
Text('\$' + widget.data['price'],
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 16,
color: Colors.red,
))
],
),
],
)
);
}
listWidget.add(QtyWidget());
if(widget.data.containsKey('option')){
for (String optionKey in widget.data['option']){
dynamic optionData = widget.itemOption[optionKey];
listWidget.add(ItemOptionWidget(optionData));
}
}
listWidget.add(Center(
child: Container(
padding: EdgeInsets.fromLTRB(0, 15, 0, 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
padding: const EdgeInsets.symmetric(vertical: 16.0),
alignment: Alignment.center,
child: RaisedButton(
onPressed: () {
Provider.of<PData>(context, listen: false)
.addItemToCart(widget.data);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CartPage()));
},
color: Colors.amber[800],
child: const Text('Add to Order'),
),
),
Container(
margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
padding: const EdgeInsets.symmetric(vertical: 16.0),
alignment: Alignment.center,
child: RaisedButton(
onPressed: () async {
Navigator.pop(context, true);
},
color: Colors.grey[200],
child: const Text('Cancel'),
),
),
]),
),
));
return Container(
padding: EdgeInsets.fromLTRB(15, 15, 15, 15),
child: Form(
child: ListView(
children: listWidget,
),
),
);
}),
);
}
}
Actually you are not using the proper weight for the Row therefore this problem occurring, I have use the Expanded with Manage the width according to device width using the MediaQuery, please check the below solution of it
class HomeScreen extends StatefulWidget {
#override
State<StatefulWidget> createState() {
// TODO: implement createState
return _HomeScreen();
}
}
class _HomeScreen extends State<HomeScreen> {
var text =
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum";
#override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Home"),
),
body: Container(
height: MediaQuery.of(context).size.height * 0.18,
child: Align(
alignment: Alignment.topCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * 0.3,
height: MediaQuery.of(context).size.height * 0.3,
decoration: BoxDecoration(
color: Colors.black,
borderRadius:
BorderRadius.all(Radius.elliptical(20.0, 20.0)),
)),
SizedBox(
width: 5.0,
),
Container(
width: MediaQuery.of(context).size.width * 0.65,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
flex:1,
child: Text("TITLE IS HERE",
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.w900,
fontSize: 22,
color: Colors.black,
)),
),
Expanded(
flex:3,
child: Text(text,
textAlign: TextAlign.left,
maxLines: 3,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 16,
color: Colors.black,
)),
),
Expanded(
flex:1,
child: Text('\$ 100',
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 16,
color: Colors.red,
)),
),
],
))
],
)),
));
}
}
And please check the output of it
Wrap the Column widget which is inside your Row with Flexible widget that will allow to take only minimum space required to render the children inside Column. Working code snippet below:
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: Image.asset('assets/placeholder.png', width: 100)
),
Flexible(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('dish', textAlign: TextAlign.left, style: TextStyle(
fontWeight: FontWeight.w900, fontSize: 22, color: Colors.black
),),
Text('This is a long description of the dish presented in the picture and loved by customers.',
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 16,
color: Colors.black,
)),
Text('\$' + 'price',
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 16,
color: Colors.red,
))
],
)
)
],
)
)
Hope this resolves your issue.
I have the following code:
class _StandardCardState extends State<StandardCard> {
#override
Widget build(BuildContext context) {
return Container(
child: Card(
margin: EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0.0),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
children: <Widget>[
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
children: <Widget>[
Text(
'L*: ',
style: TextStyle(
fontSize: 18.0, color: Colors.grey[600]),
),
Text(
widget.standard.L.toString(),
style: TextStyle(
fontSize: 18.0, color: Colors.grey[600]),
),
],
),
SizedBox(height: 6.0),
Row(
children: <Widget>[
//some children
],
),
SizedBox(height: 6.0),
Row(
children: <Widget>[
//some children
],
),
],
),
),
PopupMenuButton<WhyFarther>(
onSelected: (WhyFarther result) {
setState(() {
var _selection = result;
});
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<WhyFarther>>[
const PopupMenuItem<WhyFarther>(
value: WhyFarther.harder,
child: Text(
'Working a lot harder',
style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
),
),
const PopupMenuItem<WhyFarther>(
value: WhyFarther.smarter,
child: Text(
'Being a lot smarter',
style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
),
),
const PopupMenuItem<WhyFarther>(
value: WhyFarther.selfStarter,
child: Text(
'Being a self-starter',
style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
),
),
const PopupMenuItem<WhyFarther>(
value: WhyFarther.tradingCharter,
child: Text(
'Placed in charge of trading charter',
style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
),
),
],
)
],
),
),
),
);
}
}
which is then still called through another function by a ListView.builder. when I try to use Swatches to change the Text Style inside a popupMenuItem, I get the error ´'Invalid constant value'´.
Why does this throw an error while the text widget above doesn't? And how can I avoid that and how do I use swatches inside PopupMenuButtons then?
i only had to remove the ´const´ before the PopupMenuItem... Sometimes reading when copycoding helps a lot.