【问题标题】:How to apply properties to imported library on flutter project如何将属性应用于 flutter 项目中导入的库
【发布时间】:2023-01-10 01:13:52
【问题描述】:

我是 flutter 的新手,我正在尝试将一些属性添加到我刚从 pub.dev 导入的库中。

该库是“flutter_input_field.dart” 来自这个回购协议:https://github.com/SatishKumarRaizada/fancy_textfield/blob/master/lib/src/flutter_form_field.dart

一切正常,直到我添加 InputDecoration "(focusedBorder: OutlineInputBorder( 边框:边框( color: borderColor)" 属性,我得到一个错误: “不能将参数类型‘InputDecoration’分配给参数类型‘Color’”

这是代码

FlutterInputField(
              borderColor: InputDecoration(focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(
                    color: borderColor),
              )),
              hintText: 'Enter 1st Flour Quantity',
              labelText: 'Flour Quantity',
              textFieldController: _textEditingController,
              filledColor: Colors.grey.shade200,
              onChange: (String st) {},
              prefixWidget: const Icon(Icons.fastfood),
              onDone: () {
              },
            ),

【问题讨论】:

    标签: flutter layout colors


    【解决方案1】:
    import 'package:flutter/material.dart';
    
    class FlutterInputField extends StatelessWidget {
      final String hintText;
      final String labelText;
      final Function onChange;
      final Function onDone;
      final Widget? prefixWidget;
      final Widget? suffixWidget;
      final Color borderColor;
      final Color focusColor;
      final Color cursorColor;
      final TextEditingController? textFieldController;
      final Function? suffixTap;
      final Function? validateTextField;
      final Color? filledColor;
      final bool isSecure;
      final int lineHeight;
      final double labelFontSize;
      final double hintFontSize;
    
      const FlutterInputField(
          {Key? key,
          required this.hintText,
          required this.labelText,
          required this.onChange,
          required this.onDone,
          this.prefixWidget,
          this.suffixWidget,
          this.borderColor = Colors.blue,
          this.textFieldController,
          this.suffixTap,
          this.validateTextField,
          this.filledColor = Colors.lightBlue,
          this.isSecure = false,
          this.lineHeight = 1,
          this.labelFontSize = 16,
          this.hintFontSize = 16,
          this.focusColor = Colors.blue,
          this.cursorColor = Colors.blue})
          : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(labelText),
            const SizedBox(height: 10),
            TextFormField(
              cursorColor: cursorColor,
              controller: textFieldController,
              obscureText: isSecure,
              maxLines: lineHeight,
              decoration: InputDecoration(
                focusColor: Colors.blue,
                contentPadding: const EdgeInsets.symmetric(
                  horizontal: 10,
                  vertical: 15,
                ),
                errorStyle: const TextStyle(
                  fontSize: 16,
                ),
                filled: true,
                fillColor: filledColor,
                hintText: hintText,
                prefixIcon: prefixWidget,
                suffixIcon: suffixWidget,
                labelStyle: TextStyle(
                  fontSize: labelFontSize,
                ),
                hintStyle: TextStyle(
                  fontSize: hintFontSize,
                ),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8.0),
                  borderSide: const BorderSide(
                    width: 0,
                    style: BorderStyle.none,
                  ),
                ),
                focusedBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8.0),
                  borderSide: BorderSide(
                    width: 0.5,
                    style: BorderStyle.solid,
                    color: focusColor,
                  ),
                ),
              ),
              onChanged: (String str) {
                onChange(str);
              },
              validator: (String? value) {
                return validateTextField!(value);
              },
              onEditingComplete: () {
                onDone();
              },
            ),
          ],
        );
      }
    }
    
    
      
    

    将此文本字段添加到您的项目并调用它

    FlutterInputField( borderColor: Colors.white, focusColor: Colors.green, hintText: '输入第一个面粉数量', labelText: '面粉数量', textFieldController: TextEditingController(), 填充颜​​色:Colors.grey.shade200, onChange:(字符串 st){}, prefixWidget:常量图标( 图标.快餐, 颜色:Colors.green, ), cursorColor: Colors.green, onDone: () {}, ),

    我只是简单地添加了一个定义的可变焦点颜色,并在构造函数中调用它,并从我想调用它的地方分配了颜色,请注意,如果您希望 borderColor 和 focusColor 相同,则不需要这样做。为此你可以简单地给

    边框颜色:Colors.black

    【讨论】:

    • 是的我知道。但我也需要更改 focusedBorder 颜色。如果我只放 borderColor,那么 onFocus 颜色仍然是默认颜色。对不起,顺便说一句,我在描述问题时犯了错误。
    • borderColor: Colors.green,这将使 TextFeild 的 borderColor 和 focus Color 也变成绿色。如果你想在焦点上添加不同的颜色,我正在为此编辑答案
    • 不错不错!谢谢你。所以,这就是图书馆的工作方式。我需要添加类和所有文本
    • 那么图标和光标呢?我找不到参数。当我单击文本字段时,现在焦点是灰色的,正如我想要的那样,但图标和光标仍然是蓝色的。非常感谢
    • 就像我对 cursorColor 的焦点颜色所做的一样,并直接从主小部件提供 IconColor 我已经更新了我的答案
    猜你喜欢
    • 2018-08-10
    • 2018-05-29
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 2022-12-31
    • 1970-01-01
    • 2023-01-22
    相关资源
    最近更新 更多