【问题标题】:Flutter Dismissible not working anymore, but code looks correctFlutter Dismissible 不再工作,但代码看起来正确
【发布时间】:2020-09-30 22:47:57
【问题描述】:

我一直在编写一个应用程序,它要求用户能够从列表中消除对象。我让这一切正常工作,然后突然停止工作,我不知道为什么。

我已经注释掉了几乎所有的代码 - 剩下的就是这些了,但是可忽略的 STILL 不起作用(尽管之前它工作得很好!!!!):

//... setup stuff not relevant to question

class _Screen extends State<Screen> {

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: profileCard(true),
    );

Dismissible profileCard(bool centre) {

    return Dismissible(
      key: GlobalKey(),
      background: Container(
        width: MediaQuery.of(context).size.width * 0.8,
        height: MediaQuery.of(context).size.height * 0.3,
        color: Colors.green,
      ),
      secondaryBackground: Container(
        width: MediaQuery.of(context).size.width * 0.8,
        height: MediaQuery.of(context).size.height * 0.3,
        color: Colors.red,
      ),
      child: Container(
        width: MediaQuery.of(context).size.width * 0.8,
        height: MediaQuery.of(context).size.height * 0.3,
        color: Colors.purple,
      ),
    );

//....

滑动操作只是拒绝工作根本,我正在撕扯我的头发为什么。那里有任何 Flutter 专家可以帮助我吗?

谢谢!

【问题讨论】:

  • 我将您的代码添加到我的示例项目中,它确实会滑动并关闭。它看起来有点奇怪,但解雇工作。将需要更多信息来提供更多帮助。
  • 谢谢,@BenediktJSchlegel - 但除此之外,目前文件中唯一的其他代码是一个设置布尔值的计时器。其他所有内容都被注释掉了。
  • 没有显示错误信息?在运行您的应用之前尝试运行一次flutter cleanflutter pub get

标签: flutter dart dismissible


【解决方案1】:

正如@Benedikt J Schlegel 所述,您的代码适用于Dismissible

import 'package:flutter/material.dart';
void main() {
  runApp(MaterialApp(
    home: Screen(),
  ));
}

class Screen extends StatefulWidget {
  @override
  _ScreenState createState() => _ScreenState();
}

class _ScreenState extends State<Screen> {
  Dismissible profileCard(bool centre) {
    return Dismissible(
      key: GlobalKey(),
      background: Container(
        width: MediaQuery.of(context).size.width * 0.8,
        height: MediaQuery.of(context).size.height * 0.3,
        color: Colors.green,
      ),
      secondaryBackground: Container(
        width: MediaQuery.of(context).size.width * 0.8,
        height: MediaQuery.of(context).size.height * 0.3,
        color: Colors.red,
      ),
      child: Container(
        width: MediaQuery.of(context).size.width * 0.8,
        height: MediaQuery.of(context).size.height * 0.3,
        color: Colors.purple,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Dismissible'),
        ),
        body: profileCard(true));
  }
}

【讨论】:

    【解决方案2】:

    似乎错误来自以每 44 毫秒间隔运行一个计时器并在其中设置一个布尔值。

    不知道为什么会这样,因为有问题的布尔值与可解雇无关。

    感谢您的帮助,伙计们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多