Giter Club home page Giter Club logo

Comments (3)

huycozy avatar huycozy commented on June 27, 2024

Hi @majornuts
For the issue to be workable, it needs to be reproducible with a completed and minimal reproducible code sample that doesnโ€™t include 3rd party plugins or complex production code. Please provide such a sample. Thanks!

from flutter.

majornuts avatar majornuts commented on June 27, 2024

some how I cannot reproduce the exact error again, but the closes I've come is as follows.

import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FloatingActionButtonExample(),
    );
  }
}

class FloatingActionButtonExample extends StatefulWidget {
  const FloatingActionButtonExample({super.key});

  @override
  State<FloatingActionButtonExample> createState() =>
      _FloatingActionButtonExampleState();
}

class _FloatingActionButtonExampleState
    extends State<FloatingActionButtonExample> {
  // The FAB's foregroundColor, backgroundColor, and shape
  static const List<(Color?, Color? background, ShapeBorder?)> customizations =
  <(Color?, Color?, ShapeBorder?)>[
    (null, null, null), // The FAB uses its default for null parameters.
    (null, Colors.green, null),
    (Colors.white, Colors.green, null),
    (Colors.white, Colors.green, CircleBorder()),
  ];
  int index = 0; // Selects the customization.

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('FloatingActionButton Sample'),
      ),
      body: const Center(child: Text('Press the button below!')),
      floatingActionButton: buildFloatingActionButton(context),
    );
  }
}

buildFloatingActionButton(BuildContext context) {
  Icon icon = const Icon(Icons.add, color: Colors.white);
  return FloatingActionButton(
      backgroundColor: const Color.fromRGBO(82, 170, 94, 1.0),
      child: icon,
      onPressed: () {
        showBottomSheet(
            context: context,
            builder: (BuildContext context) {
              return Container(
                  height: 200,
                  color: Colors.amber,
                  child: Row(
                    children:[
                      Text('Add member'),
                    ]..addAll(MembersList.map((e) => ListTile(
                      onTap: () {
                        print(e);
                      },
                      title: Text(e),
                    )).toList()
                    ),
                  ));
            });
      });
}
List<String> MembersList = <String>['Bob', 'Alies'];

this produces the following error, witch is strange because it runs and crashs when the fab is clicked with the following:

======== Exception caught by gesture ===============================================================
The following assertion was thrown while handling a gesture:
No Scaffold widget found.

FloatingActionButtonExample widgets require a Scaffold widget ancestor.
The specific widget that could not find a Scaffold ancestor was: FloatingActionButtonExample
  state: _FloatingActionButtonExampleState#af236
The ancestors of this widget were: 
  : MaterialApp
    state: _MaterialAppState#86b49
  : FloatingActionButtonExampleApp
  ...

Typically, the Scaffold widget is introduced by the MaterialApp or WidgetsApp widget at the top of your application widget tree.

When the exception was thrown, this was the stack: 
#0      debugCheckHasScaffold.<anonymous closure> (package:flutter/src/material/debug.dart:139:7)
#1      debugCheckHasScaffold (package:flutter/src/material/debug.dart:150:4)
#2      showBottomSheet (package:flutter/src/material/bottom_sheet.dart:1348:10)
#3      buildFloatingActionButton.<anonymous closure> (package:duty_planer/main3.dart:56:9)
#4      _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1171:21)
#5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:344:24)
#6      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:652:11)
#7      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:309:5)
#8      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:242:7)
#9      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:696:9)
#10     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:98:12)
#11     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:143:9)
#12     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:633:13)
#13     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:141:18)
#14     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:127:7)
#15     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:495:19)
#16     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:475:22)
#17     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:425:11)
#18     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:420:7)
#19     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:383:5)
#20     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:330:7)
#21     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:299:9)
#22     _invoke1 (dart:ui/hooks.dart:328:13)
#23     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:442:7)
#24     _dispatchPointerDataPacket (dart:ui/hooks.dart:262:31)
Handler: "onTap"
Recognizer: TapGestureRecognizer#fba88
  debugOwner: GestureDetector
  state: possible
  won arena
  finalPosition: Offset(748.5, 1205.9)
  finalLocalPosition: Offset(20.5, 29.9)
  button: 1
  sent tap down
====================================================================================================

from flutter.

huycozy avatar huycozy commented on June 27, 2024

You can use a scaffoldKey as this solution or use showModalBottomSheet instead. Then you may hit BoxConstraints forces an infinite width error, it's because you are putting too many widgets in a Row on bottom sheet builder (looks like it should be a Column instead). Try the updated sample code as a worked one:

Sample code
import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FloatingActionButtonExample(),
    );
  }
}

class FloatingActionButtonExample extends StatefulWidget {
  const FloatingActionButtonExample({super.key});

  @override
  State<FloatingActionButtonExample> createState() => _FloatingActionButtonExampleState();
}

final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

class _FloatingActionButtonExampleState extends State<FloatingActionButtonExample> {
  // The FAB's foregroundColor, backgroundColor, and shape
  int index = 0; // Selects the customization.

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      appBar: AppBar(
        title: const Text('FloatingActionButton Sample'),
      ),
      body: const Center(child: Text('Press the button below!')),
      floatingActionButton: buildFloatingActionButton(context),
    );
  }
}

buildFloatingActionButton(BuildContext context) {
  Icon icon = const Icon(Icons.add, color: Colors.white);
  return FloatingActionButton(
      backgroundColor: const Color.fromRGBO(82, 170, 94, 1.0),
      child: icon,
      onPressed: () {
        scaffoldKey.currentState?.showBottomSheet((BuildContext context) {
          return Container(
              height: 200,
              color: Colors.amber,
              child: Column(
                children: [
                  Text('Add member'),
                ]..addAll(MembersList.map((e) => ListTile(
                      onTap: () {
                        print(e);
                      },
                      title: Text(e),
                    )).toList()),
              ));
        });
      });
}

List<String> MembersList = <String>['Bob', 'Alies'];

Closing the issue because it is more suitable to ask on community channels. Please see flutter's community for resources and ask questions like this. You may also get some help if you post it on Stack Overflow and flutterhelp.

from flutter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.