Let $X$ be a space of homogeneous type and $L$ be a nonnegative self-adjoint operator on $L^2(X)$ satisfying Gaussian upper bounds on its heat kernels. In this paper we develop the theory of weighted Besov spaces $dot{B}^{alpha,L}_{p,q,w}(X)$ and weighted Triebel--Lizorkin spaces $dot{F}^{alpha,L}_{p,q,w}(X)$ associated to the operator $L$ for the full range $0<p,qle infty$, $alphain mathbb R$ and $w$ being in the Muckenhoupt weight class $A_infty$. Similarly to the classical case in the Euclidean setting, we prove that our new spaces satisfy important features such as continuous charaterizations in terms of square functions, atomic decompositions and the identifications with some well known function spaces such as Hardy type spaces and Sobolev type spaces. Moreover, with extra assumptions on the operator $L$, we prove that the new function spaces associated to $L$ coincide with the classical function spaces. Finally we apply our results to prove the boundedness of the fractional power of $L$ and the spectral multiplier of $L$ in our new function spaces.