TextField inside of Row it broke the code - flutter

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

Related

Hooks can only be called from the build method of a widget that mix-in `Hooks`

I want to use modal and I want to use MultilineTextField but when I used it, dart thrown error.
Do you know how to solve this problem?
Hooks can only be called from the build method of a widget that mix-in Hooks.
Future modalWithReason(BuildContext context) {
return showModalBottomSheet(
backgroundColor: Colors.transparent,
isScrollControlled: true,
context: context,
builder: (BuildContext context) {
return GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Container(
height: 789.sp,
padding: EdgeInsets.symmetric(horizontal: 16.sp),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Column(
children: [
Container(
height: 264.h,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 34.h),
Text(
'理由を記述',
style: bodySemiBold(black),
),
SizedBox(height: 20.h),
MultilineTextField(),
],
),
),
],
),
),
),
],
),
),
);
});
}
I want to use this.
class MultilineTextField extends HookWidget {
MultilineTextFieldForReason({super.key});
final myController = useTextEditingController();
#override
Widget build(BuildContext context) {
return TextField(
keyboardType: TextInputType.multiline,
controller: myController,
maxLines: 4,
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide(
width: 1,
color: borderMidEmphasis,
),
)),
);
}
}

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 do i center a text field horizontaly and vertically in flutter

Hi i am new to flutter i need to align the text field horizontally and vertically for more elaboration see this
What i am getting:
Text aligned at the very top it needs to be in the center
What i really want:
the text field is aligned both horizontaly and vertically that is exactly what i am looing for
Here is the code:
import 'package:multi_purpose_scope/Animation/FadeAnimation.dart';
import 'package:flutter/material.dart';
void main() => runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
)
);
class HomePage extends StatelessWidget {
gotoSecondActivity(BuildContext context){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondActivity()),
);
}
#override
Widget build(BuildContext context){
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: RaisedButton(
onPressed: () {
gotoSecondActivity(context);
},
child: Container(
child: Column(
children: <Widget>[
Container(
height: 400,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/loginHeader.png'),
fit: BoxFit.fill
)
),
child: Stack(
children: <Widget>[
],
),
),
Padding(
padding: EdgeInsets.all(30.0),
child: Column(
children: <Widget>[
FadeAnimation(1.8, Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color: Color.fromRGBO(143, 148, 251, .2),
blurRadius: 20.0,
offset: Offset(0, 10)
)
]
),
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border(bottom: BorderSide(
color: Colors.grey[100]))
),
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Email or Phone number",
hintStyle: TextStyle(
color: Colors.grey[400])
),
),
),
Container(
padding: EdgeInsets.all(8.0),
child: TextField(
obscureText: true,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Password",
hintStyle: TextStyle(
color: Colors.grey[400])
),
),
)
],
),
)),
SizedBox(height: 30,),
FadeAnimation(2, Container(
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
colors: [
Color.fromRGBO(214, 0, 27, 1),
Color.fromRGBO(214, 0, 27, 1),
]
)
),
child: Center(
child: Text("Login", style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),),
),
)),
SizedBox(height: 70,),
FadeAnimation(1.5, Text("Forgot Password?",
style: TextStyle(
color: Color.fromRGBO(214, 0, 27, 1)),)),
],
),
)
],
),
),
)
),
);
}
}
class SecondActivity extends StatelessWidget {
gotoRegister(BuildContext context){
Navigator.push(
context,
MaterialPageRoute(builder: (context) =>Register()),
);
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child: Container(
height: 174,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/patient_list.png'),
)
),
child: Stack(
children: <Widget>[
Container(
alignment: Alignment.center,
height: 128,
child: Text(
'Patient List',
style: TextStyle(
fontWeight: FontWeight.bold,fontSize: 30,
color: Colors.white
),
),
)
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
gotoRegister(context);
},
tooltip: 'Increment',
child: Icon(Icons.add),
backgroundColor: Color.fromRGBO(214, 0, 27,1),
),
),
);
}
}
class Register extends StatelessWidget {
goBack(BuildContext context) {
Navigator.pop(context);
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child:Container(
height: 174,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/patient_list.png'),
)
),
child: Stack(
children: <Widget>[
Container(
alignment: Alignment.center,
height: 128,
child: Text(
'Registration
style: TextStyle(
fontWeight: FontWeight.bold,fontSize: 30,
color: Colors.white
),
)
),
Container(
padding: EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border(bottom: BorderSide(
color: Colors.grey[100]))
),
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Email or Phone number",
hintStyle: TextStyle(
color: Colors.grey[400])
),
),
),
],
),
),
)
),
);
}
}
I searched for this but cant seem to find the solution to my problem.
I have almost tried all the solutions that i can think of but those don't work
Just set the textAlign property to your TextField and also add a border in your decoration.
Sample:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Column(
children: <Widget>[
const SampleTextField(hintText: 'Enter Name'),
const SizedBox(height: 10),
const SampleTextField(hintText: 'Enter MR-Number'),
const SizedBox(height: 10),
const SampleTextField(hintText: 'Enter Phone Number'),
const SizedBox(height: 10),
const SampleTextField(hintText: 'Enter Hospital Name'),
const SizedBox(height: 10),
FlatButton(
onPressed: () {},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
// materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
// visualDensity: VisualDensity.compact,
padding: const EdgeInsets.symmetric(vertical: 20),
color: Colors.red,
child: const Center(
child: Text('Registration'),
),
),
],
),
),
),
),
);
}
}
class SampleTextField extends StatelessWidget {
const SampleTextField({
this.controller,
this.hintText = '',
});
final TextEditingController controller;
final String hintText;
#override
Widget build(BuildContext context) {
return TextField(
controller: controller,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(
width: 1,
color: Colors.black54,
),
),
hintText: hintText,
hintStyle: const TextStyle(
color: Colors.black54,
),
contentPadding: const EdgeInsets.only(left: 20),
),
// textAlign: TextAlign.center, // Align vertically and horizontally
);
}
}
You are using a stack widget so if you want to align and positioned your widget inside the stack see the below options
Wrap widget with Positioned widget if you want to change position
Wrap widget with Align widget for alignment
Check out this Video for how to use Positioned widget
Check out this Video for how to use Align widget

