简体   繁体   English

在 Flutter 中创建按钮变量的问题

[英]Issue creating a button variable in Flutter

I am currently using flutter for an android app and I am using the "Routegenerator.dart" method for navigating .我目前正在将颤振用于 android 应用程序,并且正在使用“Routegenerator.dart”方法进行导航。 In this project, a certain button gets repeated multiple times and always leads to the same page.在这个项目中,某个按钮会重复多次并且总是指向同一个页面。 I want to create a variable of this button to clean the code a bit and avoid myself useless repetitions.我想创建这个按钮的一个变量来清理代码并避免自己无用的重复。 The issue here is that I need to put the variable after the class with the scaffold, and this causes the Navigator.of(context).pushNamed() to give me an error in the (context).这里的问题是我需要将变量放在带有脚手架的类之后,这会导致 Navigator.of(context).pushNamed() 在(上下文)中给我一个错误。 How to solve this issue please?请问如何解决这个问题?

you can call TextButtonWidget anywhere in your screen like this:您可以像这样在屏幕中的任何位置调用 TextButtonWidget:

 TextButtonWidget(
         onTap: (){
           Navigator.of(context).pushNamed('anyScreen');
      },),

Import this widget导入此小部件

   import 'package:flutter/material.dart';

class TextButtonWidget extends StatelessWidget {
  const TextButtonWidget({
    Key? key,
    required this.onTap,
  }) : super(key: key);
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    ThemeData _theme = Theme.of(context);
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: onTap,
        child: Container(
            padding: const EdgeInsets.all(8.0),
            child: const Text('Choose me')),
      ),
    );
  }
}

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

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