简体   繁体   English

如何编写 function(在颤振飞镖中)以便在我们调用 function 时接受某些参数?

[英]How to write a function (in flutter-dart) so that it accepts certain parameters when we call that function?

This is my code:这是我的代码:

Text ButtonText = Text(
  _buttonText, style: TextStyle(
    color: Colors.white,
    fontFamily: 'San francisco',
    //fontSize: 21.0.ssp,
    letterSpacing: 2.0,
    wordSpacing: 2.0
),
);

when I use this Text in my button widget, I want to set font size explicitly.当我在按钮小部件中使用此文本时,我想明确设置字体大小。 How can I do that?我怎样才能做到这一点?

you can create a class for your situation we can call it customtext here is an example code:你可以为你的情况创建一个 class 我们可以称之为 customtext 这里是一个示例代码:

import 'package:flutter/material.dart';

class CustomText extends StatelessWidget {
  final String text;
  final double size;
  final Color color;
  final FontWeight weight;
  
  // name constructor that has a positional parameters with the text required
  // and the other parameters optional
  CustomText({@required this.text, this.size,this.color,this.weight});

  @override
  Widget build(BuildContext context) {
    return Text(
      text,style: TextStyle(fontSize: size ?? 16, color: color ?? Colors.black, fontWeight: weight ?? FontWeight.normal),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Dart/Flutter 中声明一个接受多个参数的 function 回调? - How to declare a function callback that accepts multiple parameters in Dart/Flutter? Flutter-Dart NoSuchMethodError(NoSuchMethodError:类'int'没有实例方法'call'。接收者:5尝试调用:call(“25”)) - Flutter-Dart NoSuchMethodError (NoSuchMethodError: Class 'int' has no instance method 'call'. Receiver: 5 Tried calling: call("25")) 如何从 Dart/Flutter 中的通用 function 调用命名构造函数 - How to call a named constructor from a generic function in Dart/Flutter 如何摆脱这个自定义小部件周围的额外填充? (飞镖) - How do I get rid of the extra padding around this custom widget? (flutter-dart) Flutter,如何用 dart 编写所需的 function? - Flutter, how can I write desired function with dart? 我如何知道列表中最畅销的产品是什么? 飞镖 - How can I know what is the most sold product of a list? flutter-dart 如何使用 object snapshot.data 在简单的 flutter-dart 代码中实现空安全? - How to implement null-safety in simple flutter-dart code with object snapshot.data? 不匹配 arguments 的闭包调用:function '[]' (Flutter/Dart) - Closure call with mismatched arguments: function '[]' (Flutter/Dart) 带参数的 Flutter 云函数调用 - Flutter cloud function call with parameters Flutter - 从 kotlin 拨打 dart function - Flutter - Call dart function from kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM