refactor createWidget function.