Cannot apply decoration to Container in 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'),
],
),
),

I want to make a button like this but not getting the desired output

I want to make a text form field and button inside a container and the click of a button text in the form should be copied to clipboard. How can I achieve this?
My Requirement is like this
Container(
height: 65.0,
width: 270.0,
child: Row(
children: <Widget>[
Expanded(
child: TextField(
decoration: InputDecoration(
hintText: "https://",
border: OutlineInputBorder(
borderRadius:
BorderRadius
.circular(10.0),
borderSide:
BorderSide()))),
),
Container(
child: FlatButton(
onPressed: () {},
child: Text("Copy Link")),
)
],
),
)
You have two things to learn to achieve, what you want, these are:
Clipboard class => Which does the clipboard copy. Also, do import this import 'package:flutter/services.dart'; in your file to use this Class
suffixIcon in InputDecoration, which will add the item to the end inside your TextField()
FINAL SOLUTION:
// mandatory for Clipboard class
import `'package:flutter/services.dart';
class _MyHomePageState extends State<MyHomePage> {
final key = new GlobalKey<ScaffoldState>();
final TextEditingController _controller = new TextEditingController();
#override
Widget build(BuildContext context){
return Scaffold(
key: key,
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Container(
height: 65.0,
width: 270.0,
child: TextField(
controller: _controller,
decoration: InputDecoration(
hintText: "https://",
suffixIcon: FlatButton(
onPressed: () {
// Here what you have to do the operation using Clipboard
Clipboard.setData(new ClipboardData(text: _controller.text.toString()));
key.currentState.showSnackBar(
new SnackBar(content: new Text("Copied to Clipboard")));
},
child: Text("Copy Link")
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide()
)
)
)
)
),
);
}
}
RESULT:
You need to add the border to the Container and not the Textfield
Container(
height: 50.0,
width: 270.0,
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey,
),
borderRadius: BorderRadius.circular(10.0),
),
child: Row(
children: <Widget>[
Expanded(
child: TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.only(left: 10.0),
hintText: "https://",
border: InputBorder.none,
),
),
),
Container(
height: 50.0,
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey,
),
borderRadius: BorderRadius.circular(6.0),
),
child: FlatButton(onPressed: () {}, child: Text("Copy Link")),
),
],
),
)
Try this:
Container(
height: 65.0,
width: 270.0,
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(20.0)),
child: Row(
children: <Widget>[
Expanded(
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: "https://")),
),
Container(
child: FlatButton(
onPressed: () {},
child: Text("Copy Link")),
)
],
),
)
);
this will give