How is it possible to edit Text in a flutter application? - flutter

I want to show a paragraph and once the user selects a part of it, somehow he can get the option to edit the selected text and the string will change and replace the old text, please tell me if that's possible.
I have found flutter_selectext plugin but it doesn't show in the examples if it's possible to edit the text or the string that provides the text to the widget it just shows that it can copy which I cannot use.

I believe you want to have a functionality of EditText just like in android. I have a similar problem before. Here is how Solve it. Define the TextEditingController in State.
and you can initialize in the InitState. you can use listener for more controll of text ( like saving the previous old text)
class _ProfileEditPageState extends State<ProfileEditPage> {
TextEditingController nameController;
.. }
#override
void initState() {
nameController = TextEditingController();
super.initState();
}
Here is how you can implement in your widget.
Padding(
padding: const EdgeInsets.only(right: 50),
child: TextField(
autofocus: false,
decoration: InputDecoration(
disabledBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(5.0)),
borderSide: BorderSide(color: Colors.transparent)),
focusedBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(5.0)),
borderSide: BorderSide(color: Colors.blue)),
border: InputBorder.none,
),
controller: nameController
..text = _userProfileData.background, //this is your text
),
),

Related

How can I generate a new TextEditingController every time when I add a new TextBlock

So, am having this page, where I can add multiple TextFormFiled that represent a text block. The thing is, it's generated dynamic so you never know how many Text Editing controllers you need.
void addTextBlock() {
state.blocks.add(
TextBlock(hint: 'Description', controler: state.descriptionController));
}
Here is the code that trigger when tapping Add Text Block, and as you can see it uses the same controller.
The TextBlock wiget :
class TextBlock extends Block {
TextBlock({required this.controler, required this.hint})
: super(BlockType.TextBlock);
final String hint;
final TextEditingController controler;
#override
Widget toWidget() {
return TextFormField(
controller: controler,
decoration: InputDecoration(
filled: true,
fillColor: AppColors.textFieldBackground,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide(color: AppColors.textFieldHintColor),
),
contentPadding:
const EdgeInsets.symmetric(vertical: 22.0, horizontal: 24.0),
hintText: hint,
hintStyle:
AppTextStyles.normalRoboto(color: AppColors.textFieldHintColor),
),
maxLines: null,
keyboardType: TextInputType.multiline,
style: AppTextStyles.normalRoboto(color: AppColors.textFieldHintColor),
);
}
}
Try out below code:
List<TextEditingController> textEditingControllers = [];
void addTextBlock() {
TextEditingController textEditingController = TextEditingController();
textEditingControllers.add(textEditingController)
state.blocks.add(
TextBlock(hint: 'Description', controler: textEditingControllers[textEditingControllers.length-1]));
}

Isn't TextField's suffix means putting widget at the right hand side of TextField?

I want to place this CustomDropDownBtn in suffix of Brought Quantity TextField. But It is still not placing this Widget in right side of textfield.
TextField(
decoration: InputDecoration(
filled: true,
fillColor: kTextFieldBgColor,
enabledBorder: UnderlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(12.0)),
borderSide: BorderSide(style: BorderStyle.none),
),
suffix: this.showSuffixWidget == true ? this.suffixWidget : null,
focusedBorder: UnderlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(12.0)),
borderSide: BorderSide(style: BorderStyle.none),
),
),
),
The suffixWidget in the above code is CustomDropDownBtn Widget.
Tried CustomDropDownBtn to suffixIcon, but didn't worked.
Also, I tried
suffix: this.showSuffixWidget == true ? Align(alignment: Alignment.centerRight, this.suffixWidget) : null,
But didn't worked.
Is adding padding only the option left?
Note: removing this.showSuffixWidget == true ? this.suffixWidget : null, and adding Icon(...) seems to work, but that's not what I want.
Try using suffixIcon instead of suffix. Or maybe wrap the this.suffixWidget with a sized box of specific width.

