skip to Main Content

Hi I’m trying to create button for my App using flutter but isn’t working I used

floatingActionButton: FloatingActionButton(
Child:Text('click'),
)

‘ this is the code that I used but still not working

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text('Waybill App'),
        centerTitle: true,
      ),
      body: Center(
        child: Text('Welcome to Waybill Me'),
      ),
    floatingActionButton: FloatingActionButton(
      child: Text('click'),
    ),
     ),
      ),
    ),
  ));
}

2

Answers


  1. You did not add onPressed argument to your FloatingActionButton() widget, which is required! try this:

    create a custom view widget like this :

    import 'package:flutter/material.dart';
    
    class CustomView extends StatelessWidget {
      const CustomView({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Waybill App'),
            centerTitle: true,
          ),
          body: const Center(
            child: Text('Welcome to Waybill Me'),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              /*implement your own logic here,
                 what do you want to happen when the user taps on this button
                you can do navigating / 
                           showing Dialogs /
                           sending and fetching data /
                           changing the state of the widget ... */
            },
            child: const Text('Tap'),
          ),
        );
      }
    }
    

    and now you can easily pass your CustomView widget to wherever you want to use it; for instance, lets change your code to this :

    import 'package:flutter/material.dart';
    import 'package:stack_overflow_questions/new.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter ShowCase',
          theme: ThemeData(
            primaryColor: Colors.blue,
          ),
          debugShowCheckedModeBanner: false,
          //pass to the CustomView to the "home:" in here 
          home: const CustomView(),
        );
      }
    } 
    
    Login or Signup to reply.
  2. In your code please add onPressed. Like this

    floatingActionButton: FloatingActionButton(
      onPressed: () {
        print('click button');
        }
      child: Text('click'),
    ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search