Cannot apply decoration to Container in Flutter - flutter

After applying border to my Container, it disappears with this error:
Incorrect use of ParentDataWidget.
After commenting decoration. Widget is rendered.
But i need to give outline border like this:
I am getting this commenting the decoration:
Container(
child: Row(
children: [
Container(
// commenting this line fixes the issue
decoration: BoxDecoration( borderRadius: BorderRadius.circular(10) ),
child: Expanded(
child: Row(
children: [
Icon(LineAwesomeIcons.search),
Expanded(
child: TextField(
decoration: null,
minLines: 1,
maxLines: 2,
controller: pickupController,
onChanged: (val) {
},
),
),
Icon(LineAwesomeIcons.times),
],
),
),
),
SizedBox(width: 20,),
Text('Map'),
],
),
)

You can just decorate the TextField.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Row(
children: <Widget>[
Expanded(
child: TextField(
decoration: InputDecoration(
prefixIcon: const Icon(Icons.search),
suffixIcon: const Icon(Icons.close),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(
color: Colors.grey,
width: 1,
),
),
),
),
),
FlatButton(
onPressed: () {},
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
visualDensity: VisualDensity.compact,
child: const Text('Map'),
),
],
),
),
);
}
}

This should do the job:
Row(
children: [
Flexible(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: controller,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.search,
color: Colors.black,
),
suffixIcon: IconButton(
icon: Icon(
Icons.close,
color: Colors.black,
),
onPressed: () {
//TODO: implement functionality here.
},
),
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black.withOpacity(
0.1,
),
),
borderRadius: BorderRadius.circular(10),
),
),
),
),
),
Text(
"Map",
style:
TextStyle(fontWeight: FontWeight.w400, fontSize: 25),
)
],
),
Where did you go wrong:
The Row widget wants to determine the intrinsic size of its non-flexible children so it knows how much space that it has left for the flexible ones. However, TextField doesn't have an intrinsic width; it only knows how to size itself to the full width of its parent container. Try wrapping it in a Flexible or Expanded to tell the Row that you're expecting the TextField to take up the remaining space:

add width size to the second Container
then add border: Border.all() to BoxDecoration
Container(
child: Row(
children: [
Container(
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
border: Border.all(),
),
child: Expanded(
child: Row(
children: [
Icon(Icons.search),
Expanded(
child: TextField(
decoration: null,
minLines: 1,
maxLines: 2,
onChanged: (val) {},
),
),
Icon(Icons.clear),
],
),
),
),
SizedBox(
width: 20,
),
Text('Map'),
],
),
),

Related

Flutter Bottom Overflow on custom bottom sheet

You can see the problem here (I turned on flutter inspector gridlines so you can see the problem better)
Overflow disappears when I wrap my Container inside Expanded,
but then my search bar height is just stretching and it isn't good. Tried to use SingleChildScrollView but it didn't do the trick.
Container(
margin: const EdgeInsets.symmetric(
horizontal: AppConstants.bottomModalSheetSearchMarin,
vertical: AppConstants.bottomModalSheetSearchMarin),
decoration: BoxDecoration(
color: AppColors.searchBoxBackgroundColor,
borderRadius:
BorderRadius.circular(AppConstants.searchBoxRadius),
),
child: const TextField(
// onChanged: (value)=>controller.filterMountain(value),
decoration: InputDecoration(
hintStyle:
TextStyle(fontSize: AppFontSizes.searchBoxHintText),
hintText: 'Search',
suffixIcon: Icon(
Icons.search,
color: AppColors.searchBoxSearchIconColor,
),
border: InputBorder.none,
contentPadding:
EdgeInsets.all(AppPaddings.searchBoxPadding),
),
),
),
The problem is in the search bar container (we can see that from flutter inspector - show gridlines)
This is how it looks when I wrap Container with Expanded, (don't be bothered with the different look of the cards below)
Edit:
Here is the full bottomSheet code as requested:
bottomNavigationBar: SolidBottomSheet(
draggableBody: false,
headerBar: Container(
child: ClipRRect(
borderRadius: const BorderRadius.only(
topRight: Radius.circular(20),
topLeft: Radius.circular(20)
),
child: OutlinedButton(
child: Column(
children: const [
Icon(Icons.expand_less,),
Text("Search pois")
],
),
onPressed: () {}),
),
),
body: Container(
color:Colors.blue,
child: Column(
children: [
Container(
decoration: BoxDecoration(
color:Colors.red,
borderRadius: BorderRadius.circular(20),
),
child: const TextField(
decoration: InputDecoration(,
hintText: 'Search',
suffixIcon: Icon(Icons.search,),
),
),
),
Obx(
),
],
),
),
),
You can wrap TextFiled's Container with Expanded and enable expanded on TextFiled.
body: Container(
child: Column(
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(22),
),
child: TextField(
expands: true,
maxLines: null,
minLines: null,
)),
),
],
),
),
SingleChildScrollView also work
body: SingleChildScrollView(
child: Column(
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(22),
),
child: TextField(
maxLines: 3,
minLines: 1,
)),
],
),
),
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
bottomNavigationBar: SolidBottomSheet(
draggableBody: true,
headerBar: Container(
child: ClipRRect(
borderRadius: const BorderRadius.only(
topRight: Radius.circular(22), topLeft: Radius.circular(22)),
child: OutlinedButton(
child: Column(
children: const [
Icon(
Icons.expand_less,
),
Text(
"Search pois",
)
],
),
onPressed: () {}),
),
),
body: SingleChildScrollView(
child: Column(
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(22),
),
child: TextField(
maxLines: 3,
minLines: 1,
)),
],
),
),
),
);
}
}

