skip to Main Content

I was learning flutter from the link given below,while writing my code I got the error , whenever I use appBar(), I got the error. But, when I use AppBar(), then there is no error , but also, I don’t get the results. According to the tutorials , after writing this code , I will get a text write option in the app, but, also, I am not getting my favourable outcome.

text

# here is my **home.dart** code
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_svg/svg.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(),
      body: Column(
        children: [TextField()],
      ),
    );
  }

  Scaffold appBar() {
    return Scaffold(
      appBar: AppBar(
          title: Text(
            'BreakFast',
            style: TextStyle(
                color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold),
          ),
          backgroundColor: Colors.white,
          elevation: 0.0,
          centerTitle: true,
          leading: GestureDetector(
            onTap: () {},
            child: Container(
              margin: EdgeInsets.all(10),
              alignment: Alignment.center,
              child: SvgPicture.asset('assets/icons/Arrow - Left 2.svg'),
              height: 20,
              width: 20,
              decoration: BoxDecoration(
                  color: Color(0xffF7F8F8),
                  borderRadius: BorderRadius.circular(10)),
            ),
          ),
          actions: [
            GestureDetector(
              onTap: () {},
              child: Container(
                margin: EdgeInsets.all(10),
                alignment: Alignment.center,
                width: 37,
                child: SvgPicture.asset(
                  'assets/icons/dots.svg',
                  height: 5,
                  width: 5,
                ),
                decoration: BoxDecoration(
                    color: Color(0xffF7F8F8),
                    borderRadius: BorderRadius.circular(10)),
              ),
            ),
          ]),
    );
  }
}

# here is my **main.dart** code

import 'package:demo/home.dart';
import 'package:flutter/material.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) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: ThemeData(fontFamily: 'Poppins'),
        home: HomePage());
  }
}

2

Answers


  1. The appBar property requires a AppBar. Inside AppBar there is other properties, like title, which accepts a Widget, usually it will be a Text.

    Login or Signup to reply.
  2. You can remove the Scaffold from your appBar method:

    import 'package:flutter/material.dart';
    import 'package:flutter/widgets.dart';
    import 'package:flutter_svg/svg.dart';
    
    class HomePage extends StatelessWidget {
      const HomePage({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: appBar(),
          body: Column(
            children: [TextField()],
          ),
        );
      }
    
      Widget appBar() {
        return AppBar(
              title: Text(
                'BreakFast',
                style: TextStyle(
                    color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold),
              ),
              backgroundColor: Colors.white,
              elevation: 0.0,
              centerTitle: true,
              leading: GestureDetector(
                onTap: () {},
                child: Container(
                  margin: EdgeInsets.all(10),
                  alignment: Alignment.center,
                  child: SvgPicture.asset('assets/icons/Arrow - Left 2.svg'),
                  height: 20,
                  width: 20,
                  decoration: BoxDecoration(
                      color: Color(0xffF7F8F8),
                      borderRadius: BorderRadius.circular(10)),
                ),
              ),
              actions: [
                GestureDetector(
                  onTap: () {},
                  child: Container(
                    margin: EdgeInsets.all(10),
                    alignment: Alignment.center,
                    width: 37,
                    child: SvgPicture.asset(
                      'assets/icons/dots.svg',
                      height: 5,
                      width: 5,
                    ),
                    decoration: BoxDecoration(
                        color: Color(0xffF7F8F8),
                        borderRadius: BorderRadius.circular(10)),
                  ),
                ),
              ],
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search