To address the problem of flexible formation and safety control for multi-UAV swarms in complex obstacle environments, a stress-matrix-based distributed model predictive control (DMPC) method is proposed. The method aims to improve the maneuverability of conventional rigid formations and enable the distributed enforcement of formation, obstacle-avoidance, and inter-UAV collision-avoidance constraints. First, the stress matrix and affine localizability conditions are used to generate the desired positions of followers from the states of multiple leaders, enabling flexible affine transformations of the swarm, including translation, rotation, scaling, and shearing. Second, control barrier function (CBF) safety constraints with dynamic decay rates are introduced and embedded into the local DMPC problems to adaptively balance safety margins and obstacle-avoidance maneuverability. Then, compatibility CBF constraints are constructed based on assumed state sequences to decouple cooperative collision-avoidance constraints and achieve synchronous distributed optimization. Finally, numerical simulations validate the effectiveness of the proposed method in achieving flexible formation and safe obstacle avoidance in scenarios involving confined-space traversal and dynamic obstacles.