OpenFlutter / flutter_oktoast показывает только на главной странице? - PullRequest
0 голосов
/ 17 апреля 2019

Я использую эту библиотеку для отображения пользовательских тостов в моем приложении. У меня есть несколько страниц в моем приложении. Проблема в том, что тост появляется на главной странице, даже когда я звоню 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)

1 Ответ

1 голос
/ 17 апреля 2019

Похоже, библиотека 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'),
          ),
        ),
      ),
    );
  }
}
...