我无法在onPressed属性中调用方法(颤振)

时间:2020-09-10 02:09:25

标签: flutter dart

我试图从全局变量文件访问方法,但是我不知道如何。这是我试图调用该方法的主要代码:

import '../../global_variables.dart';
import '../../main_components/default_button.dart';
class WelcomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        GlobalVariables().init(context);
        final globalVariables = GlobalVariables();
        final screen = GlobalVariables.screen;

        return DefaultButton(
          screen: screen,
          txt: 'Entrar',
          style: kWhiteButtonTextStyle,
          color: kBlackColor,
          function: globalVariables.NavigationToLoginPage(context),
        ),
      }
    }

这是全局变量文件代码:

import 'pages/login/login_page.dart';

class GlobalVariables {
  static MediaQueryData _mediaQueryData;
  static Size screen;

  NavigationToLoginPage(BuildContext context) {
    Navigator.push(
        context, MaterialPageRoute(builder: (context) => LoginPage()));
  }

这是默认的按钮类,我必须在onPressed属性中调用此方法。

  const DefaultButton({
    Key key,
    @required this.screen,
    @required this.txt,
    @required this.style,
    @required this.color,
    @required this.function,
  }) : super(key: key);

  final Size screen;
  final String txt;
  final TextStyle style;
  final Color color;
  final Function function;

  @override
  Widget build(BuildContext context) {
    return FlatButton(
      child: Text(
        txt,
        style: style,
      ),
      color: color,
      padding: EdgeInsets.only(
        top: screen.height * 0.02,
        left: screen.width * 0.3,
        bottom: screen.height * 0.02,
        right: screen.width * 0.3,
      ),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10),
      ),
      onPressed: function,
    );
  }
}

1 个答案:

答案 0 :(得分:0)

执行此操作:

import tkinter as tk
from tkinter import font


class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        #persistent font reference
        textfont = font.Font(family='arial', size='14')
        
        #something to type in ~ uses the persistent font reference
        tk.Text(self, font=textfont).grid(row=0, column=0, sticky='nswe')
        
        #make the textfield fill all available space
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)
        
        #font chooser
        fc = tk.Listbox(self)
        fc.grid(row=0, column=1, sticky='nswe')

        #insert all the fonts
        for f in font.families():
            fc.insert('end', f)

        #switch textfont family on release
        fc.bind('<ButtonRelease-1>', lambda e: textfont.config(family=fc.get(fc.curselection())))
        
        #scrollbar ~ you can actually just use the mousewheel to scroll
        vsb = tk.Scrollbar(self)
        vsb.grid(row=0, column=2, sticky='ns')
        
        #connect the scrollbar and font chooser
        fc.configure(yscrollcommand=vsb.set)
        vsb.configure(command=fc.yview)


if __name__ == "__main__":
    app = App()
    app.title('Font Chooser Example')
    app.geometry(f'800x600+200+200')
    app.mainloop()