Toast 出现在错误的时间
Toast appears at the wrong time
我正在为我的应用程序 (bot_toast) 测试新的 toast,我需要从另一个文件调用它来改进我的代码,但我无法使其以正确的方式工作。
我知道它是一些基本的东西,但我尝试了几个小时,但仍然无法解决。
问题是:当我 运行 应用程序时吐司弹出,而不是当我单击按钮时(这应该是正确的行为)。
代码如下:
main.dart
import 'package:teste_toast/notification.dart';
import 'package:bot_toast/bot_toast.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BotToastInit(
child: MaterialApp(
navigatorObservers: [BotToastNavigatorObserver()],
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Toast test'),
])),
floatingActionButton: FloatingActionButton(
onPressed: ShowToast(context),
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
notification.dart:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
ShowToast(BuildContext context) {
return BotToast.showText(text: 'toast message');
}
确保您在 onPressed
方法中调用方法,如下所示:
floatingActionButton: FloatingActionButton(
onPressed:(){ ShowToast(context),}
child: Icon(Icons.add),
),
我正在为我的应用程序 (bot_toast) 测试新的 toast,我需要从另一个文件调用它来改进我的代码,但我无法使其以正确的方式工作。 我知道它是一些基本的东西,但我尝试了几个小时,但仍然无法解决。 问题是:当我 运行 应用程序时吐司弹出,而不是当我单击按钮时(这应该是正确的行为)。
代码如下:
main.dart
import 'package:teste_toast/notification.dart';
import 'package:bot_toast/bot_toast.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BotToastInit(
child: MaterialApp(
navigatorObservers: [BotToastNavigatorObserver()],
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Toast test'),
])),
floatingActionButton: FloatingActionButton(
onPressed: ShowToast(context),
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
notification.dart:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
ShowToast(BuildContext context) {
return BotToast.showText(text: 'toast message');
}
确保您在 onPressed
方法中调用方法,如下所示:
floatingActionButton: FloatingActionButton(
onPressed:(){ ShowToast(context),}
child: Icon(Icons.add),
),