【问题标题】:I would like to be able to change the Fontfamily when I press the ElevatedButton in flutter当我在颤动中按下 ElevatedButton 时,我希望能够更改 Fontfamily
【发布时间】:2025-11-23 05:35:01
【问题描述】:

当按下 ElevatedButton 时,我希望顶部图片中的 Textfield 和 fontFamily 发生变化。我该怎么办?我想试试 GetX。

home_page.dart

ListView(
  scrollDirection: Axis.horizontal,
 children: [
      FontFamilyWidget(
             fontFamily: 'Cafe24SsurroundAir',
             buttonName: '✈️ 카페24 써라운드 에어'),
             const SizedBox(width: 5),
      FontFamilyWidget(
             fontFamily: 'RIDIBatang', buttonName: '???? 리디바탕'),
             const SizedBox(width: 5),
      FontFamilyWidget(
             fontFamily: 'InkLipquid', buttonName: ' 잉크립퀴드체'),
             const SizedBox(width: 5),
              .
              .
              .

fontfamily_widget.dart

【问题讨论】:

    标签: flutter dart flutter-getx


    【解决方案1】:

    'font_controller.dart'的一部分

     String select = 'RIDIBatang';
    
      void onClickChangeFont(String changedFontFamily) {
        select = changedFontFamily;
        update();
      }
    

    'fontfamily_widget.dart'的一部分

    class FontFamilyWidget extends StatefulWidget {
      final String fontFamily;
      final String buttonName;
      final String changedFontFamily;
    
      const FontFamilyWidget({
        Key? key,
        required this.fontFamily,
        required this.buttonName,
        required this.changedFontFamily,
      }) : super(key: key);
    
      @override
      _FontFamilyWidgetState createState() => _FontFamilyWidgetState();
    }
    
    class _FontFamilyWidgetState extends State<FontFamilyWidget> {
      FontController c = Get.find();
    
      @override
      Widget build(BuildContext context) {
        return ElevatedButton(
          //텍스트필드와 상단 텍스트의 fontfamily를 변경해주는 함수
          onPressed: () {
            c.onClickChangeFont(widget.changedFontFamily);
          },
    
          child: Text(
            widget.buttonName,
            style: TextStyle(fontFamily: widget.fontFamily),
          ),
        );
      }
    }
    
    
    

    之后,FontFamilyWidget 被应用到 home_page.dart。

    【讨论】:

    • 你测试了吗?我认为它不会起作用,因为您在小部件树中没有任何地方使用GetBuilder
    最近更新 更多