Hello how can I make 2 buttons which will update the body content like tabs. When you click on 1st button it will be shown as selected and the content will change (appBar title, body, slider etc.). When you click to another one It will be shown as selected and will change the content again. But the buttons will appear in both contents. like in this example below
There are look like tabs but the difference is they are changing state and updating the page
Simplest way is to create a global variable which holds the value of the button selected.
Put it out of the main() method.
You can access it from every class and file in your project.
Other ways would require a Provider and state management architecture.
Working sample:
import 'package:flutter/material.dart';
int selectedButton = 0;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Page1(),
);
}
}
class Page1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Column(children: <Widget>[
Text('Page1'),
MyWidget(selectedButton),
])));
}
}
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Column(children: <Widget>[
Text('Page2'),
MyWidget(selectedButton),
])));
}
}
class MyWidget extends StatelessWidget {
final int selected;
MyWidget(this.selected);
@override
Widget build(BuildContext context) {
return Row(children: <Widget>[
RawMaterialButton(
child: Text("Go to page1"),
fillColor: selected == 0 ? Colors.red : Colors.grey,
onPressed: () {
selectedButton = 0;
Navigator.push(
context, MaterialPageRoute(builder: (context) => Page1()));
}),
RawMaterialButton(
child: Text("Go to page2"),
fillColor: selected == 1 ? Colors.red : Colors.grey,
onPressed: () {
selectedButton = 1;
Navigator.push(
context, MaterialPageRoute(builder: (context) => Page2()));
})
]);
}
}