Hidden list with searcher - flutter - flutter

The searcher bar is not working, where did i get an error?
I get this error in the terminal when I added expanded:
RenderFlex children have non-zero flex but incoming height constraints are unbounded.
I would like the hints from lost when searching to appear.Is everything logically arranged? I am new in flutter and not everything is clear to me.
Widget _profilePage(BuildContext context) {
(context, state) {
return SafeArea(
child: Align(
alignment: const Alignment(0, -1 / 1.6),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
_buildSearchBar(),
],
),
),
),
);
}
List<String> newAllBreed = List.from(allBreed);
static List<String> allBreed =
["Affenpinscher",
"Afghan Hound",
"Aidi",
"Airedale Terrier",
"Akbash Dog",
"Akita"];
void search(String value) {
setState(() {
newAllBreed = allBreed
.where((string) =>
string.toLowerCase().contains(value.toLowerCase()))
.toList();
});
}
Widget _buildSearchBar() {
return Column(children: [
ConstrainedBox(
constraints: const BoxConstraints.tightFor(width: 350),
child: TextFormField(
controller: controller,
onFieldSubmitted: search,
decoration: InputDecoration(
labelText: 'Search',
hintText: 'Search',
fillColor: Colors.white,
filled: true,
isDense: true,
suffixIcon: GestureDetector(
onTap: () {
controller.clear();
},
child: const Padding(
padding: EdgeInsets.symmetric(
horizontal: 10,
vertical: 10,
),
child: Icon(
Icons.clear,
size: 30,
),
),
),
prefixIcon: GestureDetector(
onTap: () {
search(controller.text);
},
/// widget isntead of normal button
// cancellationWidget: Text("Cancel"),
child: const Padding(
padding: EdgeInsets.symmetric(
horizontal: 10,
vertical: 10,
),
child: Icon(
Icons.search_outlined,
size: 30,
),
),
),
focusedBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(30),
),
borderSide: BorderSide(
width: 2,
color: Colors.teal,
),
),
disabledBorder: InputBorder.none,
border: InputBorder.none,
enabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(30),
),
borderSide: BorderSide(
width: 2,
color: Colors.grey,
),
),
errorBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(5),
),
borderSide: BorderSide(
width: 2,
color: Colors.red,
),
),
),
),
),
Expanded(
child: ListView(
padding: EdgeInsets.all(12.0),
children: newAllBreed.map((data) {
return ListTile(
title: Text(data),
onTap: ()=> print(data),);
}).toList(),
),
)
]);
}

Flutter is cool and you don't need to nest the Widgets unnecessarily. Since you are designing a typical search with list you can use Column widget instead of SingleChildScrollView. Simplified approach will be as below.
Column(children: [
TextFormField( .. ), // => Your search text box
Expanded(child: ListView( .. )) // => Search result
])
Full Snippet : For your understanding
Column(children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: controller,
onFieldSubmitted: search,
decoration: InputDecoration(
labelText: 'Search',
hintText: 'Search',
fillColor: Colors.white,
filled: true,
isDense: true,
suffixIcon: GestureDetector(
onTap: () {
controller.clear();
},
child: const Padding(
padding: EdgeInsets.symmetric(
horizontal: 10,
vertical: 10,
),
child: Icon(
Icons.clear,
size: 30,
),
),
),
prefixIcon: GestureDetector(
onTap: () {
search(controller.text);
},
/// widget isntead of normal button
// cancellationWidget: Text("Cancel"),
child: const Padding(
padding: EdgeInsets.symmetric(
horizontal: 10,
vertical: 10,
),
child: Icon(
Icons.search_outlined,
size: 30,
),
),
),
focusedBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(30),
),
borderSide: BorderSide(
width: 2,
color: Colors.teal,
),
),
disabledBorder: InputBorder.none,
border: InputBorder.none,
enabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(30),
),
borderSide: BorderSide(
width: 2,
color: Colors.grey,
),
),
errorBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(5),
),
borderSide: BorderSide(
width: 2,
color: Colors.red,
),
),
),
),
),
Expanded(
child: ListView(
padding: EdgeInsets.all(12.0),
semanticChildCount: newAllBreed.length,
children: newAllBreed.map((data) {
return ListTile(
title: Text(data),
onTap: () => print(data),);
}).toList(),
),
)
]);