How to set the size of Text field Button

I am building a Flutter application where I am trying to build simple login UI but having problem as shown in image below. One problem is the size of the email field and the password field is different and another is login button is being overflowed by 13-pixels? Also want to set the height underscore of Forget your password. How can It be solved? Thank you in advance.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Navigation Bar',
theme: ThemeData(
primaryColor: Color(0xFFC41A3B),
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var _isVisible = false;
#override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
constraints: BoxConstraints(
maxHeight: MediaQuery.of(context).size.height,
),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: const [
Colors.blue,
],
begin: Alignment.topLeft,
end: Alignment.centerRight,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
flex: 2,
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 36.0,
horizontal: 24.0,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text(
'Login',
style: TextStyle(
color: Colors.white,
fontSize: 46.0,
fontWeight: FontWeight.w800,
),
),
SizedBox(
height: 10.0,
),
Text(
'Enter to the butiful world',
style: TextStyle(
color: Colors.white,
fontSize: 22.0,
fontWeight: FontWeight.w300,
),
),
],
),
),
),
Expanded(
flex: 2,
child: Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
fillColor: Color(0xffe73ded),
hintText: "E-mail",
prefixIcon: Icon(
Icons.email,
color: Colors.grey,
),
)),
SizedBox(
height: 20.0,
),
TextField(
obscureText: _isVisible ? false :true,
keyboardType: TextInputType.visiblePassword,
decoration: InputDecoration(
suffix: IconButton(
onPressed: () {
setState(() {
_isVisible = !_isVisible;
});
},
icon: Icon(
_isVisible ? Icons.visibility : Icons.visibility_off,
color: Colors.grey,
),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
fillColor: Color(0xffe73ded),
hintText: "Password",
prefixIcon: Icon(
Icons.lock_rounded,
color: Colors.grey,
),
)),
SizedBox(
height: 5.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () {},
child: Text(
'Forgot your password',
style: TextStyle(
color: Colors.blue,
decoration: TextDecoration.underline,
),
),
),
],
),
SizedBox(
height: 20.0,
),
Container(
color: Colors.blue,
width: double.infinity,
child: ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(28),
),
),
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 16.0),
child: Text(
'Login',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
),
),
),
),
),
SizedBox( height: 2,),
RichText(
text: TextSpan(
text: 'Do you have an account?',
style: TextStyle(
color: Colors.black,
fontSize: 18,
),
children: [
TextSpan(
text: 'Register',
style: TextStyle(
color: Colors.blue,
fontSize: 18,
),
recognizer: TapGestureRecognizer()
..onTap = () {}),
],
),
),
],
),
),
),
),
],
),
),
),
);
}
}
The issue is you created a container with max height as the device.then used expanded widgets which are equally spaced. You can try increasing the flex value of the second expanded widget
Expanded(
flex: 3
)
Now the first and second expanded will be of ratio 2:3 which will give more space to the second widget

Textfields shrinking whenever keyboard appears in flutter

