Flutter Angela Yu example - flutter

I want to print first element in the list "first ques"
but I'm getting different sort of errors tried using final also, then all sort of errors.
class _QuizPageState extends State<QuizPage> {
List<Widget> scoreKeeper = [];
List<String> questions = ["First ques", "seacond ques", "third ques"];
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
"example text"
questions[0], //why its not displaying "first ques"
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),

try this
class _QuizPageState extends State<QuizPage> {
List<Widget> scoreKeeper = [];
List<String> questions = ["First ques", "seacond ques", "third ques"];
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
questions[0], //why its not displaying "first ques"
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
the reason you are getting error is that the Text widget takes only one string and you are passing two string to it.

First, you should remove const from the Expanded widget because you're using a variable inside on Text which is questions[0].
And I want to mention that its better to use questions.first for readability proposes and check if the list is empty before it to prevent possible errors.
So Here's the final result:
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 5,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Center(
child: Text(
questions.first,
//why its not displaying "first ques"
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
],
),

Related

Flutter Flex Widget

I have this class State:
class _ItemCardState extends State<ItemCard> {
double imgSize = 30;
Axis expanded = Axis.horizontal;
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
setState(() {
expanded =
expanded == Axis.horizontal ? Axis.vertical : Axis.horizontal;
imgSize = imgSize == 30 ? 200 : 30;
});
},
child: Card(
margin: const EdgeInsets.all(5.0),
child: Padding(
padding: const EdgeInsets.all(15),
child: Flex(
direction: expanded,
children: [
Image.network(
'http://cdn.shopify.com/s/files/1/0565/0697/4379/articles/Cafe-expresso-sin-maquina_1200x1200.jpg?v=1621619617',
width: imgSize,
),
Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(widget.product['name'],
style: const TextStyle(fontSize: 20)),
Text(widget.product['price'] + ' \$',
style: const TextStyle(fontSize: 15)),
],
),
Text(widget.product['ingredients'],
style: const TextStyle(fontSize: 15, color: Colors.grey)),
],
),
],
),
),
),
);
}
}
I want the Flex Widget change direction onTap, it works.
But the column inside is not taking all space avaible in the crossAxis.
If I put an Expanded Widget it stops working,...
I've tried Flexibles, but somehow it didnt work.
I used ListTiles also, but I couldnt make it work.
Any idea on how to do it?
well. I resolved it putting the Flex Widget inside a SizedBox and then I was able to use Flexible>fit:FlexFit.tigth:
SizedBox(
height: 300,
child: Flex(
direction: expanded,
children: [
Image.network(
'http://cdn.shopify.com/s/files/1/0565/0697/4379/articles/Cafe-expresso-sin-maquina_1200x1200.jpg?v=1621619617',
width: imgSize,
),
Flexible(
fit: FlexFit.tight,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(widget.product['name'],
style: const TextStyle(fontSize: 20)),
Text(widget.product['price'] + ' \$',
style: const TextStyle(fontSize: 15)),
],
),
Text(widget.product['ingredients'],
style: const TextStyle(
fontSize: 15, color: Colors.grey)),
],
),
)
],
),
),

A value of type 'Null' can't be assigned to a parameter of type 'String' in a const constructor

I'm unable to use questions[questionNumber] as a Text Constructor in Flutter.
Errors:
Evaluation of this constant expression throws an exception.dart(const_eval_throws_exception)
A value of type 'Null' can't be assigned to a parameter of type 'String' in a const constructor.
Try using a subtype, or removing the keyword 'const'.dartconst_constructor_param_type_mismatch
Arguments of a constant creation must be constant expressions.
Try making the argument a valid constant, or use 'new' to call the constructor.dartconst_with_non_constant_argument
class _QuizPageState extends State<QuizPage> {
List<Widget> scoreKeeper = [];
List<String> questions = [
'You can lead a cow down stairs but not up stairs.',
'Approximately one quarter of human bones are in the feet.',
'A slug\'s blood is green.'
];
int questionNumber = 0;
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
questions[questionNumber],
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
],
);
}
}
Well, the error is due to using the keyword const for the Expanded widget. Just remove it and you will be all good.
So this:
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
questions[questionNumber],
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
],
);
}
}

Text Wrapping in Flutter

I am writing a widget in flutter for a project which should look like this, I am new to flutter and I am trying to imitate the card below
So far this is my code for the widget
import 'package:flutter/material.dart';
import 'package:smooth_star_rating/smooth_star_rating.dart';
class TrailCard extends StatelessWidget {
const TrailCard({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
child: InkWell(
onTap: () {},
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: Image.asset(
'assets/images/cocoa.jpg',
height: 100,
width: 90,
fit: BoxFit.fitHeight,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Boring Cocoa",
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.bold),
),
Text('Rajhamundry, AP'),
Text(
'Feel the authentic flavours of life while you visit the farms around Rajahmundry',
overflow: TextOverflow.ellipsis,
)
],
),
),
],
)),
)),
);
}
}
I tried using flexible and expanded to try and make the content look the way but I am getting overflow error
You can wrap the Padding widget with a Flexible widget so it takes only the amount of space needed:
NOTE: you should also set the maximum amount of lines the Text widget should take before showing the ellipsis.
Flexible(
// new line
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Boring Cocoa",
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
Text('Rajhamundry, AP'),
Text(
'Feel the authentic flavours of life while you visit the farms around Rajahmundry more text to cause overflow',
// set maximum number of lines the text should take
maxLines: 3, // new line
overflow: TextOverflow.ellipsis,
)
],
),
),
);
RESULT:
Wrap Padding with Expanded
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Boring Cocoa",
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.bold),
),
Text('Rajhamundry, AP'),
Text(
'Feel the authentic flavours of life while you visit the farms around Rajahmundry',
overflow: TextOverflow.ellipsis,
)
],
),
),
)

