When being flexible matters: ecological underpinnings for the evolution of collective flexibility and task allocation