I am trying to create an simple login form, there are 3 components in a column: row and 2 text fields.
The row contains an image and text.
The problem is whenever the keyboard appears while I want to type something in the textfield, all UI is shrinking.
I have attached before and after keyboard appears
How to make it such that whenever keyboard appears the elements hidden below the keyboard?
Here is my code:
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Flexible(
fit: FlexFit.tight,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Flexible(
child: Padding(
padding: EdgeInsets.only(top: 50),
child: Hero(
tag: "logo",
child: Image.asset(
"images/flash.png", width: 70, height: 50,)
),
),
),
Flexible(
child: Padding(
padding: EdgeInsets.only(top: 50),
child: Text(
"Demo Chat",
style: TextStyle(
fontSize: 40,
color: Colors.black,
fontWeight: FontWeight.bold
),
),
),
)
],
),
),
Flexible(
fit: FlexFit.tight,
child: SizedBox(
height: 50,
),
),
Flexible(
fit: FlexFit.tight,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
decoration: kTextFieldDecorationEmail,
keyboardType: TextInputType.emailAddress,
),
),
),
Flexible(
fit: FlexFit.tight,
child: SizedBox(
height: 50,
),
),
Flexible(
fit: FlexFit.tight,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
decoration: kTextFieldDecorationPassword,
keyboardType: TextInputType.visiblePassword,
obscureText: true,
),
),
),
Flexible(
fit: FlexFit.tight,
child: SizedBox(
height: 70,
),
),
Flexible(
fit: FlexFit.tight,
child: Padding(
padding: EdgeInsets.all(20),
child: MaterialButton(
color: Colors.blueAccent,
onPressed: (){},
height: 50,
child: Text(
"Login",
style: TextStyle(
color: Colors.black
),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)
),
),
),
)
],
),
That is because you are using flexible with FlexFit.tight, this allows it to fit into screen regardless of the size. I would suggest you to use MediaQuery or some other field for this to tackle. An example to eliminate this would be something like this. I have edited your code a slight bit since I do not have the image and decoration at the moment which you are using I have put in some random one for the time being.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.only(top: 50),
child: Text(
"Demo Chat",
style: TextStyle(
fontSize: 40,
color: Colors.black,
fontWeight: FontWeight.bold),
),
)
],
),
SizedBox(
height: 50,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Username',
filled: true,
fillColor: Colors.grey,
contentPadding:
const EdgeInsets.only(left: 14.0, bottom: 6.0, top: 8.0),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red),
borderRadius: BorderRadius.circular(10.0),
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
borderRadius: BorderRadius.circular(10.0),
),
),
keyboardType: TextInputType.emailAddress,
),
),
SizedBox(
height: 50,
),
Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Password',
filled: true,
fillColor: Colors.grey,
contentPadding:
const EdgeInsets.only(left: 14.0, bottom: 6.0, top: 8.0),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red),
borderRadius: BorderRadius.circular(10.0),
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
borderRadius: BorderRadius.circular(10.0),
),
),
keyboardType: TextInputType.visiblePassword,
obscureText: true,
),
),
Flexible(
fit: FlexFit.tight,
child: SizedBox(
height: 70,
),
),
Padding(
padding: EdgeInsets.all(20),
child: MaterialButton(
color: Colors.blueAccent,
onPressed: () {},
height: 50,
child: Text(
"Login",
style: TextStyle(color: Colors.black),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
),
)
],
),
),
);
}
}
Screen:

How to put multiple suffix icon in TextField flutter?

