Flutter: Failed assertion: line 1927 pos 12: 'hasSize' - flutter

I want to add an image in the background. I found this code snippet here in StackOverflow.
SizedBox.expand(
// -> 01
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://wallpapercave.com/wp/wp5561184.jpg'),
fit: BoxFit.cover, // -> 02
),
),
),
),
this worked quite well for me in this code:
enter codeimport 'package:flutter/material.dart';
class Welcome extends StatelessWidget {
final int _picState;
final VoidCallback _picStateChange;
final Function _quizStarted;
Welcome(this._quizStarted, this._picState, this._picStateChange);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
alignment: AlignmentDirectional.bottomCenter,
children: [
SizedBox.expand(
// -> 01
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://wallpapercave.com/wp/wp5561184.jpg'),
fit: BoxFit.cover, // -> 02
),
),
),
),
SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Spacer(),
Text(
'Start Application',
style: Theme.of(context).textTheme.headline3?.copyWith(
color: Colors.white54,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
Spacer(),
Spacer(),
TextField(
onTap: () => _picStateChange(),
decoration: InputDecoration(
focusColor: Colors.amber,
filled: true,
fillColor: Colors.white.withOpacity(0.2),
hintText: 'ENTER YOUR NAME HERE',
hintStyle: TextStyle(
wordSpacing: 2,
fontWeight: FontWeight.bold,
color: Colors.white,
decoration: TextDecoration.underline),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(12))),
),
),
InkWell(
onTap: () => _quizStarted(),
child: Container(
width: double.infinity,
alignment: Alignment.center,
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
gradient: (LinearGradient(
colors: [Colors.lime, Colors.blueGrey.shade700],
begin: Alignment.topRight,
end: Alignment.bottomLeft))),
child: Text('START THE APPLICATION',
style: Theme.of(context)
.textTheme
.button
?.copyWith(color: Colors.white38)),
),
),
Spacer(),
],
),
))
],
),
);
}
}
but when I used the same code in another section(the one below) the error "Failed assertion: line 1927 pos 12: 'hasSize'" appears
**import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final VoidCallback ansk;
final String gimme_Da_Answer;
Answer(this.ansk, this.gimme_Da_Answer);
#override
Widget build(BuildContext context) {
return Stack(
alignment: AlignmentDirectional.bottomCenter,
children: [
SizedBox.expand(
// -> 01
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://wallpapercave.com/wp/wp5561184.jpg'),
fit: BoxFit.fitHeight, // -> 02
),
),
),
),
SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 18, horizontal: 17),
child: InkWell(
onTap: () => ansk(),
child: (Container(
child: Text(gimme_Da_Answer,textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25,
color: Colors.cyan.shade800,
fontWeight: FontWeight.bold)),
width: double.infinity,
padding: EdgeInsets.all(15),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border.all(color: Colors.blue, width: 4),
color: Colors.blueAccent.shade100.withOpacity(0.4),
borderRadius: BorderRadius.all(Radius.circular(15))),
)),
),
),
),
]);
}
}**
^the code that shows error for me
THANKS A LOT!!!

The error occurs here because the Widget is looking for a size since fit has been set on the BoxDecoration. To solve this issue, set a width and height on the Container.
Container(
// set Container dimension
width: 100,
height: 100,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(),
fit: BoxFit.cover,
),
),
)

Related

Flutter: Only gridview is scrollable

I am trying to make my whole page scrollable on my app, but for some unknown reason, only the grid view is scrolling can someone help me figure out why this is happening?
Thanks a million.
Here is my code:
#override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
Container(
width: double.infinity,
height: SizeConfig.screenHeight / 2.5,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/food2.png'),
fit: BoxFit.cover)),
),
const Align(
alignment: Alignment.centerLeft,
child: Text(
"Donation Cards",
style: TextStyle(
fontFamily: 'Quicksand',
fontSize: 31,
color: Colors.black,
fontWeight: FontWeight.w300,
),
),
),
const Divider(color: Colors.black38),
const SizedBox(
height: 20,
),
Expanded(
child: GridView.count(
crossAxisCount: 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
children: _listItem
.map((item) => Card(
color: Colors.transparent,
elevation: 0,
//child: GestureDetector(
child: GestureDetector(
onTap: () => Navigator.pushNamed(context, item.route),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
image: DecorationImage(
image: AssetImage(item.image),
fit: BoxFit.cover)),
)),
))
.toList(),
))
],
),
);
}
}
Thanks once again!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Colunm wrap with singlechildscrollview and give gridview physics:NeverScrollableScrollPhysics()

I want to design grid like this in flutter:

I want to design a Grid like the below output in flutter.
Please help with this.
The Code I have tried is this:
Container(
height: 100,
width: 170,
margin: const EdgeInsets.symmetric(horizontal: 30),
child: DecoratedBox(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.white,
),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.black
),
margin: const EdgeInsets.all(1.5),
child: const Center(
child: Text(
"data",
style: TextStyle(color: Colors.white),
),
),
),
),
),
And the output I get is this:
Thanks in advance for your help.
What you can do to maintain a grid is use Wrap.
Not very efficient for long list of items but for short it will suffice.
Take a look at the code below :
( You can run this code using Dartpad )
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.all(16),
child: LayoutBuilder(builder: (c, box) {
var crossCount = 2;
var spacing = 15.0;
var maxWidth = (box.maxWidth - ((crossCount - 1) * spacing)) / 2;
return Wrap(
spacing: spacing,
runSpacing: spacing,
children: List.generate(
10,
(i) => Stack(
children: [
Container(
width: maxWidth,
height: maxWidth * 0.55 * 0.5,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(topLeft: Radius.circular(16)),
gradient : LinearGradient(
end: Alignment.topLeft,
begin: Alignment.bottomRight,
colors: [
Colors.black,
Colors.black,
Colors.amber,
],
),
// color: Colors.white,
// boxShadow: [BoxShadow(color: Colors.black, blurRadius: 4)],
),
),
Container(
width: maxWidth,
height: maxWidth * 0.55,
margin: const EdgeInsets.all(2),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: Colors.black,
),
)
],
),
),
);
}),
),
),
);
}
}
Note : Replace the Container's child with your own UI logic for implementing the design you like.
I tried it by myself and manage to built the same.
This is the Code:
Padding(
padding: const EdgeInsets.all(10),
child: GridTile(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
"Gate 2",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w500,
color: Colors.white),
),
const Text(
"Unlocked",
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w500,
color: Colors.grey),
),
const SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const Icon(
Icons.lock,
color: Colors.white,
size: 30,
),
const SizedBox(width: 5),
Image.asset("assets/swipe.png",height: 30,width: 30,),
const SizedBox(width: 5),
// const Icon(Icons.lock, color: Colors.white,size: 30,),
Draggable(
axis: Axis.horizontal,
maxSimultaneousDrags: 3,
rootOverlay: false,
child: Container(
padding: const EdgeInsets.all(5),
decoration: const BoxDecoration(
color: Colors.white12,
borderRadius: BorderRadius.all(Radius.circular(100),),
),
child: const Icon(
Icons.lock_open,
color: Colors.orangeAccent,
size: 30,
),
),
feedback: Container(
decoration: const BoxDecoration(
color: Colors.white12,
borderRadius: BorderRadius.all(Radius.circular(100),),
),
child: const Icon(
Icons.lock_open,
color: Colors.transparent,
size: 40,
),
),
),
],
),
],
),
),
),
Here is the Output.

flutter : Text on image in flutter