flutter setState isn't defined

I have a Widget to create a sidebar menu:
Widget dashboard(context) {
return Material(
elevation: 8,
color: backgroundColor,
child: Container(
padding: const EdgeInsets.only(left: 16, right: 16, top: 48),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: [
InkWell(child: Icon(Icons.menu, color: Colors.white), onTap: () {
setState(() {
isCollapsed = !isCollapsed;
});
},),
Text("Corona App", style: TextStyle(fontSize: 24.0, color: Colors.white)),
Icon(Icons.settings, color: Colors.white),
])
],
),
)
);
}
But I keep getting the error that my setState function isn't defined. My main app is a statefullwidget so this is pretty weird. Can anyone help me?
Here is the full code!
import 'package:flutter/material.dart';
final Color backgroundColor = Color(0xFF4A4A58);
class MenuDashboard extends StatefulWidget {
#override
_MenuDashboardState createState() => _MenuDashboardState();
}
class _MenuDashboardState extends State<MenuDashboard> {
bool isCollapsed = true;
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: backgroundColor,
body: Stack(
children: <Widget>[
menu(context),
dashboard(context),
],
),
);
}
}
Widget menu(context) {
return Padding(
padding: const EdgeInsets.only(left: 16.0),
child: Align(
alignment: Alignment.centerLeft,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Home", style: TextStyle(color: Colors.white, fontSize: 20),),
SizedBox(height: 10.0),
Text("Brochures", style: TextStyle(color: Colors.white, fontSize: 20),),
SizedBox(height: 10.0),
Text("Visitekaartje", style: TextStyle(color: Colors.white, fontSize: 20),),
SizedBox(height: 10.0),
]
)
)
);
}
Widget dashboard(context) {
return Material(
elevation: 8,
color: backgroundColor,
child: Container(
padding: const EdgeInsets.only(left: 16, right: 16, top: 48),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: [
InkWell(child: Icon(Icons.menu, color: Colors.white), onTap: () {
setState(() {
isCollapsed = !isCollapsed;
});
},),
Text("Corona App", style: TextStyle(fontSize: 24.0, color: Colors.white)),
Icon(Icons.settings, color: Colors.white),
])
],
),
)
);
}
with the error message: 'The function setState isn't defined'
setState is a member of State class, which means it can only be called inside a class that extends a State class of a StatefulWidget. If your class is extending StatelessWidget, you can't called state members/methods provided by the super class, including setState, initState and dispose methods.
For the answer, you can't call setState outside of the State class, if possible, put the widget methods inside the class, or pass setState function as a parameter to those methods.
setState can only called inside StatefulWidget. So if I got it right, you use setState in Widget dashboard or menu widgets. It can't work, because those widgets in outside of class. Try to put widgets inside the class

body of nestedscrollview overflowed - flutter

I made a page with NestedScrollView, the body of NestedScrollView is a container that holds data from previous requests. but the body of the NestedScrollView is overflowed. The following code:
NestedScrollView(
controller: _scrollViewController,
headerSliverBuilder:
(BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
title: new Text(
nama,
style: TextStyle(color: putih),
),
iconTheme: IconThemeData(color: putih),
backgroundColor: colorPrimaryDark,
)
];
},
body: Container(
child: Column(
children: <Widget>[
Container(
color: goldtua,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Text(
string.harga,
style: TextStyle(color: putih, fontSize: 24.0),
),
Text(
formatingRupiah(harga),
style: TextStyle(color: putih, fontSize: 24.0),
),
],
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
string.fasilitas,
style: TextStyle(fontSize: 18.0),
),
Expanded(
child: Text(
fasilitas, //this is the result of the request, the text is multiline so it takes up less space
style: TextStyle(fontSize: 18.0),
),
)
],
),
......
Please, can someone tell me where do I do it wrong?
If you want or need to keep using the NestedScrollView, you can use ListView instead Column to ListView, just like this:
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
class HomePage extends StatefulWidget {
HomePage() : super();
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
CupertinoSliverNavigationBar(
largeTitle: Text('My large title'),
)
];
},
body: ListView( // This is the right way
children: [
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
],
),
),
);
}
}
You will see a screen like this.
Notice, that if you change only the ListView to column, you will get this result:
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
class HomePage extends StatefulWidget {
HomePage() : super();
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
CupertinoSliverNavigationBar(
largeTitle: Text('My large title'),
)
];
},
body: Column( // I changed here from ListView and Column and I will get an overflow error
children: [
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
Text("List item", style: TextStyle(fontSize: 96)),
],
),
),
);
}
}
You're getting this error because the size of your content is bigger than the size of the container. That means the Container size is smaller than the size of Column. In order to solve this issue, you should wrap Container with a Scrollable Widget like SingleChildScrollView which will scroll overflowing content and match parents size or available space. You don't need to use Expanded since each item of Column will fill only needed space
body:
SingleChildScrolView(
child: Column(
children: <Widget>[
Container(
color: goldtua,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Text(
string.harga,
style: TextStyle(color: putih, fontSize: 24.0),
),
Text(
formatingRupiah(harga),
style: TextStyle(color: putih, fontSize: 24.0),
),
],
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
string.fasilitas,
style: TextStyle(fontSize: 18.0),
),
Expanded(
child: Text(
fasilitas, //this is the result of the request, the text is multiline so it takes up less space
style: TextStyle(fontSize: 18.0),
),
)
],
),
......
Simply just use a SingleChildScrollView at the root of body:
body: SingleChildScrollView(
child: Container(
child: Column(
children: <Widget>[.....