Related

How to add dropdownMenu for Phone Number

I want to add dropdownMenu like picture below:
But I still do not know how to do it.
Here is my code:
SizedBox(
width: 400,
height: 60,
child: TextField(
style: body1(color: ColorName.neutralTextPrimary),
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: ColorName.neutralTextSecondary,
),
borderRadius: BorderRadius.circular(10),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(
color: ColorName.neutralTextSecondary,
),
),
hintText: 'Phone Number',
hintStyle: body1(color: ColorName.neutralTextSecondary),
),
),
),
use this package
it's customizable you can add your own decoration
https://pub.dev/packages/intl_phone_field
You can use row inside a container with DropdownMenu
static const List<String> codes = ['+62', '+82', '+1'];
Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12), border: Border.all()),
child: Row(
children: [
Padding(
padding: const EdgeInsets.all(4),
child: DropdownButton(
icon: const Icon(
Icons.expand_more,
),
value: _selectedIndex,
items: codes
.map((e) => DropdownMenuItem(
value: codes.indexOf(e),
child: Text(e),
))
.toList(),
onChanged: (v) {
_selectedIndex = v ?? 0;
setState(() {});
}),
),
const Padding(
padding: EdgeInsets.all(2),
child: Divider(
color: Colors.black,
thickness: 2,
),
),
Expanded(
child: TextFormField(
textAlign: TextAlign.center,
))
],
),
),
),
),

Keyboard appears when click on help icon inside textfield

How do I stop the keyboard from appearing if the user clicks on this help icon... Here is a screenshot
Any type of help will greatly appreciated
Here is my code
TextFormField(
obscureText: false,
controller: urlController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter instagram url';
}
return null;
},
decoration: InputDecoration(
focusedBorder:OutlineInputBorder(
borderSide: const BorderSide(color: Colors.green, width: 1.0),
borderRadius: BorderRadius.circular(8.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8)),
borderSide: BorderSide(width: 1,color: Colors.green),
),
filled: true,
fillColor: Colors.white,
border: InputBorder.none,
labelText: 'Paste Your URL',
suffixIcon: IconButton(
onPressed: (){},
icon: Icon(Icons.help),
iconSize: 30.0,
color: Colors.grey[400],
),
),
)
Use suffix property instead of suffixIcon and if it still doesn't work on its own, wrap it with a IgnorePointer widget.
However, I assume that that ? button will have a onTap function, if so, it should then it shouldn't be a problem.
suffix: IgnorePointer(
child:IgnorePointer(
child: IconButton(
onPressed: () {},
icon: Icon(Icons.help),
iconSize: 30.0,
color: Colors.grey[400],
),
);
),
suffixIcon is now part of the TextField so we cannot do it using focus node as per changes made by flutter team.
However you can create your layout with Stack widget.
Container(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Stack(alignment: Alignment.centerRight, children: [
TextFormField(
focusNode: focusNode,
obscureText: false,
controller: urlController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter instagram url';
}
return null;
},
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide:
const BorderSide(color: Colors.green, width: 1.0),
borderRadius: BorderRadius.circular(8.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8)),
borderSide: BorderSide(width: 1, color: Colors.green),
),
filled: true,
fillColor: Colors.white,
border: InputBorder.none,
labelText: 'Paste Your URL',
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(
Icons.help,
size: 30.0,
color: Colors.grey,
),
),
])),
Or you can use use CupertinoTextField
try this:
suffixIcon: IconButton(
onPressed: (){
FocusScope.of(context).unfocus();
//or FocusScope.of(context).requestFocus(new FocusNode());
},
icon: Icon(Icons.help),
iconSize: 30.0,
color: Colors.grey[400],
),
[EDITED]
or simple use Stack to avoid touching Textfield when touching icon:
Stack(
children: [
TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.only(right: 20.0),
),
),
Positioned(
right: 0,
child: Container(
width: 20,
height: 20,
child: IconButton(onPressed:(){}, icon: Icon(Icons.add),),
),
),
],
),
stack(
children: [
TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.only(right: 20.0),
),
),
Positioned(
right: 0,
child: Container(
width: 20,
height: 20,
child: IconButton(onPressed:(){}, icon: Icon(Icons.add),),
),
),
],
),

