John McCain — the Vietnam war hero and maverick Republican who rose through the Senate’s ranks over the course of four decades, becoming a two-time presidential candidate and chairman of the powerful Armed Services committee — succumbed to an aggressive brain tumor on Saturday.

He was 81.

The senator battled brain cancer for more than a year , and on Friday announced that he was discontinuing medical treatment

Since then, his family and closest friends had stayed close by him and his wife, Cindy, at their 15-acre ranch near Sedona, Arizona.

McCain had acknowledged back in July 2017, when his cancer was diagnosed, that his doctors gave him a “very poor prognosis.”

He said at the time that in addition to undergoing cancer treatment, he planned to “celebrate, with gratitude, a life well-lived.”

Still, McCain, who remained chairman of the Senate Armed Services Committee, continued to work, soldiering on for much of the following year as an influential GOP leader and counterpoint to President Trump.

Given his stage 4 cancer diagnosis, and knowing he would not run for re-election, “I can speak my mind without fearing the consequences much,” McCain explained in his memoir, “The Restless Wave, which he published in May.

“And I can vote my conscience without worry.”

In July 2017, less than two weeks after his diagnosis and a craniotomy to remove a blood clot above his left eye, he was back in DC, casting a deciding vote allowing the Senate to begin deliberations on replacing Obamacare.

On July 28, he cast another deciding vote that scuttled the so-called “skinny repeal” of Obamacare.

McCain left Washington in December, but remained a formidable political force from his ranch.

He visited by phone with political friends and former foes, including George W. Bush, who defeated him for president in 2000, and Barack Obama, who bested him in 2008.

In May, McCain led an unsuccessful Senate effort to oppose Trump’s nominee to lead the CIA, Gina Haspel, due to her role in the Bush-era waterboarding interrogations of suspected terrorists.

In July, he blasted Trump for the president’s chummy summit in Helsinki with Russian President Vladimir Putin.

“One of the most disgraceful performances by an American president in memory,” McCain tweeted.

Then, on Friday, his family announced that his end was near.

“Last summer, Senator John McCain shared with Americans the news our family already knew: he had been diagnosed with an aggressive glioblastoma, and the prognosis was serious,” his family said in a statement.

“In the year since, John has surpassed expectations for his survival. But the progress of disease and the inexorable advance of age render their verdict. With his usual strength of will, he has now chosen to discontinue medical treatment.”

McCain was born the son and grandson of Navy admirals in 1936.

He graduated from the US Naval Academy in 1958, and would serve in the Navy until 1981, when he moved to Arizona and took up politics.

While a naval aviator in the Vietnam War, his plane was shot from the sky by North Vietnamese soldiers.

Ejected from the aircraft, he landed in a lake in Hanoi — with both arms and his right knee broken.

He was held and tortured for five and a half years, including two years in solitary confinement.

Despite the excruciating pain of the beatings that came with interrogations, McCain would refuse his captors’ offer to release him.

The North Vietnamese had realized he was the son of a Naval admiral, and were eager for the publicity boost that McCain’s release would give them.

“They could have said to the [other prisoners], ‘look, you poor devils, the son of the man who is running the war has gone home and left you here. No one cares for you ordinary fellows,’” McCain recalled after his release.

He returned to the United States in March, 1973, permanently unable to raise his arms above his head despite extensive physical therapy.

He was back behind the controls of fighter planes by late 1974, and began serving as the Navy’s liaison to the Senate two years later.

He was elected to the House of Representatives in 1982, and served two terms. In 1986 he was elected to the US Senate, where he’d easily win re-election five times, including in 2016.

Along the way, he kept a special bond with the US military.

“Adventure, comradeship, education, rich experience – these things are all part of the fabric of military life, and they all matter,” he said in one of his last speeches, at an October memorial service at the Pentagon honoring 10 sailors whose lives were lost aboard the USS John S. McCain:

“But there is a unique dignity that comes from stepping forward, as a volunteer, and placing one’s life in harm’s way in commitment to a greater cause,” he said.

“There is no greater cause than the freedoms enshrined in the U.S. Constitution.”

The warship was named after his grandfather, John S. McCain, Sr., and father, John S. McCain, Jr. In a July 11 rededication ceremony, the senator officially became the ship’s third namesake.

Trump and McCain first and most famously locked horns in 2015, when the then-candidate mocked McCain for getting captured in Vietnam. “He’s not a war hero,” Trump taunted at a rally. “He was a war hero because he was captured. I like people who weren’t captured.”

Despite their acrimony, on Dec. 12, McCain issued a statement praising the president’s authorizing of some $700 billion in defense spending.

“It is the result of an open and bipartisan legislative process,” McCain said of the spending authorization — an apparent dig at the behind-closed-doors Republican tax-draft efforts.

Long a champion of the US military, campaign finance reform and the fight against pork barrel spending, what was perhaps his most famous vote came just two weeks after he underwent brain surgery in July.

McCain had seemed poised to save the GOP’s so-called “skinny” Obamacare repeal, but instead joined with two other Republicans in voting no.

In doing so, McCain saved the signature legislation of President Obama, who’d beat him in a bitter campaign for the White House in 2008. And in doing so, he stuck by his principles of open debate and bipartisanship by killing a bill that his GOP colleagues had drafted in back rooms without input from colleagues across the aisle.

“We have seen the world’s greatest deliberative body succumb to partisan rancor and gridlock,” he chided his colleagues.

He is survived by his wife, Cindy — who chairs the $300 million-a-year Hensley & Co., one of the largest Anheuser-Busch beer distributors in the US.

He is also survived by seven children from his two marriages. Meghan McCain, his daughter with Cindy, is a co-host of “The View.”