Kitereative Indonesia
Kitereative Indonesia

Reputation: 1217

flutter - How do position the value from the List to up?

I have a List<Address> where in the Address class there are several keys, between:

id, address, city and isPrimary.

I want to work with isPrimary, suppose that when I set isPrimary address from the list to true then that Address value positions to up (primary). how to do it?

The following is the List<Address> that I mean:

[
  { 'id': 1,
    'address': '40 Talbot Ambler, PA 19002',
    'city': 'New York',
    'isPrimary': false
  },

  { 'id': 2,
    'address': '618 Oak Valley West Deptford, NJ 08096',
    'city': 'Sydney',
    'isPrimary': true
  },

  { 'id': 3,
    'address': '8207 Gulf Ringgold, GA 30736',
    'city': 'London',
    'isPrimary': false
  },

  { 'id': 4,
    'address': '873 Ridgewood St.Romeoville, IL 60446',
    'city': 'Manchester',
    'isPrimary': false
  },
]

I want Address that is isPrimary = true to be at the top of the List, followed by a list below with isPrimary = false

So that looks like this:

[
  { 'id': 2,
    'address': '618 Oak Valley West Deptford, NJ 08096',
    'city': 'Sydney',
    'isPrimary': true
  },

  { 'id': 1,
    'address': '40 Talbot Ambler, PA 19002',
    'city': 'New York',
    'isPrimary': false
  },

  { 'id': 3,
    'address': '8207 Gulf Ringgold, GA 30736',
    'city': 'London',
    'isPrimary': false
  },

  { 'id': 4,
    'address': '873 Ridgewood St.Romeoville, IL 60446',
    'city': 'Manchester',
    'isPrimary': false
  },
]

See my code below:

class UserAddressItemList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScopedModelDescendant<MainModel>(
      builder: (context, child, model) {
        return model.isLoadingUser
            ? LoadingProgress
            : model.addressList == null
                ? NoProductFound()
                : ListView.builder(
                    physics: ClampingScrollPhysics(),
                    scrollDirection: Axis.horizontal,
                    itemCount:
                        model.addressList == null ? 0 : model.getAddressCount(),
                    itemBuilder: (context, i) {
                      var address = model.addressList[i];
                      return _buildAddressItemList(address, context);
                    },
                  );
      },
    );
  }

  Widget _buildAddressItemList(Address address, BuildContext context) {
    return Container(
        padding: EdgeInsets.symmetric(horizontal: 3),
        height: 150,
        width: 280,
        child: Card(
            // color: Colors.blueGrey,
            elevation: 2.0,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(10))),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                SizedBox(height: 15),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.only(left: 20),
                      // color: Colors.greenAccent,
                      width: 120,
                      child: Text(
                        '${address.address}\n'
                            '${address.city}',
                        style: Theme.of(context).textTheme.title.copyWith(
                            fontSize: 16,
                            fontWeight: FontWeight.w400,
                            height: 1.1),
                      ),
                    ),
                    Container(
                        padding: EdgeInsets.only(right: 15),
                        child: Icon(
                          FontAwesomeIcons.minus,
                          size: 16,
                          color: Colors.red,
                        ))
                  ],
                ),
                SizedBox(height: 5),
                Container(
                    height: 20,
                    //  color: Colors.green,
                    margin: EdgeInsets.symmetric(horizontal: 10),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: <Widget>[
                        Container(
                            padding: EdgeInsets.only(bottom: 2),
                            child: Text(
                              "Delivery Address",
                              style: Theme.of(context)
                                  .textTheme
                                  .caption
                                  .copyWith(
                                      fontSize: 12, color: Colors.grey[400]),
                            )),
                        SizedBox(width: 10),
                        Container(
                          child: Icon(FontAwesomeIcons.solidCheckCircle,
                              size: 20,
                              color: address.isPrimary
                                  ? Colors.blue[600]
                                  : Colors.grey),
                        )
                      ],
                    )),
                SizedBox(height: 5),
              ],
            )));
  }
}

Upvotes: 1

Views: 268

Answers (1)

HeavenOSK
HeavenOSK

Reputation: 804

isPrimary is not used to set the position. You should manually set the position through ScrollController.

example code:

class ListViewPage extends StatefulWidget {
  @override
  ListViewPageState createState() {
    return new ListViewPageState();
  }
}

class ListViewPageState extends State<ListViewPage> {
  ScrollController _scrollController;

  @override
  initState() {
    _scrollController = ScrollController();
    _scrollController.animateTo(
      40.0, // insert your ListItem's height.
      duration: Duration(
        milliseconds: 500,
      ),
      curve: Curves.linear,
    );
    super.initState();
  }

  Widget build(context) {
    return ListView.builder(
      controller: _scrollController,
      itemCount: 100,
      itemBuilder: (context, int index) {
        return Container(
          height: 40.0,
          width: double.infinity,
          child: Text(
            index.toString(),
          ),
        );
      },
    );
  }
}

Upvotes: 2

Related Questions