There are multiple heroes that share the same tag within a subtree error without hero or FloatingActionButton widgets in code

I'm Working on app, and i'm navigating through pages using routes, i'm not using any hero or FloatingActionButtons but i'm getting this hero related error, does anybody has any idea what could be the cause of that?
Here's the navigation call
nextTapped()async{
if(occasionTitleTxtField.text.isEmpty || dateTxtField.text.isEmpty || detailsTxtField.text.isEmpty)
ErrorOverlay("Invalid Data", false);
else
{
await LoadingOverlay.of(context).during(UserOperations().createFirstOccasion(context, occasionTitleTxtField.text, dateTxtField.text, detailsTxtField.text, repeatEveryYear, timeLimit)).then((value){
if(value.elementAt(0) == "true")
Navigator.of(context).pushReplacementNamed('/firstGift',arguments: FirstGiftPage(value.elementAt(1),occasionTitleTxtField.text));
});
}
}
Here's the route_generator
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) {
// Getting arguments passed in while calling Navigator.pushNamed
final args = settings.arguments;
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => SplashScreen());
case '/login':
return MaterialPageRoute(builder: (_) => LoginPage());
case '/homepage':
return MaterialPageRoute(builder: (_) => HomePage());
case '/signUp':
return MaterialPageRoute(builder: (_) => SignUpScreen());
case '/otpScreen':
return MaterialPageRoute(builder: (_) {
OTPScreen screenArgs = args;
return OTPScreen(screenArgs.phoneNumber,screenArgs.countryCode);
});
case '/phoneNumber':
return MaterialPageRoute(builder: (_) {
PhoneNumberScreen screenArgs = args;
return PhoneNumberScreen(screenArgs.Provider);
});
case '/interests':
return MaterialPageRoute(builder: (_) => InterestsPage());
case '/invitation':
return MaterialPageRoute(builder: (_) => InvitationPage());
case '/firstOccasion':
return MaterialPageRoute(builder: (_) => FirstOccasion());
case '/firstGift':
return MaterialPageRoute(builder: (_) {
FirstGiftPage screenArgs = args;
return FirstGiftPage(screenArgs.occasionID,screenArgs.occasionTitle);
});
case '/map':
return MaterialPageRoute(builder: (_) => MapsPage());
default:
return _errorRoute();
}
}
static Route<dynamic> _errorRoute() {
return MaterialPageRoute(builder: (_) {
return Scaffold(
appBar: AppBar(
title: Text('Error'),
),
body: Center(
child: Text('UnderConstruction'),
),
);
});
}
}
Page i want to navigate to
class FirstGiftPage extends StatefulWidget {
String occasionID;
String occasionTitle;
static LatLng storeLocation;
FirstGiftPage(this.occasionID,this.occasionTitle);
#override
_FirstGiftPageState createState() => _FirstGiftPageState();
}
class _FirstGiftPageState extends State<FirstGiftPage> {
TextEditingController giftTitleTxtField = TextEditingController();
TextEditingController giftLinkTxtField = TextEditingController();
TextEditingController giftPriceTxtField = TextEditingController();
TextEditingController giftStoreAddTxtField = TextEditingController();
int giftCounter = 0;
TextEditingController detailsTxtField = TextEditingController();
Future<File> imageFile;
nextTapped()async{
if(giftTitleTxtField.text.isEmpty || giftLinkTxtField.text.isEmpty || giftPriceTxtField.text.isEmpty || detailsTxtField.text.isEmpty)
ErrorOverlay("Invalid Data",false);
else
{
Map<String,dynamic> giftData={
"giftName" : giftTitleTxtField.text,
"giftLink" : giftLinkTxtField.text,
"giftPrice" : giftPriceTxtField.text,
"storeAddress": giftStoreAddTxtField.text,
"details" : detailsTxtField.text,
"giftQuantity" : giftCounter,
"images":[imageFile.toString()],
"occasionID": widget.occasionID
};
ErrorOverlay("Gift Done",true);
Navigator.of(context).pushReplacementNamed("/homepage");
}
}
showMaps(){
Navigator.of(context).pushNamed("/map");
}
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Color.fromRGBO(246, 76, 77, 1),
Color.fromRGBO(156, 51, 117, 1)
],
begin: Alignment .topRight,
end: Alignment.bottomLeft
)
),
child: Scaffold(
backgroundColor: Colors.transparent,
appBar:AppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.transparent,
elevation: 0,
bottom: PreferredSize(
preferredSize: Size(0.0, 80.0),
child: Padding(
//TODO::Change padding to ScreenWidth - 300 / 2
padding: const EdgeInsets.only(left: 55.0 ,right: 55.0,bottom: 30.0),
child: Text(AppLocalizations.of(context).firstGift,style: TextStyle(color: Colors.white,fontSize: 30.0),textAlign: TextAlign.center,maxLines: 2,),
),
),
),
body: Padding(
padding: const EdgeInsets.only(top: 20.0,left: 15.0,right: 15.0),
child: Align(
alignment: Alignment.topCenter,
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: giftTitleTxtField,
obscureText: false,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
contentPadding:EdgeInsets.only(top:20.0,bottom:20.0,left: 15.0),
hintText: AppLocalizations.of(context).giftName,
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1
),
borderRadius: BorderRadius.circular(30.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1
),
borderRadius: BorderRadius.circular(30.0),
),
prefix: Container(width: 10.0,),
hintStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
)
),),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: giftLinkTxtField,
obscureText: false,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
contentPadding:EdgeInsets.only(top:20.0,bottom:20.0,left: 15.0),
hintText: AppLocalizations.of(context).giftLink,
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1
),
borderRadius: BorderRadius.circular(30.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1
),
borderRadius: BorderRadius.circular(30.0),
),
prefix: Container(width: 10.0,),
hintStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
)
),),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: giftPriceTxtField,
obscureText: false,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
contentPadding:EdgeInsets.only(top:20.0,bottom:20.0,left: 15.0),
hintText: AppLocalizations.of(context).giftPrice,
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1
),
borderRadius: BorderRadius.circular(30.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1
),
borderRadius: BorderRadius.circular(30.0),
),
prefix: Container(width: 10.0,),
hintStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
)
),),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
readOnly: true,
onTap: showMaps,
controller: giftStoreAddTxtField,
obscureText: false,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
contentPadding:EdgeInsets.only(top:20.0,bottom:20.0,left: 15.0),
hintText: AppLocalizations.of(context).giftStoreAdd,
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1
),
borderRadius: BorderRadius.circular(30.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1
),
borderRadius: BorderRadius.circular(30.0),
),
hintStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
)
),),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(AppLocalizations.of(context).giftQuantity,style: TextStyle(color: Colors.white),),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Counter(
initialValue: giftCounter,
minValue: 0,
maxValue: 10,
step: 1,
decimalPlaces: 0,
textStyle: TextStyle(color: Colors.white),
onChanged: (value) { // get the latest value from here
setState(() {
giftCounter = value;
});
},
),
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
enabled: false,
obscureText: false,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
contentPadding: EdgeInsets.only(top: 20),
hintText: widget.occasionTitle,
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1
),
borderRadius: BorderRadius.circular(20.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1
),
borderRadius: BorderRadius.circular(20.0),
),
prefix: Container(width: 10.0,),
hintStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
)
),),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
maxLines: null,
textAlign: TextAlign.center,
textAlignVertical: TextAlignVertical.center,
controller: detailsTxtField,
obscureText: false,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
contentPadding: EdgeInsets.only(top: 20),
hintText: AppLocalizations.of(context).details,
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1
),
borderRadius: BorderRadius.circular(20.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
width: 1
),
borderRadius: BorderRadius.circular(20.0),
),
prefix: Container(width: 10.0,),
hintStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
height: 4.0
)
),),
),
InkWell(
onTap: null,
child: Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 50.0,
height: 50.0,
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.white,width: 4.0),
borderRadius: BorderRadius.circular(2.0)
),
child: Center(
child: Icon(Icons.camera_alt_outlined,color: Colors.white,size: 25.0,),
),
),
),
),
)
],
),
),
),
bottomNavigationBar: BottomAppBar(
child: Container(
child: Wrap(
//mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Align(
alignment: MyApp.getLocale(context).languageCode == "ar" ? Alignment.centerLeft:Alignment.centerRight,
child: Text("(4/5)"),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: LinearProgressIndicator(
minHeight: 10.0,
backgroundColor: Colors.white,
value: 0.80,
valueColor: AlwaysStoppedAnimation<Color>(Colors.orange),
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: (){
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (BuildContext context)=> FirstGiftPage("","")));
},
child: Text(AppLocalizations.of(context).ignore,style: TextStyle(decoration: TextDecoration.underline),)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: nextTapped,
child: Container(
width: 80.0,
padding: EdgeInsets.all(8.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
gradient: LinearGradient(
colors: [
Color.fromRGBO(246, 76, 77, 1),
Color.fromRGBO(156, 51, 117, 1)
],
begin: Alignment .topRight,
end: Alignment.bottomLeft
)
),
child: Center(child: Text(AppLocalizations.of(context).next, style: TextStyle(color: Colors.white),)),
),
),
)
],
)
],
),
),
),
),
);
}
}
Here's the error
════════ Exception caught by scheduler library ═════════════════════════════════════════════════════
The following assertion was thrown during a scheduler callback:
There are multiple heroes that share the same tag within a subtree.
Within each subtree for which heroes are to be animated (i.e. a PageRoute subtree), each Hero must have a >unique non-null tag.
In this case, multiple heroes had the following tag:
Here is the subtree for one of the offending heroes: Hero
tag:
state: _HeroState#48fb7
When the exception was thrown, this was the stack:
#0 Hero._allHeroesFor.inviteHero. >(package:flutter/src/widgets/heroes.dart:268:11)
#1 Hero._allHeroesFor.inviteHero (package:flutter/src/widgets/heroes.dart:279:8)
#2 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:298:21)
#3 SingleChildRenderObjectElement.visitChildren >(package:flutter/src/widgets/framework.dart:6105:14)
#4 Hero._allHeroesFor.visitor (package:flutter/src/widgets/heroes.dart:311:15)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
If you are using more than one floating action button then you need to pass your own tag to each button like this
FloatingActionButton(
heroTag: "tag1",
)
FloatingActionButton(
heroTag: "tag2",
)
By default, the heroTag is tag so in this case, you will get an error.
Second thing
If you are using more than one hero animation widget on the same page and share the same tag then also same error exist so you need to pass your own tag to each Hero widget
I solved this by creating a new file for the page i want to navigate to and it worked, have no idea how

