A Flutter吐司插件错误“No overlay widget found”

9nvpjoqh  于 2023-04-07  发布在  Flutter
关注(0)|答案(1)|浏览(479)
E/flutter (12906): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: No Overlay widget found.
E/flutter (12906): Some widgets require an Overlay widget ancestor for correct operation.
E/flutter (12906): The most common way to add an Overlay to an application is to include a MaterialApp, CupertinoApp or Navigator widget in the runApp() call.
E/flutter (12906): The context from which that widget was searching for an overlay was:
E/flutter (12906):   MyApp
E/flutter (12906): #0      Overlay.of.<anonymous closure>
overlay.dart:384
E/flutter (12906): #1      Overlay.of
overlay.dart:387
E/flutter (12906): #2      ToastView.createView
toast.dart:97
E/flutter (12906): #3      Toast.show
toast.dart:68

我的主 dart

import 'package:flutter/material.dart';
import 'package:minwentaryzacja/screens/loginForm.dart';
import 'package:minwentaryzacja/screens/signupForm.dart';
import 'package:toast/toast.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    ToastContext().init(context);
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'm-Inwentaryzacja',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        primaryColor: Colors.white,
        accentColor: Colors.black,
        backgroundColor: Color.fromARGB(255, 0, 36, 66),
        buttonTheme: ButtonThemeData(
          buttonColor: Color.fromARGB(255, 190, 2, 58),
        ),
      ),
      home: const LoginForm(title: 'm-Inwentaryzacja'),
      routes: {
        SignUpForm.routeName: (ctx) => SignUpForm(),
      },
    );
  }
}

我的toastHelper.dart

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

alertDialog(BuildContext context, String msg) {
  Toast.show(msg, duration: Toast.lengthLong, gravity: Toast.bottom);
}

我的登录表单.dart

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:minwentaryzacja/common/textFormField.dart';
import 'package:minwentaryzacja/common/toastHelper.dart';
import 'package:minwentaryzacja/databaseHandler/dbHelper.dart';
import 'package:minwentaryzacja/screens/signupForm.dart';

class LoginForm extends StatefulWidget {
  const LoginForm({super.key, required this.title});

  // static const routeName = '/loginPange';

  final String title;

  @override
  State<LoginForm> createState() => _LoginFormState();
}

class _LoginFormState extends State<LoginForm> {
  bool _obscureText = true;

  final _formKey = new GlobalKey<FormState>();

  final _conUserId = TextEditingController();
  final _conPassword = TextEditingController();
  var dbHelper;

  @override
  void initState() {
    super.initState();
    dbHelper = DbHelper();
  }

  void login() {
    final form = _formKey.currentState;
    String uid = _conUserId.text;
    String password = _conPassword.text;
    // String passwordR = _conPasswordR.text;

    if (uid.isEmpty) {
      alertDialog(context, "Wpisz nazwę Użytkownika");
    } else if (password.isEmpty) {
      alertDialog(context, "Podaj Hasło");
    }
  }