I want to write text with transparent color on image. like this.
I want to write text over the image but I am not able to do it properly.
This is my code.
I want to write text over the image but I am not able to do it properly.
And I also want transparent color for text.
Please help me.
import 'package:card_example/color_filters.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
static final String title = 'Card Example';
#override
Widget build(BuildContext context) => MaterialApp(
debugShowCheckedModeBanner: false,
title: title,
theme: ThemeData(primarySwatch: Colors.deepOrange),
home: MainPage(title: title),
);
}
class MainPage extends StatefulWidget {
final String title;
const MainPage({
#required this.title,
});
#override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
#override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(
padding: EdgeInsets.all(16),
children: [
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: Stack(
alignment: Alignment.center,
children: [
Ink.image(
image: NetworkImage(
'https://resources.pulse.icc-cricket.com/ICC/photo/2018/04/22/c19486c2-4e5b-48c4-82af-c6d0eebb7bd2/Main.jpg',
),
colorFilter: ColorFilters.greyscale,
child: InkWell(
onTap: () {},
),
height: 240,
fit: BoxFit.cover,
),
Text(
'Sachin Tendulkar',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 24,
),
),
],
),
)
],
),
);
}}
I want to write text over the image but I am not able to do it properly.
And I also want transparent color for text.
Please help me.
If want to use for that container and I use decoration for image text widget for text then how to make exact that thing like is in image.
If you consider splashColor, there are multiple ways to handle this:
using Ink.image() inside Stack will not provide circular borderRadius. Check this GitHub issue. is still open.
ClipRRect(
borderRadius: BorderRadius.circular(12),
child: SizedBox(
width: 300,
height: 300,
child: Stack(
fit: StackFit.expand,
children: [
Align(
alignment: Alignment.bottomCenter,
child: Container(
width: double.infinity,
padding: EdgeInsets.all(8),
color: Colors.blue.withOpacity(.5),
child: Text(
'Sachin Tendulkar',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 24,
),
),
),
),
Ink.image(
image: NetworkImage(
'https://resources.pulse.icc-cricket.com/ICC/photo/2018/04/22/c19486c2-4e5b-48c4-82af-c6d0eebb7bd2/Main.jpg',
),
fit: BoxFit.cover,
child: InkWell(
borderRadius: BorderRadius.circular(12),
onTap: () {},
),
),
],
)),
);
1: Using GridTile
Container(
width: 300,
height: 300,
color: Colors.transparent,
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: InkWell(
onTap: () {},
child: GridTile(
child: Image.network(
'https://resources.pulse.icc-cricket.com/ICC/photo/2018/04/22/c19486c2-4e5b-48c4-82af-c6d0eebb7bd2/Main.jpg',
fit: BoxFit.cover,
),
footer: Container(
padding: EdgeInsets.all(8),
color: Colors.blue.withOpacity(.5),
child: Text(
'Sachin Tendulkar',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 24,
),
),
),
),
),
),
);
2: Using decoration of Container
InkWell(
onTap: () {},
splashColor: Colors.red,
child: Container(
width: 300,
height: 300,
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
image: DecorationImage(
image: NetworkImage(
'https://resources.pulse.icc-cricket.com/ICC/photo/2018/04/22/c19486c2-4e5b-48c4-82af-c6d0eebb7bd2/Main.jpg',
),
fit: BoxFit.cover,
),
),
child: Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.blue.withOpacity(.5),
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(12),
bottomRight: Radius.circular(12),
),
),
padding: EdgeInsets.all(8),
child: Text(
'Sachin Tendulkar',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 24,
),
),
),
),
);
Try adding bottomCenter to alignment
Stack(
alignment: Alignment.bottomCenter, //Here
children: [
Ink.image(
image: NetworkImage(
'https://static.remove.bg/remove-bg-web/3661dd45c31a4ff23941855a7e4cedbbf6973643/assets/start_remove-79a4598a05a77ca999df1dcb434160994b6fde2c3e9101984fb1be0f16d0a74e.png',
),
child: InkWell(
onTap: () {},
),
height: 240,
fit: BoxFit.cover,
),
Text(
'Sachin Tendulkar',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 24,
),
),
],
);
Result:

Flutter TextField Hint Text Not Align Center

Container(
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(5.0),
bottomLeft: Radius.circular(5.0)
),
color: Colors.white,
),
padding: EdgeInsets.only(left: 15.w, right: 15.w),
child: TextField(
maxLines: 1,
style: TextStyle(
fontSize: 12, //This line!!!!!!!!!
textBaseline: TextBaseline.alphabetic,
),
decoration: InputDecoration(
border: InputBorder.none,
hintText: configNotifier.translationObject["SearchOrder"],
hintStyle: TextStyle(
color: fromCSSColor("#808080"),
textBaseline: TextBaseline.alphabetic
),
),
),
)
Before I add "fontSize: 12"
After I add "fontSize: 12"
How to apply font size to textfield with vertical center alignment?
Remark: I've tried "textAlignVertical: TextAlignVertical.center" and hint->textstyle "height:1.0" which is not working
========Edit 1===========
IOS Device is work fine but not on Samsung Galaxy Tab A (8.0", 2019) which model is SM-T295C).
========Edit 2===========
With Parent Widget
Container(
margin: EdgeInsets.only(left: 70, right: 70),
height: 50.h,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(5.0),
bottomLeft: Radius.circular(5.0)
),
color: Colors.white,
),
padding: EdgeInsets.only(left: 15.w, right: 15.w),
child: TextField(
maxLines: 1,
style: TextStyle(
fontSize: 12, //This line!!!!!!!!!
textBaseline: TextBaseline.alphabetic,
),
decoration: InputDecoration(
border: InputBorder.none,
hintText: configNotifier.translationObject["SearchOrder"],
hintStyle: TextStyle(
color: fromCSSColor("#808080"),
textBaseline: TextBaseline.alphabetic
),
),
),
),
),
GestureDetector(
onTap: (){
//...
},
child: Container(
width: 100.w,
height: 50.h,
margin: EdgeInsets.only(right: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: fromCSSColor("#000000"),
),
child: Center(
child: Text(
"Submit",
style: TextStyle(
color: fromCSSColor("#000000"),
fontSize: 12
),
textAlign: TextAlign.center,
),
)
)
),
GestureDetector(
onTap: (){
//...
},
child: Container(
width: 65.w,
height: 50.h,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: fromCSSColor("#000000"),
),
child: Center(
child: Container(
width: 41.w,
height: 41.h,
alignment: Alignment.center,
child: Image.asset('assets/images/icon_scan_white.png', fit: BoxFit.contain),
),
),
)
)
],
)
)
Try to textalign.center in TextField like below
TextField(
textAlign : TextAlign.center,
),
your screen look like this-
try contentPadding inside decoration. and dont need to use padding on Expanded Container.
import 'package:flutter/material.dart';
class CounterList extends StatefulWidget {
#override
_CounterListState createState() => _CounterListState();
}
class _CounterListState extends State<CounterList> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.red,
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: [
CustomField(
fontSize: 17,
),
SizedBox(
height: 22,
),
CustomField(
fontSize: 37,
),
SizedBox(
height: 20,
),
CustomField(
fontSize: 11,
),
SizedBox(
height: 20,
),
CustomField(
fontSize: 22,
),
],
),
));
}
}
class CustomField extends StatelessWidget {
final double fontSize;
CustomField({
Key? key,
required this.fontSize,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(left: 70, right: 70),
height: 50,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(5.0),
bottomLeft: Radius.circular(5.0)),
color: Colors.white,
),
// padding: EdgeInsets.only(left: 15.w, right: 15.w),
child: TextField(
maxLines: 1,
style: TextStyle(
fontSize: fontSize, //This line!!!!!!!!!
textBaseline: TextBaseline.alphabetic,
),
decoration: InputDecoration(
contentPadding: EdgeInsets.only(left: 15, right: 15),
border: InputBorder.none,
hintText: "search",
// hintText: configNotifier.translationObject["SearchOrder"],
hintStyle: TextStyle(
// color: fromCSSColor("#808080"),
color: Colors.grey,
textBaseline: TextBaseline.alphabetic,
),
),
),
),
),
GestureDetector(
onTap: () {
//...
},
child: Container(
width: 100,
height: 50,
margin: EdgeInsets.only(right: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
// color: fromCSSColor("#000000"),
color: Colors.black,
),
child: Center(
child: Text(
"Submit",
style: TextStyle(
// color: fromCSSColor("#000000"),
color: Colors.black,
fontSize: 12,
),
textAlign: TextAlign.center,
),
))),
GestureDetector(
onTap: () {
//...
},
child: Container(
width: 65,
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
// color: fromCSSColor("#000000"),
color: Colors.black,
),
child: Center(
child: Container(
width: 41,
height: 41,
alignment: Alignment.center,
child: Image.asset(
// 'assets/images/icon_scan_white.png',
"assets/me.jpg",
fit: BoxFit.contain,
),
),
),
))
],
));
}
}
Try it:
TextField(
textAlign : TextAlign.center,
textAlignVertical: TextAlignVertical.center,
),