Trying to get a Flutter/Dart DateTime to appear in a dropdown menu

I am trying to get the DateTime to appear in a flutter/Dart project and it is not working. For my other selections I am using a TextFormField but when I try to put the dropdown in a box decoration it gives me an error. I am new to Flutter/Dart so I am wondering what I am doing wrong and is there a better more efficient way to write it?
//some other code
...
TextFormField(
decoration: InputDecoration(
labelText: 'Course Name',
contentPadding:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.blueAccent, width: 1.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.blueAccent, width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
),
),
new SizedBox(
height: 20.0,
),
new Row(children: <Widget>[
Container(
padding: EdgeInsets.symmetric(horizontal: 20.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(32.0)),
border: Border.all(
color: Colors.blueAccent,
style: BorderStyle.solid,
width: 0.80),
),
child: new Expanded(
child: new TextFormField(
decoration: new InputDecoration(
hintText: 'Enter your date of birth',
labelText: 'Date of Birth',
),
controller: _controller,
keyboardType: TextInputType.datetime,
)),
),
new IconButton(
icon: Icon(Icons.keyboard_arrow_down),
tooltip: 'Choose date',
onPressed: (() {
_chooseDate(context, _controller.text);
}),
)
]),
]),
),
),
);
}
}
Error Message:
The following assertion was thrown building Container(padding:
EdgeInsets(20.0, 0.0, 20.0, 0.0), bg: BoxDecoration(border:
Border.all(BorderSide(MaterialAccentColor(primary value:
Color(0xff448aff)), 0.8, BorderStyle.solid)), borderRadius:
BorderRadius.circular(32.0))): Incorrect use of ParentDataWidget.
use showDatePicker
DateTime selectedDate = DateTime.now();
Future<Null> _selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: selectedDate,
firstDate: DateTime(2015, 8),
lastDate: DateTime(2101));
if (picked != null && picked != selectedDate)
setState(() {
selectedDate = picked;
});
}
you can check full sourcecode in the below dartpad link
https://dartpad.dev/e5a99a851ae747e517b75ac221b73529
I was able to figure it out by using a plugin (flutter_datetime_picker 1.3.4). I altered it a bit to fit my needs.
TextFormField(
decoration: InputDecoration(
labelText: 'Course Name',
contentPadding:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.blueAccent, width: 1.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.blueAccent, width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
),
),
new SizedBox(
height: 20.0,
),
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(32.0),
side: BorderSide(color: Colors.blueAccent)),
onPressed: () {
DatePicker.showDatePicker(context,
theme: DatePickerTheme(
containerHeight: 210.0,
),
showTitleActions: true,
minTime: DateTime(1940, 1, 1),
maxTime: DateTime.now(), onConfirm: (date) {
print('confirm $date');
_date = '${date.month} / ${date.day} / ${date.year}';
//_date = '${date.year} - ${date.month} - ${date.day}';
setState(() {});
}, currentTime: DateTime.now(), locale: LocaleType.en);
},
child: Container(
alignment: Alignment.center,
height: 50.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Container(
child: Row(
children: <Widget>[
Text(
" $_date",
style: TextStyle(
color: Colors.black, fontSize: 16.0),
),
],
),
)
],
),
Text(
" Change",
style:
TextStyle(color: Colors.blueAccent, fontSize: 16.0),
),
],
),
),
color: Colors.white,
),
SizedBox(
height: 20.0,
),
],
),
),
),
);
}
}