  void _toggle() {
    setState(() {
      _obscureText = !_obscureText;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
          image: DecorationImage(
              image: AssetImage("assets/images/background.png"),
              fit: BoxFit.cover)),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        // appBar: AppBar(
        //   title: Text(widget.title),
        // ),
        body: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          child: Container(
            width: double.infinity,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                SizedBox(
                  height: 30.0,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    // Expanded(
                    //     child: SizedBox(
                    //   width: 10.0,
                    // )),
                    Padding(
                      padding: EdgeInsets.only(left: 20, top: 20),
                      child: Image.asset(
                        "assets/images/logo.png",
                        width: 100,
                      ),
                    ),
                    Expanded(
                        child: SizedBox(
                      width: 10.0,
                    )),
                  ],
                ),
                SizedBox(
                  height: 80.0,
                ),
                Text(
                  "Inwentaryzacja",
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Theme.of(context).primaryColor,
                      fontSize: 45),
                ),
                SizedBox(
                  height: 70.0,
                ),
                Text(
                  "Logowanie",
                  style: TextStyle(
                      fontWeight: FontWeight.normal,
                      color: Theme.of(context).primaryColor,
                      fontSize: 25.0),
                ),
                SizedBox(
                  height: 10,
                ),
                getTextFormField(
                  controller: _conUserId,
                  hintName: "Użytkownik",
                  icon: Icons.person,
                ),
                SizedBox(
                  height: 10,
                ),
                // getTextFormField(
                //   controller: _conUserId,
                //   hintName: "Hasło",
                //   icon: Icons.person,
                //   isobscureText: true,
                // ),
                Container(
                  margin: EdgeInsets.only(left: 50, right: 50),
                  child: TextFormField(
                    obscureText: _obscureText,
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(20)),
                            borderSide: BorderSide(color: Colors.transparent)),
                        focusedBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.all(Radius.circular(20)),
                        ),
                        prefixIcon: Icon(Icons.lock),
                        suffixIcon: IconButton(
                          onPressed: _toggle,
                          icon: Icon(_obscureText
                              ? Icons.visibility
                              : Icons.visibility_off),
                        ),
                        hintText: "Hasło",
                        fillColor: Colors.grey[200],
                        filled: true),
                  ),
                ),
                Container(
                    width: double.infinity,
                    margin: EdgeInsets.only(top: 30, left: 50, right: 50),
                    child: TextButton(
                        child: Text(
                          "Zaloguj",
                          style:
                              TextStyle(color: Theme.of(context).primaryColor),
                        ),
                        onPressed: login,
                        style: TextButton.styleFrom(
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                          foregroundColor: Theme.of(context).primaryColor,
                          padding: const EdgeInsets.all(20.0),
                          backgroundColor:
                              // Theme.of(context).colorScheme.primary,
                              Theme.of(context).backgroundColor,
                        ))),
                Container(
                    width: double.infinity,
                    margin: EdgeInsets.only(top: 10, left: 50, right: 50),
                    child: TextButton(
                      child: Text(
                        "Przejdź do rejestracji",
                        style: TextStyle(color: Theme.of(context).primaryColor),
                      ),
                      onPressed: () {
                        Navigator.of(context).pushNamed(SignUpForm.routeName);
                      },
                    )),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

我的textFormField.dart

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:minwentaryzacja/common/toastHelper.dart';

class getTextFormField extends StatelessWidget {
  TextEditingController controller;
  String hintName;
  IconData icon;
  bool isobscureText;

  getTextFormField(
      {required this.controller,
      required this.hintName,
      required this.icon,
      this.isobscureText = false});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 10, left: 50, right: 50),
      child: TextFormField(
        controller: this.controller,
        obscureText: isobscureText,
        validator: (value) {
          if (value == null || value.isEmpty) {
            return "Wprowadź $hintName";
          }
          if (hintName == "Hasło" && value.length < 6) {
            return 'Hasło musi zawierać wiecej niż 6 znakoów';
          }
          return null;
        },
        // validator: (val) => (val == null) ? "Proszę wprowadź $hintName" : null,
        // onSaved: (val) => controller.text = val!,
        decoration: InputDecoration(
            errorStyle:
                TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
            border: InputBorder.none,
            enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(20)),
                borderSide: BorderSide(color: Colors.transparent)),
            focusedBorder: OutlineInputBorder(
              borderRadius: BorderRadius.all(Radius.circular(20)),
            ),
            prefixIcon: Icon(icon),
            hintText: hintName,
            // labelText: hintName,
            labelStyle: TextStyle(color: Theme.of(context).accentColor),
            fillColor: Colors.grey[200],
            filled: true),
      ),
    );
  }
}

我在显示toastHelper时遇到问题。我遇到一个错误,我不知道如何修复。依赖项:flutter:sdk:flutter path_provider:^2.0.14 sqflite:^2.2.6吐司:0.2.9
when my input(uid.isEmpty)I have Error when i click Login“alertDialog(context,“Podaj Hasło”);“不工作。不显示通知窗口

iqjalb3h

iqjalb3h1#

你的主 dart 应该像

import 'package:flutter/material.dart';
import 'package:minwentaryzacja/screens/loginForm.dart';
import 'package:minwentaryzacja/screens/signupForm.dart';
import 'package:toast/toast.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    ToastContext().init(context);
    return OverlaySupport.global(
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'm-Inwentaryzacja',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          primaryColor: Colors.white,
          accentColor: Colors.black,
          backgroundColor: Color.fromARGB(255, 0, 36, 66),
          buttonTheme: ButtonThemeData(
            buttonColor: Color.fromARGB(255, 190, 2, 58),
          ),
        ),
        home: const LoginForm(title: 'm-Inwentaryzacja'),
        routes: {
          SignUpForm.routeName: (ctx) => SignUpForm(),
        },
      ),
    );
  }
}

相关问题