I need to do the following:
By other words only one of two buttons can be selected and there must be a TextField
between them. The only solution I found is to use two ToggleButtons
with TextField between them. I mean:
Row(
children: [
ToggleButtons(...),
TextField(...),
ToggleButtons(...),
]
)
But I don't like this solution because two buttons must belong to one widget. As the same time time when I insert TextField in ToggleButton.children TextField becomes third button and this is not what I need.
Could anyone say how to solve such a problem in Flutter?
CodePudding user response:
You can create custom widget to handle this
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(body: Center(child: CustomCounter())));
}
}
class CustomCounter extends StatefulWidget {
const CustomCounter({Key? key}) : super(key: key);
@override
State<CustomCounter> createState() => _CustomCounterState();
}
class _CustomCounterState extends State<CustomCounter> {
bool minusSelected = false;
TextEditingController _textEditingController = TextEditingController();
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
InkWell(
onTap: () {
minusSelected = true;
setState(() {
int currentValue = int.tryParse(_textEditingController.text) ?? 0;
_textEditingController.text = (currentValue -= 1).toString();
_textEditingController.selection = TextSelection.fromPosition(
TextPosition(offset: _textEditingController.text.length));
});
},
child: Container(
decoration: BoxDecoration(
color: minusSelected ? Colors.green : Colors.white,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(4),
topLeft: Radius.circular(4)),
border: Border.all(color: Colors.grey, width: 1)),
child: Icon(Icons.remove),
),
),
SizedBox(
width: 100,
child: TextField(
controller: _textEditingController,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
decoration: InputDecoration(
isDense: true,
contentPadding: EdgeInsets.all(5),
border: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey)),
),
)),
InkWell(
onTap: () {
minusSelected = false;
setState(() {
int currentValue = int.tryParse(_textEditingController.text) ?? 0;
_textEditingController.text = (currentValue = 1).toString();
_textEditingController.selection = TextSelection.fromPosition(
TextPosition(offset: _textEditingController.text.length));
});
},
child: Container(
decoration: BoxDecoration(
color: minusSelected == false ? Colors.green : Colors.white,
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(4),
topRight: Radius.circular(4)),
border: Border.all(color: Colors.grey, width: 1)),
child: Icon(Icons.add),
),
)
],
);
}
}
CodePudding user response:
You don't have to use TextField, you can use a simple container to set your value
Row(
children: [
ToggleButtons(...),
Container(child: Text(yourValue),
ToggleButtons(...),
]
)
Or you can use readonly Textfield,
TextField(readonly: true,...)