Sen. Jeff Flake (R-Ariz.) in the Russell Senate Office Building on Oct. 24. (Matt McClain/The Washington Post)

Jeff Flake's mind — and President Trump's leadership — have been taking the Arizona senator in opposite directions all year.

Flake said he found himself appalled, again and again, by the president's boorish behavior and penchant for picking personal fights — so much so that he felt compelled to publicly criticize Trump directly, in the press and in a best-selling book.

Flake could see the beginnings of political damage during the 2016 presidential campaign, when he first raised doubts about Trump. His decision to retire when his term ends next year, which he announced Tuesday in a dramatic floor speech, came after reams of polling data showed he would lose decisively in a Republican primary battle next year, when he probably would have faced a pro-Trump challenger.

But ultimately, Flake said, there was not a single trigger for the decision. If anything, he held on because he is a strong supporter of most of Trump's policies and personnel decisions. He voted for his judicial nominees, his regulatory rollbacks and the GOP health-care plan.

"I knew that when I spoke out at that time that I was out of step with a lot of the Republican primary voters, but I felt that I had to do it," he said in an interview. "I had hoped — and I still hope and I'm confident at some point — that the fever will break. But it just became more and more apparent that it certainly wasn't going to break by next year."

Even if he survived the primary, neither Flake nor others in his orbit deny that the same polling showed he might also have lost the general election to his presumed Democratic opponent, popular Phoenix-area congresswoman Kyrsten Sinema.

But it was his disgust with Trump's behavior that proved decisive in his decision to call it quits.

"I couldn't sleep at night having to embrace the president or condoning his behavior or being okay with some of his positions," he said. "I just couldn't do it — it was never in the cards."

Flake's retirement is not directly tied to the feud between Republican Party leaders and former White House adviser Stephen K. Bannon — who is waging a populist "war" against the GOP establishment — but he is a battlefield casualty. The fight he picked with Trump followed years of cooperation with Democrats on immigration policy, global trade deals and reestablishing diplomatic ties to Cuba. Such cooperation is unacceptable to Arizona's most die-hard Republicans.

"Democrats are saying, 'See, the Republican Party is falling apart.' Then there's the Breitbart Trump people looking at this and thinking, 'Donald Trump is pushing these RINOs out,' " said Jarrod White, a longtime party activist who was an Arizona delegate to the Republican National Convention last year, referring to "Republicans in Name Only." "The truth of the matter is, Jeff Flake is down by double digits in the polls against Kelli Ward. He knows he's not going to win. I think he chose to quit and be 'dignified' and get his final lashes out at Trump."

Ward is a former Republican state senator who unsuccessfully challenged Sen. John McCain (R-Ariz.) last year, accusing him of being past his prime and insufficiently conservative. Just days after the presidential election ended last year, Ward launched her campaign against Flake — raising similar concerns about his conservative bona fides. The attacks worked. For now, she's the only GOP Senate candidate.

Sen. Jeff Flake (R-Ariz.) walks with his wife, Cheryl, after his surprise retirement announcement, on Oct. 24, 2017, in the Russell Senate Office Building. (Matt McClain/The Washington Post)

Ward and her aides did not return multiple requests for comment. But Doug McKee, the head of KelliPAC — a group independent of her campaign that first launched during her 2016 bid against McCain — said Ward is drawing attention because Flake "seems to be a lot more in step with what the Democrats' views are as opposed to what the Arizona Republican Party is looking for."

A GOP strategist who's reviewed polling in the race concluded that in Arizona and elsewhere, "the attacks on Trump are totally unsustainable for anyone. You don't have to be best friends with the guy, but you can't do what Flake did."

"The old rules still apply — local issues matter," said the strategist, who asked for anonymity to speak frankly about Republican dynamics, "but the Trump image is the new preeminent test of whether you satisfy the desires of Republican conservative primary voters."

Flake's public contortions were apparent throughout the year.

In a March interview, he praised Trump's pick of Neil M. Gorsuch to serve as a U.S. Supreme Court justice. A conservative jurist from the West was exactly what Flake wanted to see on the high court. He was also impressed by how the White House introduced Gorsuch to the country — in a crisply delivered prime-time address by Trump with GOP senators in the White House East Room to meet the nominee and sing his praises to reporters afterward.

"It was well done, it really was, both in style and in substance — it's a really good pick," Flake said at the time.

The balancing act and overtures continued in May, when he told constituents back home that he looked forward to working with Trump on confirming other conservative picks to run federal agencies.

"The president has assembled a pretty good Cabinet. He's surrounded himself with a good group of people," Flake said during a tour of a nuclear power plant, citing picks for regulatory agencies in particular.

But under questioning by a voter at an event in Glendale, Ariz., he said "I have a hard time believing" that a GOP-controlled Congress could fulfill Trump's wish to quickly pass health-care reform. When a hospital employee asked about how to save the Medicaid program, Flake said, "We're trying to find that balance, and we aren't close yet, frankly."

But Flake voted for the GOP health-care reform plan, even as McCain famously voted no.

Then Congress went home for August recess and Flake published his best-selling treatise, "Conscience of a Conservative," which leveled a blistering attack on Trump. The president responded by relentlessly going after the senator on Twitter. He then announced plans to visit Phoenix for a campaign-style event with speculation building that Trump could endorse Ward or another Flake challenger.

"It wasn't the Trump rally, but it was two weeks of media coverage leading up to that rally, where the media said 'Trump is going to come out and bash Flake and endorse someone else,'" said a Flake confidant, who asked for anonymity to maintain his close relationship with the senator. "It was millions of dollars in earned media that basically said, 'Jeff Flake is going to get roasted by Trump.' And then he came out here and that didn't happen."

Frosty Taylor, a Republican activist and the editor of a daily email newsletter for thousands of Republicans in Maricopa County, Ariz., warned at the time that most of her readers were "disenchanted" with Flake.

"They want him replaced. They're just not happy with him," she said in August. "He's not following the way they want him to vote. He's voting with Democrats on a lot of issues. He's following John McCain's lead on a lot of stuff — that's a problem here for a lot of people."

Flake's retirement announcement Tuesday came just minutes after Trump left a luncheon with Republican senators. He had talked about the race generally with Senate Majority Leader Mitch McConnell (R-Ky.), but Flake said he didn't give him much advanced warning. He also didn't call his longtime friend and ideological brother-in-arms, Vice President Pence, with whom he once served in the House.

"I didn't want to put him in a bad position of knowing that and not sharing it. He's a trusted friend," Flake said of Pence — but the two haven't spoken since the announcement.

Flake warned that while his criticism of the president helped lead to his downfall, GOP candidates should also be wary of embracing him too tightly.

Any Republican who "simply aligns themselves for the purpose of the primary with the president on every issue and basically contracts out any thinking on policy issues, and is willing to condone any behavior that the president exhibits — I think they put themselves at great risk in a general election," he said.

Now free of campaign politics for the first time in 18 years, Flake is focused on how to make his last months in office meaningful while enjoying his newfound political freedom.

"I don't have any fundraising calls to make," he said. "That in itself is a pretty good feeling."