简体   繁体   English

水平列表视图 flutter 上的圆角末端

[英]Rounded ends on horizontal listview flutter

do you know if there is any way i could make my listview ends rounded?你知道有什么办法可以让我的列表视图结束吗? Ive tried wraping the listview builder in a container with box decoration, borderadius and it did not work.我尝试将 listview 构建器包装在一个带有框装饰的容器中,borderadius 并且它不起作用。 Any help is appreciated.任何帮助表示赞赏。

How it looks like: https://i.stack.imgur.com/Jr4WJ.png它的样子: https://i.stack.imgur.com/Jr4WJ.png

my code:我的代码:

Padding(
                    padding: EdgeInsets.only(left: 5.0),
                    child: Container(
                      color: null,
                      child: ListView.builder(
                        scrollDirection: Axis.horizontal,
                        itemCount: profiles[0].skills.length,
                        itemBuilder: (BuildContext context, int index) {
                          return Container(
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(15)),
                            child: GestureDetector(
                              behavior: HitTestBehavior.translucent,
                              onTap: () {
                                setState(() {
                                  _filterselectedIndex = index;
                                });
                                print(index);
                              } //setState(() => _filterselected = index)
                              ,
                              child: Align(
                                alignment: Alignment.centerRight,
                                child: Container(
                                  margin: EdgeInsets.all(10.0),
                                  height: 32,
                                  //width: 100,
                                  decoration: BoxDecoration(
                                    color: _filterselectedIndex == index
                                        ? Theme.of(context).accentColor
                                        : Colors.white,
                                    borderRadius:
                                        BorderRadius.circular(15.0),
                                  ),
                                  child: Padding(
                                    padding: EdgeInsets.only(
                                      left: 20,
                                      right: 20,
                                    ),
                                    child: Align(
                                      alignment: Alignment.center,
                                      child: Text(
                                        profiles[0].skills[index],
                                        style: TextStyle(
                                          fontSize: 16.0,
                                          fontWeight: FontWeight.w600,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                          );
                        },
                      ),
                    ))

Yes, there are multiple ways.是的,有多种方法。 The easiest way i think would be using containers.我认为最简单的方法是使用容器。 Wrap your widget with a container widget.用容器小部件包装您的小部件。 Then add decoration to it like the code below:然后像下面的代码一样添加装饰:

return ListView.builder(
    itemBuilder: (context, position) {
      return Container(
          decoration: BoxDecoration(
              color: Colors.black,
              shape: BoxShape.rectangle,
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(25.0),
                  bottomRight: Radius.circular(25.0))),

            child: Paste your existing code. 
))

You can also do it using ClipRRect .您也可以使用ClipRRect来做到这一点。

There are two ways of doing this,有两种方法可以做到这一点,

First is using Container and second is ClipRRect首先是使用Container ,其次是ClipRRect

*Note: If you will use Container the widgets inside it will flow out if your BorderRadius will be a large number. *注意:如果您将使用Container ,如果您的BorderRadius很大,则其中的小部件将流出。 But in ClipRRect this will not happen, it will stay inside.但是在ClipRRect中这不会发生,它会留在里面。

See the code:见代码:

Using Container使用容器

Container(
  height: 200.0,
  decoration: BoxDecoration(
    borderRadius: BorderRadius.all(Radius.circular(50.0))
  ),
  child: ListView.builder(
    scrollDirection: Axis.horizontal,
    itemBuilder: (context, index) {
      return Padding(
        padding: const EdgeInsets.all(8.0),
        child: Container(
          color: Colors.blue,
          width: 150.0,
        ),
      );
    }, 
    itemCount: 10
  ),
),

Using ClipRRect使用 ClipRRect

SizedBox(
  height: 200.0,
  child: ClipRRect(
    borderRadius: BorderRadius.all(Radius.circular(50.0)),
      child: ListView.builder(
      scrollDirection: Axis.horizontal,
      itemBuilder: (context, index) {
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: Container(
            color: Colors.blue,
            width: 150.0,
          ),
        );
      }, 
      itemCount: 10
    ),
  ),
)

If I am correctly understanding your question you want to want to round the corner of yours listView, SO there are various method of doing that, One method is Wrapping the Listview in Container and then in decoration giving it borderRadous.如果我正确理解您的问题,您想要解决您的 listView 的问题,那么有多种方法可以做到这一点,一种方法是将 Listview 包装在 Container 中,然后在装饰中为其赋予borderRadous。

I have implemented the same functionality for you.我已经为您实现了相同的功能。

here is the code.这是代码。

return Scaffold(
      appBar: AppBar(
        title: Text("App Bar"),
      ),
      body: Container(
        padding: EdgeInsets.all(26),
        decoration: BoxDecoration(
            color: Colors.grey,
            borderRadius: BorderRadius.circular(
                16) // <- incease the value for more rounded corner
            ),
        child: ListView(
          children: <Widget>[
            Text("List Item 1"),
            Divider(),
            Text("List Item 1"),
            Divider(),
            Text("List Item 1"),
            Divider(),
            Text("List Item 1"),
            Divider(),
          ],
        ),
      ),
    );

I have a clone of app where I have implemented this type of listview with greater functionality like gestures and divider and other.我有一个应用程序的克隆,我在其中实现了这种类型的列表视图,具有更大的功能,如手势和分隔符等。 ( https://github.com/atul-chaudhary/country_dairy/blob/master/lib/user_func/user_menu.dart ) https://github.com/atul-chaudhary/country_dairy/blob/master/lib/user_func/user_menu.dart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM