如何在另一个带有很多参数的StatefulWidget类中使用自定义文本字段?

问题描述

今天,我创建了自己的自定义文本字段,我想在许多页面中使用它,但是其中包含一些参数。 您可以在这里看到

import 'package:flutter/material.dart';

class RequiredText extends StatefulWidget {

@override
_RequiredTextState createState() => _RequiredTextState();
}

class _RequiredTextState extends State<RequiredText> {
final myController = TextEditingController();

@override
void dispose() {
myController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
String LabelTextField;
String HelperTextField;
Color ColorBorder;
Color ColorField;
Color ColorCursor;

return Padding(
  padding: const EdgeInsets.only(left: 18.0),child: TextField(
    cursorColor: ColorCursor,style: TextStyle(
      color: ColorField,),keyboardType: TextInputType.number,textInputAction: TextInputAction.next,controller: myController,decoration: InputDecoration(
        enabledBorder: new OutlineInputBorder(
            borderSide: BorderSide(width: 1.5,color: ColorBorder)),border: OutlineInputBorder(
            borderSide: new BorderSide(color: Colors.cyan[200]),borderRadius: new BorderRadius.all(Radius.circular(20.0))),helperText: HelperTextField,labelText: LabelTextField,labelStyle: TextStyle(
          color: Colors.black26,fontSize: 20.0,fontFamily: 'DancingScript',icon: Icon(
          Icons.apps,)),);
}
}

但是我也想在main.dart类和其他页面中使用它。 但是显示错误

import 'package:AllInOneCalci/CustomTextFields.dart';
import 'package:AllInOneCalci/customAppBar.dart';
import 'package:flutter/material.dart';

class BMICalcUI extends StatefulWidget {
@override
_BMICalcUIState createState() => _BMICalcUIState();
}

class _BMICalcUIState extends State<BMICalcUI> {
 @override
 Widget build(BuildContext context) {
 double AppBarHeight = MediaQuery.of(context).size.height;
 return Scaffold(
   appBar: customAppBar(
    height: (AppBarHeight / 3) * 0.4,child: Row(
      mainAxisAlignment: MainAxisAlignment.center,children: [
        Padding(
          padding: const EdgeInsets.only(top: 18.0),child: Text(
            'All In One Cali',style: TextStyle(
                color: Colors.black,fontSize: 35.0,fontWeight: FontWeight.bold),],body: Padding(
    padding: const EdgeInsets.only(top: 18.0),child: Container(
      width: 300.0,child: Column(
        children: [
          RequiredText('Height','Input height in meters',Colors.cyan[200],Colors.redAccent,Colors.redAccent),);
 }
}

我也想在很多页面中使用它。您能帮我该怎么做吗? 这对我很有帮助。我被困在这里

RequiredText('Height',

此行显示错误。

解决方法

String LabelTextField;
String HelperTextField;
Color ColorBorder;
Color ColorField;
Color ColorCursor;

您提到了参数,但是您没有对其进行初始化, 用这种方式做

class RequiredText extends StatefulWidget {
String LabelTextField;
String HelperTextField;
Color ColorBorder;
Color ColorField;
Color ColorCursor;
RequiredText(this.LabelTextField,this.HelperTextField,this.ColorBorder,this.ColorField,this.ColorCursor);
@override
_RequiredTextState createState() => _RequiredTextState();
}

class _RequiredTextState extends State<RequiredText> {
final myController = TextEditingController();

@override
void dispose() {
myController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {


return Padding(
  padding: const EdgeInsets.only(left: 18.0),child: TextField(
    cursorColor: widget.ColorCursor,style: TextStyle(
      color: widget.ColorField,),keyboardType: TextInputType.number,textInputAction: TextInputAction.next,controller: myController,decoration: InputDecoration(
        enabledBorder: new OutlineInputBorder(
            borderSide: BorderSide(width: 1.5,color: widget.ColorBorder)),border: OutlineInputBorder(
            borderSide: new BorderSide(color: Colors.cyan[200]),borderRadius: new BorderRadius.all(Radius.circular(20.0))),helperText: widget.HelperTextField,labelText: widget.LabelTextField,labelStyle: TextStyle(
          color: Colors.black26,fontSize: 20.0,fontFamily: 'DancingScript',icon: Icon(
          Icons.apps,)),);
}
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...