OpenFlutter / flutter_oktoast仅在主页上显示?

问题描述 投票:0回答:1

我正在使用this库在我的应用程序中显示自定义吐司。我的应用中有多个页面。问题是,即使我从任何其他页面调用showToastWidget(...),吐司也出现在主页面上。

主页

  @override
  Widget build(BuildContext context) {
    return OKToast(
      child: Scaffold(
        backgroundColor: Theme.of(context).accentColor,
        body: Center(
          child: SizedBox(
            height: 50,
            width: 50,
            child: Image(image: AssetImage('assets/ic_logo.png')),
          ),
        ),
      ),
    );
  }

第2页

@override
  Widget build(BuildContext context) {
    return OKToast(
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text('Reset Password'),
        ),
        body: Center(
          child: Padding(
            padding: EdgeInsets.all(20),
            child: FlatButton(
              onPressed: () {
                showToastWidget(
                  Text('Hello. I am Toast!!!'),
                  duration: Duration(seconds: 2),
                );
              },
              child: Text('Show'),
            ),
          ),
        ),
      ),
    );
  }

当我从这个页面调用showToastWidget(...)时,它出现在主页面上

编辑1当我将上下文传递给showToastWidget()时,我得到了这个异常

I/flutter (24327): The following NoSuchMethodError was thrown while handling a gesture:
I/flutter (24327): The getter 'position' was called on null.
I/flutter (24327): Receiver: null
I/flutter (24327): Tried calling: position
I/flutter (24327): 
I/flutter (24327): When the exception was thrown, this was the stack:
I/flutter (24327): #0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
I/flutter (24327): #1      showToastWidget (package:oktoast/src/toast.dart:210:40)
dart flutter flutter-dependencies
1个回答
1
投票

看起来OKToast库不支持同一个应用程序中的多个OKToast小部件。您必须将整个应用程序包装在OKToast小部件中,完整示例:

import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return OKToast(
      child: MaterialApp(
        home: MainPage(),
      ),
    );
  }
}

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).accentColor,
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            FlatButton(
              child: Text("Show"),
              onPressed: () {
                showToast(
                  "Main Page toast",
                  duration: Duration(seconds: 2),
                );
              },
            ),
            SizedBox(height: 12.0),
            FlatButton(
              child: Text("Go to next page"),
              onPressed: () => _goToNextPage(context),
            ),
          ],
        ),
      ),
    );
  }

  void _goToNextPage(BuildContext context) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => SecondPage(),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("Second Page"),
      ),
      body: Center(
        child: Padding(
          padding: EdgeInsets.all(20),
          child: FlatButton(
            onPressed: () {
              showToast(
                "Second Page toast",
                duration: Duration(seconds: 2),
              );
            },
            child: Text('Show'),
          ),
        ),
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.