AppBar with a ListView (Flutter)

0

I'm a big beginner with Flutter and I followed a tutorial to make a ListView with pictures. However, I'm a bit lost in the code and I don't know where to implement my AppBar. I've already tried several times but never succeeded. I hope you'll be able to help me, here's the code:

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../recyclerview/data.dart';


class ListViewExample extends StatefulWidget {
  @override 
  State<StatefulWidget> createState() {
    return new ListViewExampleState(
    );
  }
}

class ListViewExampleState extends State<ListViewExample>{
  List<Container> _buildListItemsFromItems(){
    int index = 0;
    return item.map((item){

      var container = Container(
        decoration: index % 2 == 0?
        new BoxDecoration(color: const Color(0xFFFFFFFF)):
          new BoxDecoration(
            color: const Color(0xFFFAFAF5)
          ),
        child: new Row(
          children: <Widget>[
            new Container(
              margin: new EdgeInsets.all(5.0),
              child: new CachedNetworkImage(
                imageUrl: item.imageURL,
                width: 200.0,
                height: 100.0,
                fit: BoxFit.cover,
              ),
            ),

            new Row(
              mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
  
                new Text(
                    item.title,
                    style: new TextStyle(),
                  ),
                new Text(
                    item.description,
                  ),

                ]
            )
            

          ],
        )
      );

      index = index + 1;
      return container;
    }).toList();
  }
  @override
  Widget build(BuildContext context) {
    return new ListView(
      children: _buildListItemsFromItems(),
    );
  }
}

If you need more resources I'll send you this, Thank you.

Update: I have added the AppBar at outside of the ListView in the Scaffold

flutter
listview
dart
appbar
asked on Stack Overflow Oct 10, 2020 by Rianou • edited Nov 25, 2020 by Rianou

1 Answer

3

You have to use the AppBar in a Scaffold and not in the listview like this:

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../recyclerview/data.dart';


class ListViewExample extends StatefulWidget {
  @override 
  State<StatefulWidget> createState() {
    return new ListViewExampleState(
    );
  }
}

class ListViewExampleState extends State<ListViewExample>{
  List<Container> _buildListItemsFromItems(){
    int index = 0;
    return item.map((item){

      var container = Container(
        decoration: index % 2 == 0?
        new BoxDecoration(color: const Color(0xFFFFFFFF)):
          new BoxDecoration(
            color: const Color(0xFFFAFAF5)
          ),
        child: new Row(
          children: <Widget>[
            new Container(
              margin: new EdgeInsets.all(5.0),
              child: new CachedNetworkImage(
                imageUrl: item.imageURL,
                width: 200.0,
                height: 100.0,
                fit: BoxFit.cover,
              ),
            ),

            new Row(
              mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
  
                new Text(
                    item.title,
                    style: new TextStyle(),
                  ),
                new Text(
                    item.description,
                  ),

                ]
            )
            

          ],
        )
      );

      index = index + 1;
      return container;
    }).toList();
  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
       appBar: AppBar(),
       body: ListView(
      children: _buildListItemsFromItems(),
    ),
    );
  }
}
answered on Stack Overflow Oct 10, 2020 by Zaza.codes

User contributions licensed under CC BY-SA 3.0