繁体   English   中英

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

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

这是我的代码:

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

当我在按钮小部件中使用此文本时,我想明确设置字体大小。 我怎样才能做到这一点?

你可以为你的情况创建一个 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM