Living collective systems, such as multicellular organisms or social insect colonies, have evolved diverse strategies to dynamically allocate individuals to different tasks. These strategies range from rigid, inflexible task allocation that is not adjusted to changing circumstances to more fluid, flexible task allocation that is rapidly adjusted to the external environment. We have been wondering to what extent such differences in the flexibility of task allocation can be viewed as adaptive responses to different ecological contexts—for example, different degrees of temporal variability. Motivated by this question, we have developed a general mathematical framework to study the evolution of task allocation in dynamic environments. In my presentation, I will show how this framework can be used to identify general rules for the evolution of collective flexibility. As it turns out, collective flexibility should not necessarily be always expected to evolve, in particular when the environment changes too slowly (relative to how long tasks can be left unattended) or too quickly (relative to how rapidly task allocation can be adjusted). I will also use the framework to propose potential adaptive explanations for some puzzling empirical observations, such as seemingly unnecessary task switching under constant environmental conditions, apparent task specialization without efficiency benefits, and high levels of individual inactivity.