Exiting Focus from Multiline - Flutter

I am currently trying to make a page that take notes on my app so I am using the multiline keyboard type to keep track of the text. However, I am having difficulties escaping the focus of the text. Return brings a new line (which I want it to do) but I am unsure as to when I have onSubmit or onEditingComplete because I never actually submit anything using multiline. In addition, tapping outside of the textfield does not exit the focus of the field. Here is the code that I wrote please let me know if you have any recommendations!
import 'package:flutter/material.dart';
class NoteDetail extends StatefulWidget {
#override
_NoteDetailState createState() => _NoteDetailState();
}
class _NoteDetailState extends State<NoteDetail> {
#override
Widget build(BuildContext context) {
final dimensions = MediaQuery.of(context).size;
return LimitedBox(
maxHeight: dimensions.height - 100,
maxWidth: dimensions.width - 100,
child: Column(
children: <Widget>[
SizedBox(
height: dimensions.height * 0.10,
),
TextField(
keyboardType: TextInputType.multiline,
//this allows the text to work with multiple lines
maxLines: null,
//no max lines making it scroll forever
decoration: InputDecoration(
hintText: 'Start taking some notes',
//shows text before the user starts to text
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
//this removes the border all together
contentPadding:
EdgeInsets.symmetric(horizontal: dimensions.width * 0.1),
//giving the content some padding on the side
),
),
],
),
);
}
}

Flutter Why my textfield lose the value when I dont focus on it?

I made a class and it has a controller and a widget that includes a padding within a textfield.
Now when I change the focus from the textfield to something else it lose the value.
this is my code.
`
class RxInput {
final String hindText;
final bool obscureText;
Widget widget;
final TextEditingController controller = new TextEditingController ();
String get value => controller.text;
RxInput (this.hindText,context,{this.obscureText = false}) {
widget = Padding (
padding: const EdgeInsets.all(20.0),
child: Material(
borderRadius: BorderRadius.circular(20.0),
shadowColor: Colors.blue.shade200,
elevation: 5.0,
child:TextField(
controller: controller,
decoration: InputDecoration(
hintText: hindText,
border: InputBorder.none,
alignLabelWithHint: true,
),
obscureText: obscureText,
)
),
);
}
}
`
I solved this question. You should initialize your variable in the initState instead of build.

Text in the TextField disappears when keyboard is removed

Text entered in my TextField widget disappears when I remove the keyboard from the view.
There are two TextField's, title and description. The above problem only occurs for the title but not with the description.
Here's the relevant excerpt from the build method:
#override
Widget build(BuildContext context) {
_note = widget._note; // This is coming from StatefulWidget Class above
TextStyle textStyle = Theme.of(context).textTheme.title;
_titleController.text = _note.title;
_descriptionController.text = _note.description;
return Scaffold(
body: ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.all(15.0),
child: TextField(
style: textStyle,
controller: _titleController,
decoration: InputDecoration(
labelText: "Title",
labelStyle: textStyle,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
),
),
Padding(
padding: EdgeInsets.all(15.0),
child: TextField(
style: textStyle,
controller: _descriptionController,
decoration: InputDecoration(
labelText: "Description",
labelStyle: textStyle,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
),
),
...
}
}
Screenshots of Keyboard shown & removed.
This occurs because you are setting the text in your build method. This build method can get invoked at any time, e.g. when the keyboard is contracted because the UI needs to react to that.
This means that you should move this code to initState:
#override
void initState() {
_note = widget._note;
_titleController.text = _note.title;
_descriptionController.text = _note.description;
super.initState();
}
initState is only called once when the your widget is inserted into the build tree.
I am not sure why this only happens with one of the TextFields's. I assume that you are using the TextController's somewhere else to set the Note's content, which could cause this behavior.
Furthermore, you should probably avoid using a leading underscore _ for _note in your StatefulWidget (widget._note) as you access it from your State.