Home > OS >  Flutter: buttons not showing while emulation
Flutter: buttons not showing while emulation

Time:08-03

I try to make simple app with counter button. Everything seems allright before i try to emulate this - button do not show while emulation. Maybe problem with extends between classes?

import 'package:flutter/material.dart';

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

    class CountApp extends StatelessWidget {
    @immutable
    const CountApp({Key? key}) : super(key: key);

    @override
    Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Color.fromRGBO(255, 215, 64, 1),
          title: Text("Count is here^_^"),
          titleTextStyle: TextStyle(
              color: Colors.indigo, fontSize: 35, fontWeight: FontWeight.w700),
          centerTitle: true,
        ),
      ),
    );
  }
}

class CounterWidget extends StatefulWidget {

  @override
  _CounterWidgetState createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
  int _counter = 50;

  void _incrementCounter() {
    setState(() {
      _counter  ;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('widget.title'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'Increment',
            ),
            Text(
              '$_counter',
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add_rounded),
      ),
    );
  }
}

Can't see where is my mistake, only header showing while i trying to emulate

Can anyone say what i do wrong, please?

CodePudding user response:

You are not calling CounterWidget widget in material app

import 'package:flutter/material.dart';

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

class CountApp extends StatelessWidget {
@immutable
const CountApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
  home: CounterWidget() // this line
  ),
);}

CodePudding user response:

check two line if you add counter in appbar:

CountApp class add this one:

  home: CounterWidget()

also in CounterWidget class add this one

title: Text('widget.title ' '$_counter'), 

full code solution:

import 'package:flutter/material.dart';
    
        void main() {
        runApp(const CountApp());
        }
    
        class CountApp extends StatelessWidget {
        @immutable
        const CountApp({Key? key}) : super(key: key);
    
        @override
        Widget build(BuildContext context) {
        return MaterialApp(
          //here you add class
          home: CounterWidget()
        );
      }
    }
    
    class CounterWidget extends StatefulWidget {
    
      @override
      _CounterWidgetState createState() => _CounterWidgetState();
    }
    
    class _CounterWidgetState extends State<CounterWidget> {
      int _counter = 50;
    
      void _incrementCounter() {
        setState(() {
          _counter  ;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            //title here add your counter
            title: Text('widget.title ' '$_counter'), 
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'Increment',
                ),
                Text(
                  '$_counter',
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add_rounded),
          ),
        );
      }
    }
  • Related