How to avoid overflow by using SingleChildScrollView

I am very new with flutter and im very stuck in a positioning.
Does anybody know where should I place the "SingleChildScrollView"? I get stack overflow 148PX.
my code looks like this,i tried in all the posible ways, but i still didnt managed how to fix it.
I can't understand where should i put the body :
class SignUp extends StatefulWidget {
#override
_SignUpState createState() => _SignUpState();
}
class _SignUpState extends State<SignUp> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
alignment: Alignment.bottomCenter,
width: 400,
height: 350,
decoration: BoxDecoration(
image: DecorationImage(
image: ExactAssetImage('assets/images/qlogo1.png'))),
),
SizedBox(height: 1),
Container(
alignment: Alignment.bottomCenter,
padding: EdgeInsets.symmetric(horizontal: 40),
child: Column(
children: [
TextField(
style: mediumTextStyle(),
decoration: textFieldInputDecoration('username'),
),
TextField(
style: mediumTextStyle(),
decoration: textFieldInputDecoration('password'),
),
],
),
),
SizedBox(
height: 25,
),
// creaza spatii intre containere
Container(
alignment: Alignment.centerRight,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 37, vertical: 8),
child: Text(
'Forgot Password?',
style: TextStyle(
color: Colors.white,
fontSize: 14,
fontFamily: 'Quicksand',
decoration: TextDecoration.underline),
)),
),
SizedBox(height: 24),
Container(
alignment: Alignment.center,
width: MediaQuery.of(context).size.width - 70,
padding: EdgeInsets.symmetric(vertical: 20),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [const Color(0xFFF06292), const Color(0xff2A75BC)]),
borderRadius: BorderRadius.circular(30),
),
child: Text(
'Sign In',
style: mediumTextStyle(),
),
),
SizedBox(
height: 16,
),
Container(
alignment: Alignment.center,
width: MediaQuery.of(context).size.width - 70,
padding: EdgeInsets.symmetric(vertical: 20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(30),
),
child: Text('Register',
style: TextStyle(
color: Colors.black87,
fontFamily: 'Quicksand',
fontSize: 17)),
),
],
),
);
}
}
Thank you!
If you want all you screen to be scrolled - do this:
return Scaffold(
body: SingleChildScrollView(
child: Column(
// your other code
)
)
);