flutter divider: How could i add divider between each line in my code?

前端 未结 7 1324
野趣味
野趣味 2020-12-02 12:00

How could i add divider to list? I use flatter for android. I want to add a divider between each line and I want to colorize the divider and add styles.

I tried to

相关标签:
7条回答
  • 2020-12-02 12:41

    Put your widget inside container with BoxDecoration as

    Container(
       child: YourWidgetHere(),
       decoration: BoxDecoration(
          border: Border(bottom: BorderSide(color: Colors.black26))),
    );
    
    0 讨论(0)
  • 2020-12-02 12:41

    On the flutter getting started tutorial it is covered, the solution they provide is something like this:

      body: ListView.builder(
        itemCount: _kittens.length,
        itemExtent: 60.0,
        itemBuilder: (context, i) {
            // Add a one-pixel-high divider widget before each row in theListView.
            if (i.isOdd) return new Divider(color: Colors.purple); // notice color is added to style divider
    
            return _listItemBuilder();
          },
      ),
      ...
    

    That should add the dividers taking into account the odd and even rows to do so.

    Also to color the divider pas "color" to the Divider Class:

    new Divider(color: Colors.purple);
    
    0 讨论(0)
  • 2020-12-02 12:47

    There are a number of ways to do the same thing. Let me compare them here.

    For a short static list

    Use ListTile.divideTiles

    ListView(
      children: ListTile.divideTiles( //          <-- ListTile.divideTiles
          context: context,
          tiles: [
            ListTile(
              title: Text('Horse'),
            ),
            ListTile(
              title: Text('Cow'),
            ),
            ListTile(
              title: Text('Camel'),
            ),
            ListTile(
              title: Text('Sheep'),
            ),
            ListTile(
              title: Text('Goat'),
            ),
          ]
      ).toList(),
    )
    

    For a long dynamic list

    Use ListView.separated.

    ListView.separated(
      itemCount: 100,
      itemBuilder: (context, index) {
        return ListTile(
          title: Text('$index sheep'),
        );
      },
      separatorBuilder: (context, index) {
        return Divider();
      },
    )
    

    This returns two widgets for every item, except for the last item. The separatorBuilder is used to add the divider.

    For adding a divider after the last item

    Create a custom item widget that uses a Divider or BoxDecoration.

    Using Divider

    final items = ['Horse', 'Cow', 'Camel', 'Sheep', 'Goat'];
    
    @override
    Widget build(BuildContext context) {
      return ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, index) {
          return Column(
            children: <Widget>[
              ListTile(
                title: Text(items[index]),
              ),
              Divider(), //                           <-- Divider
            ],
          );
        },
      );
    }
    

    Using BoxDecoration

    final items = ['Horse', 'Cow', 'Camel', 'Sheep', 'Goat'];
    
    @override
    Widget build(BuildContext context) {
      return ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, index) {
          return Container(
            decoration: BoxDecoration( //                    <-- BoxDecoration
              border: Border(bottom: BorderSide()),
            ),
            child: ListTile(
              title: Text(items[index]),
            ),
          );
        },
      );
    }
    

    Both Divider and BoxDecoration are customizable as far as the line height and color go. Divider also has an indent option, but you could get a BoxDecoration to do the same thing with some padding.

    For more style

    Use a Card

    final items = ['Horse', 'Cow', 'Camel', 'Sheep', 'Goat'];
    
    @override
    Widget build(BuildContext context) {
      return ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, index) {
          return Card( //                           <-- Card
            child: ListTile(
              title: Text(items[index]),
            ),
          );
        },
      );
    }
    
    0 讨论(0)
  • 2020-12-02 12:48

    following this Just add Divider() :

             Column(
                    children: <Widget>[
                      Container(
                        padding: EdgeInsets.all(16.0),
                        child: Column(
                          children: <Widget>[
                            Image.network(video["imageUrl"]),
                            Container(
                              height: 6.0,
                            ),
                            Text(
                              video["name"],
                              textScaleFactor: 1.05,
                              style: TextStyle(fontWeight: FontWeight.bold),
                            ),
                          ],
                        ),
                      ),
                      Divider(
                        color: Theme.of(context).primaryColor,
                      )
                    ],
                  );                                                                           
    
    0 讨论(0)
  • 2020-12-02 12:52

    Thats another way usig Container.

     ListTile(
            leading: Icon(Icons.home),
            title: Text('Home'),
     ),
     Container(height: 1, color: Colors.grey), //divider
     ListTile(
            leading: Icon(Icons.logout),
            title: Text('Logout'),
     ),
    
    0 讨论(0)
  • 2020-12-02 13:01

    The most correct way is to use ListView.separated

    ListView.separated(
         itemCount: 25,
         separatorBuilder: (BuildContext context, int index) => Divider(height: 1),
         itemBuilder: (BuildContext context, int index) {
           return ListTile(
             title: Text('item $index'),
           );
         },
    );
    
    0 讨论(0)
提交回复
热议问题