The widget is from the package 'flutter_platform_widgets'
As you can see, the floating icon that shows input location doesn't go behind app bar when screen is scrolled down. I need to either get rid of it completely or at least have it disappear properly. I tried dismissing the keyboard on screen drag, but it gets a bit annoying as all the content fits on one screen, it isn't actually scrollable (scrollview necessary because there is content behind the keyboard and I was getting pixel overflow errors without it).
Some code if that helps
EDIT:
This is the widget seen on the first two screenshots.
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 16.0),
child: Form(
key: widget.formKey,
child: SingleChildScrollView(
// keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
child: Column(
children: [
EmailTextFormField(
controller: widget.emailController,
),
PasswordTextFormField(
labelText: AppLocalizations.of(context)!.password,
controller: widget.passwordController,
),
PasswordTextFormField(
labelText: AppLocalizations.of(context)!.confirmPassword,
controller: widget.passwordConfirmationController,
password: widget.passwordController,
),
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Button(
text: AppLocalizations.of(context)!.next,
onPressed: () {
FocusScope.of(context).unfocus();
_validateLoginInformation();
},
),
),
,
SizedBox(
height: MediaQuery.of(context).size.height / 2.75,
),
const TermsOfUse(),
],
),
),
),
);
}
This is the emailtextformfield. The code for passwordtextformfield is nearly identical.
#override
Widget build(BuildContext context) {
return Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 16.0),
child: Icon(
Icons.mail_outline,
color: iconColor,
),
),
Expanded(
child: AutofillGroup(
child: PlatformTextFormField(
hintText: AppLocalizations.of(context)!.email,
controller: widget.controller,
focusNode: _focusNode,
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
autofillHints: const [AutofillHints.email],
onFieldSubmitted: (_) => _focusNode.nextFocus(),
validator: (email) {
if (email == null || email.isEmpty) {
return AppLocalizations.of(context)!.emailIsNull;
} else if (!EmailValidator.validate(email)) {
return AppLocalizations.of(context)!.emailInvalid;
} else {
return null;
}
},
material: (_, __) => MaterialTextFormFieldData(
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.auto,
labelText: AppLocalizations.of(context)!.email,
suffixIcon: widget.controller.text.isNotEmpty
? ClearTextFieldIcon(controller: widget.controller)
: null,
),
),
cupertino: (_, __) => CupertinoTextFormFieldData(
textInputAction: TextInputAction.next,
focusNode: _focusNode,
decoration: BoxDecoration(
color: CupertinoColors.extraLightBackgroundGray,
borderRadius: BorderRadius.circular(10.0),
),
),
),
),
),
],
);
}
I think it is not possible to remove it. But you can make it invisible by setting its color to transparent.
You can set the color globally:
textSelectionTheme: const TextSelectionThemeData(
selectionHandleColor: Colors.transparent,
),
main.dart
#override
Widget build(BuildContext context) => MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
textSelectionTheme: const TextSelectionThemeData(
selectionHandleColor: Colors.transparent,
),
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
I also tried to set it locally using the Theme widget, but it seems buggy.
You can also access the complete source code through GitHub.
If you have further questions, please don't hesitate to write in the comments.
Related
I need 80 pixel top padding (so AppBar to be shown) for my bottom sheet when keyboard is visible and when keyboard is not visible.
This is my code:
import 'package:flutter/material.dart';
class Temp2Screen extends StatelessWidget {
const Temp2Screen({super.key});
static const String _title = 'Flutter Code Sample';
#override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: MyWidget(),
),
);
}
}
class MyWidget extends StatefulWidget {
MyWidget({Key? key}) : super(key: key);
#override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
late ScrollController scrollController;
List<String> messages = [
"msg1", "msg2", "msg3", "msg4", "msg5", "msg6", "msg7", "msg8", "msg9", "msg10",
];
#override
void initState() {
super.initState();
scrollController = new ScrollController();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
showModalBottomSheet(
shape: const RoundedRectangleBorder(
borderRadius:
BorderRadius.vertical(top: Radius.circular(25.0))),
backgroundColor: Colors.yellow,
context: context,
isScrollControlled: true,
builder: (context) => Padding(
padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
child: SizedBox(
height: MediaQuery.of(context).size.height - 80,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SingleChildScrollView(
child: Column(
children: [
for (var m in this.messages) ...[
Text(m)
]
],
),
),
TextField(
textAlign: TextAlign.left,
decoration: InputDecoration(
hintText: 'Message',
contentPadding: EdgeInsets.all(10),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
isDense: true,
),
keyboardType: TextInputType.multiline,
maxLines: 4,
minLines: 1,
//controller: textController,
textInputAction: TextInputAction.send,
onSubmitted: (value) {
this.setState(() {
this.messages.add(value);
});
},
)
],
),
),
)
);
},
child: const Text('Show Modal Bottom Sheet'),
),
));
}
}
When keyboard is not visible everything is ok (system top panel is visible and AppBar is visible):
However, when keyboard is visible I have a problem as bottom sheet covers both top panel and and AppBar:
Could anyone say how to fix this problem so top panel and AppBar to be visible in both cases (when keyboard is on and when it is off)?
Instead of wrap your whole bottom sheet with padding, try wrap your textField with padding, like this:
builder: (context) => SizedBox(
height: MediaQuery.of(context).size.height - 80,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SingleChildScrollView(
child: Column(
children: [
for (var m in this.messages) ...[Text(m)]
],
),
),
Padding(
padding: EdgeInsets.only(
bottom:
MediaQuery.of(context).viewInsets.bottom),
child: TextField(
textAlign: TextAlign.left,
decoration: InputDecoration(
hintText: 'Message',
contentPadding: EdgeInsets.all(10),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
isDense: true,
),
keyboardType: TextInputType.multiline,
maxLines: 4,
minLines: 1,
//controller: textController,
textInputAction: TextInputAction.send,
onSubmitted: (value) {
this.setState(() {
this.messages.add(value);
});
},
),
)
],
),
));
In flutter's textField when we use prefixIcon and label, in focus mode label's animation transfers to the top of the text (not to the top of the icon).
I don't want to use prefix instead of prefixIcon because prefix hides when textField is not on focus mode. I need my prefix always visible.
Like these images from material designs text fields part:
Although there are some fix position value,
here is my implementation,
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,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
FocusNode _focusNode = FocusNode();
bool textEditHasFocus = false;
#override
void initState() {
super.initState();
_focusNode.addListener(() {
setState(() {
textEditHasFocus = _focusNode.hasFocus;
});
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: _buildBody(),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
Widget _buildBody() {
return Container(
decoration: BoxDecoration(color: Colors.white),
padding: new EdgeInsets.symmetric(
vertical: 40.0,
horizontal: 20,
),
child: Stack(
clipBehavior: Clip.none,
children: <Widget>[
Container(
child: TextField(
focusNode: _focusNode,
decoration: InputDecoration(
// hintText: textEditHasFocus ? '' : 'Label',
// hintStyle: TextStyle(
// color: Colors.grey,
// ),
border: OutlineInputBorder(),
focusedBorder: OutlineInputBorder(),
contentPadding: EdgeInsets.only(left: 40),
),
),
),
AnimatedPositioned(
duration: Duration(milliseconds: 200),
left: textEditHasFocus ? 10 : 40,
top: textEditHasFocus ? -10 : 13,
child: InkWell(
onTap: () {
_focusNode.requestFocus();
},
child: Container(
padding: EdgeInsets.only(left: 3),
color: Colors.white,
child: Text('Label'),
),
),
),
Positioned.fill(
child: Align(
alignment: Alignment.centerLeft,
child: Container(
padding: EdgeInsets.only(left: 10),
color: Colors.transparent,
child: Icon(Icons.favorite),
),
),
),
],
),
);
}
}
Your material designs text fields part link provided I try same design and your need when you focused on TextField the labelText is display on the prefixIcon In lots of their is not correct solution I found
Refer prefixIcon Widget documentaion here and see the given example
Refer prefix Widget documentation here
Refer InputDecoration class here
Try below code when icon is display ouside of the TextField
TextFormField(
decoration: InputDecoration(
icon: Icon(
Icons.favorite,
),
border: OutlineInputBorder(),
labelText: 'Brand Description',
hintText: 'Brand Description Here',
),
),
Your result screen Without focus->
Your result screen With focus->
There is one easy solution for flutter 3 plus users
InputDecorator(
decoration: const InputDecoration(
labelText: "Select Hospital",
prefixIcon: Icon(Icons.ac_unit),
contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 10),
border: OutlineInputBorder()),
child:TextFormField(),
);
What I am aiming for is, when a user long presses on a particular date, they should be able to enter their weight value, and upon submitting, the modal bottom sheet should close, and the updated weight should be visible as soon as the modal sheet is out of view. But It does not happen.
Instead I have to go to other date and come back to see the changes. Please help me. I am new to flutter.
Here is the demo of the problem :
problem i am facing
Here is the code:
void _modalBottomSheetMenu(DateTime dt) {
showModalBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
context: context,
isScrollControlled: true,
builder: (builder) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text("Add a weight"),
Container(
width: 200.0,
child: TextField(
controller: myController,
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
autofocus: true,
maxLength: 4,
onSubmitted: (value) {
weightValue = double.parse(value);
print("Weight entered is $weightValue");
print("Date passed is $dt");
setState(() {
_events[dt] = [weightValue].toList();
Navigator.pop(context);
});
print(_events[dt][0]);
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Weight',
hintText: 'Enter your weight'),
),
),
],
);
},
);
}
BuildEventList : This contains the weight to be printed
import 'package:flutter/material.dart';
class BuildEventList extends StatefulWidget {
String weight;
BuildEventList(this.weight);
#override
_BuildEventListState createState() => _BuildEventListState();
}
class _BuildEventListState extends State<BuildEventList> {
#override
void setState(fn) {
// TODO: implement setState
super.setState(fn);
}
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
border: Border.all(width: 0.8),
borderRadius: BorderRadius.circular(12.0),
),
margin: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0),
child: Container(
width: double.infinity,
child: Center(child: Text("${widget.weight}")),
),
);
}
}
This is where it is called :
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title), actions: [
IconButton(
icon: Icon(Icons.add),
color: Colors.white,
onPressed: () {},
)
]),
body: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
_buildTableCalendar(),
const SizedBox(height: 8.0),
//_buildButtons(),
const SizedBox(height: 8.0),
Expanded(
child: BuildEventList(_selectedEvents.length > 0
? _selectedEvents[0].toString()
: "No weight given!")),
],
),
);
}
I was making a simple S.I Calculator which has a field for principal amount intrest, and time along with that i have a dropdown to select the currency.
Problem is when I press 'calculate' RaisedButton(looking at the image might help) nothing appears on screen (Initially I thought there is problem with buttton but later ) I found out that after pressing RaisedButton if I selected any item from dropdown then answer appears why is this happening, I have set an inital value as _selected = 'Rupee' am I doing something wrong?.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Simple Intrest Calculator',
home: MyHomePage(),
theme: ThemeData(
brightness: Brightness.dark,
primaryColor: Colors.indigo,
accentColor: Colors.indigoAccent,
),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var _currencies = ['Rupee', 'Dollar', 'Pound'];
final _minPadding = 5.0;
var _selected = 'Rupee';
TextEditingController principalCntr = TextEditingController();
TextEditingController roiCntr = TextEditingController();
TextEditingController termCntr = TextEditingController();
String _amount = '0';
#override
Widget build(BuildContext context) {
TextStyle styles = Theme.of(context).textTheme.headline6;
return Scaffold(
//resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text('S.I Calculator'),
centerTitle: true,
),
body: Container(
margin: EdgeInsets.symmetric(vertical: 0, horizontal: _minPadding * 2),
child: ListView(
children: [
getImg(),
Padding(
padding: const EdgeInsets.symmetric(vertical: 5),
child: TextField(
controller: principalCntr,
keyboardType: TextInputType.number,
style: styles,
decoration: InputDecoration(
labelText: 'Principal',
hintText: 'Enter principal amount',
labelStyle: styles,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
),
),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 5),
child: TextField(
controller: roiCntr,
keyboardType: TextInputType.number,
style: styles,
decoration: InputDecoration(
labelText: 'Intrest',
hintText: 'Enter intrest rate',
labelStyle: styles,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: TextField(
controller: termCntr,
keyboardType: TextInputType.number,
style: styles,
decoration: InputDecoration(
labelText: 'Time',
hintText: 'Time in years',
labelStyle: styles,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
),
),
),
),
SizedBox(width: 50,),
Expanded(
child: DropdownButton(
value: _selected,
onChanged: (newVal) {
setState(() {
_selected = newVal;
});
},
items: _currencies.map((selectedVal) {
return DropdownMenuItem(
value: selectedVal,
child: Text(selectedVal),
);
}).toList(),
),
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: RaisedButton(
color: Theme.of(context).accentColor,
textColor: Theme.of(context).primaryColor,
child: Text('Calculate'),
onPressed: (){
_amount = _calculateReturns();
},
),
),
SizedBox(width: 20),
Expanded(
child: RaisedButton(
color: Theme.of(context).primaryColorDark,
textColor: Theme.of(context).primaryColorLight,
child: Text('Clear', style: TextStyle(fontSize: 20,),),
onPressed: (){
debugPrint('pressed');
},
),
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(_amount, style: styles,),
),
],
),
),
);
}
String _calculateReturns(){
double principal = double.parse(principalCntr.text);
double roi = double.parse(roiCntr.text);
double year = double.parse(termCntr.text);
double sI = principal + (principal*roi*year)/100;
String res = 'After $year years, you get $sI';
return res;
}
Widget getImg() {
AssetImage img = AssetImage('images/money.jpg');
Image image = Image(image: img, width: 125, height: 125);
return Container(
child: image,
margin: EdgeInsets.all(_minPadding * 10),
);
}
}
This is after i click Raised Button
This is after i select an item from dropdown but do not click on the button again:
Code walkthrough:
made a list of _currencies.
added textinput fields dropdown button and two raised buttons.
when calculate button is pressed _calculateReturns() is called which returns a value and stores it in _amount
Output is just a Text widget.
you forgot to update the screen when pressing on the button, remember every time you need to change the content of the page or update it you have to call the method :
setState(() {
// code here for values to be updated
});
so in your onPressed function of Raised button you have to add it like that :
onPressed: (){
_amount = _calculateReturns();
setState(() { });
}
I am trying to make a button bellow Stepper. The problem is, if I wrap it in a Column or ListView, scrolling in Stepper doesn't work. I tried to wrap them by NestedScrollView, scrolling is working, but the problem is that the button posted above Stepper. There are two example of _MyHomePageState in code, first with ListView and second with NestedView, both do not work for me. How can I implement Stepper with Button under it?
This is what I want
enter image description here
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(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
// 1 case with ListView (doesn't scroll)
class _MyHomePageState extends State<MyHomePage> {
int _currentStep = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
iconTheme: IconThemeData(color: Colors.red),
),
body: Form(
child: ListView(
children: <Widget>[
Stepper(
type: StepperType.vertical,
currentStep: _currentStep,
onStepTapped: (int index) {
setState(() {
_currentStep = index;
});
},
steps: [
Step(
title: Text('Step 1'),
content: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: 'City', border: UnderlineInputBorder()),
),
TextFormField(
decoration: InputDecoration(labelText: 'Name'),
),
TextFormField(
decoration: InputDecoration(labelText: 'Address'),
),
],
),
isActive: true,
),
Step(
title: Text('Step 2'),
content: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText: 'City'),
),
TextFormField(
decoration: InputDecoration(labelText: 'Name'),
),
TextFormField(
decoration: InputDecoration(labelText: 'Address'),
),
],
),
isActive: true,
),
Step(
title: Text('Step 3'),
content: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText: 'Width'),
),
TextFormField(
decoration: InputDecoration(labelText: 'Length'),
),
TextFormField(
decoration: InputDecoration(labelText: 'Height'),
),
TextFormField(
decoration: InputDecoration(labelText: 'Weigth'),
),
],
),
isActive: true,
),
],
),
RaisedButton(
child: Text('Button'),
onPressed: () {},
)
],
),
),
);
}
}
// 2 case with NestedScrollView
class _MyHomePageState extends State<MyHomePage> {
int _currentStep = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
iconTheme: IconThemeData(color: Colors.red),
),
body: Form(
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverList(
delegate: SliverChildListDelegate(<Widget>[
RaisedButton(
child: Text('Button'),
onPressed: () {},
)
]),
),
];
},
body: Stepper(
type: StepperType.vertical,
currentStep: _currentStep,
onStepTapped: (int index) {
setState(() {
_currentStep = index;
});
},
steps: [
Step(
title: Text('Step 1'),
content: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: 'City', border: UnderlineInputBorder()),
),
TextFormField(
decoration: InputDecoration(labelText: 'Name'),
),
TextFormField(
decoration: InputDecoration(labelText: 'Address'),
),
],
),
isActive: true,
),
Step(
title: Text('Step 2'),
content: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText: 'City'),
),
TextFormField(
decoration: InputDecoration(labelText: 'Name'),
),
TextFormField(
decoration: InputDecoration(labelText: 'Address'),
),
],
),
isActive: true,
),
Step(
title: Text('Step 3'),
content: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText: 'Width'),
),
TextFormField(
decoration: InputDecoration(labelText: 'Length'),
),
TextFormField(
decoration: InputDecoration(labelText: 'Height'),
),
TextFormField(
decoration: InputDecoration(labelText: 'Weigth'),
),
],
),
isActive: true,
),
],
),
),
),
);
}
}
All you need is to add
physics: ClampingScrollPhysics(),
property inside Stepper since Listview is a scrollable widget.
I hope it solves your problem
If the button should be persistant on the bottom of the screen you could use the persistentFooterButtons property of the Scaffold widget, or the bottomNavigationBar property with a custom widget.