How to add following decoration to TextFormField in flutter?

Following is my code where I am trying to add form TextFormField with decoration as seen in mock:
Rounded border
Background colour to grey
First email and then password with text not visible
Where password field will have show button to make password visible
finally a rounded submit button
MOCK:
CODE:
class MyCustomFormState extends State<MyCustomForm> {
final _formKey = GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
TextFormField(
decoration: InputDecoration(
fillColor: Colors.grey,
focusColor: Colors.grey
),
validator: (value) {
if (value.isEmpty) {
return 'Your email';
}
return null;
},
),
TextFormField(
decoration: InputDecoration(
fillColor: Colors.grey,
focusColor: Colors.grey
),
validator: (value) {
if (value.isEmpty) {
return 'Your password';
}
return null;
},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: RaisedButton(
onPressed: () {
// Validate returns true if the form is valid, or false
// otherwise.
if (_formKey.currentState.validate()) {
// If the form is valid, display a Snackbar.
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Processing Data')));
}
},
child: Text('Submit'),
),
),
],
),
);
}
}
EDIT:
How to change color of this label ?
You can use borderRadius in OutlineInputBorder to make it roundable.
#override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Scaffold(
appBar: AppBar(
title: Text('Testing'),
),
body: Form(
child: Column(
key: _formKey,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.all(10),
child: Container(
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: new BorderRadius.circular(10.0),
),
child: Padding(
padding: EdgeInsets.only(left: 15, right: 15, top: 5),
child: TextFormField(
decoration: InputDecoration(
border: InputBorder.none,
labelText: 'Email',
))))),
Padding(
padding: EdgeInsets.all(10),
child: Stack(
alignment: const Alignment(0, 0),
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: new BorderRadius.circular(10.0),
),
child: Padding(
padding:
EdgeInsets.only(left: 15, right: 15, top: 5),
child: TextFormField(
obscureText: true,
decoration: InputDecoration(
border: InputBorder.none,
labelText: 'Your password',
)))),
Positioned(
right: 15,
child: RaisedButton(
onPressed: () {
// _controller.clear();
},
child: Text('SHOW')))
],
),
),
Padding(
padding: const EdgeInsets.all(10),
child: Container(
height: 50,
width: double.infinity,
child: RaisedButton(
color: Colors.green,
onPressed: () {
// Validate returns true if the form is valid, or false
// otherwise.
if (_formKey.currentState.validate()) {
// If the form is valid, display a Snackbar.
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('Processing Data')));
}
},
child: Text(
'Submit',
style: TextStyle(color: Colors.white),
),
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.green)),
),
)),
],
),
));
}
Output
Edit
You can change the border color when it is clicked
#override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Scaffold(
appBar: AppBar(
title: Text('Testing'),
),
body: Form(
child: Column(
key: _formKey,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.all(10),
child: TextField(
autofocus: false,
style: TextStyle(fontSize: 15.0, color: Colors.black),
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),
),
),
),
),
Padding(
padding: EdgeInsets.all(10),
child: Stack(
alignment: const Alignment(0, 0),
children: <Widget>[
TextField(
obscureText: true,
autofocus: false,
style: TextStyle(fontSize: 15.0, color: Colors.black),
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),
),
),
),
Positioned(
right: 15,
child: Container(
width: 65,
height: 30,
child: RaisedButton(
onPressed: () {
// _controller.clear();
},
child: Text(
'SHOW',
style: TextStyle(fontSize: 8),
))))
],
),
),
Padding(
padding: const EdgeInsets.all(10),
child: Container(
height: 50,
width: double.infinity,
child: RaisedButton(
color: Colors.green,
onPressed: () {
// Validate returns true if the form is valid, or false
// otherwise.
if (_formKey.currentState.validate()) {
// If the form is valid, display a Snackbar.
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('Processing Data')));
}
},
child: Text(
'Submit',
style: TextStyle(color: Colors.white),
),
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.green)),
),
)),
],
),
));
}
Output
Container(
padding:EdgeInsets.only(top:20,right:10,left:10),
child:Card(
shape:RoundedRectangleBorder(
borderRadius:BorderRadius.circular(20),
),
color:Colors.grey,
child: Container(
padding:EdgeInsets.only(left:12),
child: TextFormField(
decoration:InputDecoration(
hintText:"You phone number here...",
border:InputBorder.none,
fillColor:Colors.white,
),
),
),
),
),