简体   繁体   中英

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:

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),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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