pile_nav_new/lib/service/pile/input.dart
2024-08-23 17:54:57 +08:00

169 lines
4.9 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ignore_for_file: must_be_immutable
import 'package:flutter/material.dart';
import 'device_type.dart';
// ignore: camel_case_types,
class inputText extends StatefulWidget {
//内容
String value;
//跟在后面的单位,默认为空
String? endValue;
//控制焦点
FocusNode? focusNode;
//控制长度
double inputLength;
//控制键盘类型默认为数字0其他参数都是字符串
int keyboardType;
//回调数据变化后返回新值
double fontsize;
double inputHeight;
final ValueChanged<String>? onChanged;
final Function()? onTap;
inputText(
{super.key,
required this.value,
required this.focusNode,
required this.onChanged,
this.endValue = "",
this.inputLength = 200,
this.keyboardType = 0,
this.fontsize = 20,
this.inputHeight = 40,
this.onTap});
@override
State<inputText> createState() => _inputTextState();
}
// ignore: camel_case_types
class _inputTextState extends State<inputText> {
//控制默认文本
TextEditingController? _controller;
//传入的值,作为显示的初始值
String _inputTextContext = "";
//字体
late double _fontSize;
// late double _inputHeight;
Color _fontcolor = Colors.black;
@override
void initState() {
super.initState();
_inputTextContext = widget.value;
_controller = TextEditingController(text: _inputTextContext);
_fontSize = widget.fontsize;
// _inputHeight = widget.inputHeight;
}
@override
Widget build(BuildContext context) {
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
_fontcolor = isDarkMode ? Colors.white : Colors.black;
final deviceType = getDeviceType(context);
double dynamicWidth = deviceType == DeviceType.mobile ? 0 : 50;
return Row(
children: [
Container(
margin: const EdgeInsets.only(bottom: 5),
width: widget.endValue != ""
? widget.inputLength + dynamicWidth - 5
: widget.inputLength + dynamicWidth,
height: widget.inputHeight,
child: TextField(
keyboardType: widget.keyboardType == 0
? TextInputType.number
: TextInputType.text,
clipBehavior: Clip.hardEdge,
focusNode: widget.focusNode,
controller: _controller,
style: TextStyle(fontSize: _fontSize, color: _fontcolor),
onChanged: (value) {
setState(() {
_inputTextContext = value;
});
// 调用回调函数,通知外部数据变化
if (widget.onChanged != null) {
widget.onChanged!(value);
}
},
decoration: InputDecoration(
contentPadding: const EdgeInsets.only(left: 10),
hintText: _inputTextContext,
border: const OutlineInputBorder(
borderSide: BorderSide(width: 1, color: Colors.black))),
onTap: widget.onTap,
),
),
Container(
padding: const EdgeInsets.only(left: 10),
child: Transform.translate(
offset: const Offset(-35.0, 0.0),
child: Text(
"${widget.endValue}",
style: TextStyle(fontSize: _fontSize, color: _fontcolor),
),
),
)
],
);
}
}
class DropDown extends StatefulWidget {
List<String> optionList = [];
double listLength;
double fontSize;
double listHeight;
final void Function(String?) onValueChanged;
DropDown(
{super.key,
required this.optionList,
this.listLength = 200,
this.fontSize = 20,
this.listHeight = 50,
required this.onValueChanged});
@override
State<DropDown> createState() => _DropDownState();
}
class _DropDownState extends State<DropDown> {
List<String> optionList = [];
String? dropdownValue;
@override
void initState() {
super.initState();
optionList = widget.optionList;
// dropdownValue = widget.optionList.isNotEmpty ? widget.optionList[0] : null;
dropdownValue = optionList[0];
// widget.onValueChanged(optionList[0]);
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
widget.onValueChanged(dropdownValue);
});
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: widget.listHeight,
width: widget.listLength,
child: DropdownButton<String>(
value: dropdownValue,
style: TextStyle(fontSize: widget.fontSize, color: Colors.black),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
widget.onValueChanged(newValue);
},
items: optionList.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
));
}
}