I've been trying hard to get this setup, but I couldn't just get to behave the icon the way I wanted it to be.
[1]: https://i.stack.imgur.com/mJFds.png
I wanted to make the add button snap with the TextField, but I could not wrap it with any other widgets.
All I could end up is this.
[2]: https://i.stack.imgur.com/m2Ze9.png
Can somebody please help me figure out a way over this.
Thanks in advance.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:badminton_app/constants.dart';
import 'package:badminton_app/model/players_data.dart';
TextEditingController _controller = TextEditingController();
class AddPlayersScreen extends StatefulWidget {
#override
_AddPlayersScreenState createState() => _AddPlayersScreenState();
}
class _AddPlayersScreenState extends State<AddPlayersScreen> {
String newText;
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xff07021A),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(25.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30.0,
),
Text(
'Add Players',
style: TextStyle(
color: Colors.white,
fontSize: 40.0,
fontFamily: 'Montserrat'),
),
SizedBox(
width: 400.0,
height: 50.0,
child: Divider(
height: 10.0,
color: Color(0xff525274),
),
),
Container(
constraints: BoxConstraints.tight(Size(400.0, 70.0)),
child: TextField(
cursorColor: Color(0xffA8A3BE),
style: TextStyle(color: Color(0xffA8A3BE), fontSize: 20.0),
controller: _controller,
onChanged: (newValue) {
newText = newValue;
},
enabled: true,
decoration: InputDecoration(
suffix: IconButton(
onPressed: () {
_controller.clear();
},
icon: Icon(
Icons.clear,
color: Colors.white,
),
),
suffixIcon: IconButton(
onPressed: () {
Provider.of<PlayerData>(context).changeString(newText);
},
icon: CircleAvatar(
backgroundColor: Colors.amberAccent,
child: Icon(
Icons.add,
color: Colors.black,
)),
),
hintText: "New member......",
hintStyle: TextStyle(
fontSize: 20.0,
color: Color(
0xffA199C6,
),
),
filled: true,
fillColor: Color(0xff585179),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
borderSide: BorderSide.none),
),
),
)
//Something(),
],
),
),
),
);
}
}
suffixIcon: Container(
width: 100.w,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
IconButton(
padding: EdgeInsets.zero,
constraints: BoxConstraints(),
onPressed: () {
print('mic button pressed');
},
icon: Icon(
Icons.mic,
color: background_color,
),
),
IconButton(
padding: EdgeInsets.fromLTRB(1, 0, 1, 0).r,
constraints: BoxConstraints(),
onPressed: () {
print('photo button pressed');
},
icon: Icon(
Icons.photo,
color: background_color,
),
),
IconButton(
padding: EdgeInsets.fromLTRB(2, 0, 3, 0).r,
constraints: BoxConstraints(),
onPressed: () {
print('Emoji button pressed');
},
icon: Icon(
Icons.emoji_emotions,
color: background_color,
),
),
],
),
),
_cellEdit() => Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: Colors.white,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
child: Text('断开'),
padding: EdgeInsets.all(BaseSize.dp(10)),
),
Row(
children: <Widget>[
ImageIcon(IconUtils.getAssetIcon('ic_bluetooth'),
color: ColorRes.COLOR_03B798),
Container(
margin: EdgeInsets.only(left: BaseSize.dp(3)),
child: Text('A8-123456789'))
],
mainAxisAlignment: MainAxisAlignment.start,
),
],
),
);
You can refer to this to re-layout your layout

TextField inside of Row it broke the code

I'm trying to make a custom search widget. when I add TextField inside of Row it broke the code.
My code without TextField.
import 'package:flutter/material.dart';
class SearchWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.yellowAccent,
borderRadius: BorderRadius.circular(500),
),
child: Row(
children: [
Container(
decoration: BoxDecoration(
color: Colors.redAccent,
borderRadius: BorderRadius.circular(500),
),
child: IconButton(icon: Icon(Icons.search), onPressed: () {}),
),
],
),
);
}
}
But after adding TextField everything will gon.
my code with TextField inside of Row.
import 'package:flutter/material.dart';
class SearchWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.yellowAccent,
borderRadius: BorderRadius.circular(500),
),
child: Row(
children: [
Container(
decoration: BoxDecoration(
color: Colors.redAccent,
borderRadius: BorderRadius.circular(500),
),
child: IconButton(icon: Icon(Icons.search), onPressed: () {}),
),
TextField(
decoration: InputDecoration(
labelText: "جستجو",
),
),
],
),
);
}
}
I hade this problem in other cases, I think it's any problem with the Row and Column.
Try this way
You need to wrap your TextField inside Expanded widget
class SearchWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.yellowAccent,
borderRadius: BorderRadius.circular(500),
),
child: Row(
children: [
Container(
decoration: BoxDecoration(
color: Colors.redAccent,
borderRadius: BorderRadius.circular(500),
),
child: IconButton(icon: Icon(Icons.search), onPressed: () {}),
),
Expanded(
child: TextField(
decoration: InputDecoration(
labelText: "جستجو",
),
),
),
],
),
);
}
}
I think you don't even need the Container just to create a circular rectangle text field because the textField has enableBorder, disableBorder, and border as its properties to make its border circular and you can suffix/prefix IconButton for the search implementation, but if you want to implement it the with the container I have a similar code:
Row(
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(24)),
child: TextFormField(
autovalidate: true,
autocorrect: true,
textInputAction: TextInputAction.search,
onFieldSubmitted: (val) {
_search();
},
onChanged: (val) {
// Your Code
},
controller: _controller,
decoration: InputDecoration(
hintText: 'Search',
contentPadding: const EdgeInsets.only(left: 24.0),
border: InputBorder.none),
),
),
),
IconButton(
icon: Icon(
Icons.search,
color: Colors.white,
),
onPressed: () {
_search();
})
],
),
Let me know if you need the TextField circular border code I'll be happy to help you