Toggle button to call container class flutter - flutter

hi this is the first time i use a toggle button i try to make the toggle button to call a class container, there are 2 button
when "folmulir" button is true it called class container 1
when "List data" button is true it called class container 2.
should i make the container in the void so i return it?
import 'package:flutter/material.dart';
final List<bool> _selecteddatamaster = <bool>[
true,
false,
];
class TestbayToggle extends StatefulWidget {
const TestbayToggle({Key? key}) : super(key: key);
#override
TestbayToogleState createState() => TestbayToogleState();
}
class TestbayToogleState extends State<TestbayToggle> {
List<bool> isSelected = [true, false];
List<Widget> datamaster = const <Widget>[
Text('Folmulir'),
Text('List Data'),
];
#override
Widget build(BuildContext context) {
return Container(
width: 1000,
height: 50,
decoration: BoxDecoration(
color: Colors.grey[400],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(8), topRight: Radius.circular(8))),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
height: 50,
decoration: const BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8), topRight: Radius.circular(8)),
),
child: ToggleButtons(
onPressed: (int index) {
setState(() {
for (int i = 0; i < _selecteddatamaster.length; i++) {
_selecteddatamaster[i] = i == index;
if (index == 0) {
}
}
});
},
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(8), topRight: Radius.circular(8)),
selectedColor: Colors.black,
fillColor: Colors.white,
renderBorder: false,
color: Colors.white,
constraints: const BoxConstraints(
minHeight: 40.0,
minWidth: 100.0,
),
isSelected: _selecteddatamaster,
children: datamaster ,
),
)
]),
);
}
}
class Container1 extends StatefulWidget {
const Container1({Key? key}) : super(key: key);
#override
State<Container1> createState() => _Container1State();
}
class _Container1State extends State<Container1> {
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(left: 8, right: 8, bottom: 8),
child: Container(
width: 1000,
height: 500,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(8), bottomRight: Radius.circular(8)),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: const [Text("test1")],
),
),
),
);
}
}
class Container2 extends StatelessWidget {
const Container2({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(left: 8, right: 8, bottom: 8),
child: Container(
width: 1000,
height: 500,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(8), bottomRight: Radius.circular(8)),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: const [Text("test2")],
),
),
),
);
}
}

I think the Visibility widget will serve your case better. Here's an example to demonstrate:
bool isSelected = true; // true by default, becuase first ToggleButton is activated
Visibility(
child: Container(), // display container of choice
visible: isSelected // will display child if true
);

Related

flutter - PageView Builder & Card - displaying different info on each card

I am trying to use PageView and Card. My objective is to display different information on the fourth cards that the user can see.
To be more precise, on one card, I could display a title for example. On the second card, I will display dates, on the third card, it could be some random text...
I hope this is clear.
To give you more color on this, the information will come from FireBase.
I do not know how to do this easily. I have considered to create 4 widget myCard, with different info on it and to use them. But I would like to do something more professional and efficient. Thank you
About FireBase, I do not use json. I have a simple document in FireBase. In this document, I have several fields, like 'price' 'quantity' 'reference'...
I just want that each card to display two or three fields from this FireBase document.
class MyBodyWindMill extends StatefulWidget {
const MyBodyWindMill({Key key}) : super(key: key);
#override
_MyBodyWindMillState createState() => _MyBodyWindMillState();
}
class _MyBodyWindMillState extends State<MyBodyWindMill> {
#override
Widget build(BuildContext context) {
return
Container(
height: 260,
child: PageView.builder(controller: PageController(viewportFraction: 0.88),
itemCount: 4,
itemBuilder: ( _, i){
return GestureDetector(
child: Container(
padding: const EdgeInsets.only(left:20, top:20),
height: 220,
width: MediaQuery.of(context).size.width-20,
margin: const EdgeInsets.only(right:10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
),
child:Column(
children: [
MyCard(),
],
))
);
},
));
}}
class MyCard extends StatefulWidget {
const MyCard( {Key key} ) : super(key: key);
#override
_MyCardState createState() => _MyCardState();
}
class _MyCardState extends State<MyCard> {
//String monText;
#override
Widget build(BuildContext context) {
return Container(
height: 230,
child: Stack(
children: [
Positioned(
top: 35,
left: 20,
child: Card(
elevation: 6.0,
shadowColor: Colors.grey,
child: Container(
height: 180,
width: 0.9,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(0.0),
boxShadow: [BoxShadow(
color: Colors.black.withOpacity(0.3),
offset: Offset(-10.0, 10.0),
blurRadius: 20.0,
spreadRadius: 4.0,
),
])
),
),
),
Positioned(
top: 0.0,
left: 30.0,
child:Card(
elevation: 10.0,
shadowColor: Colors.grey.withOpacity(0.5),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Container(
height: 200,
width: 150,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
image: DecorationImage(
fit: BoxFit.fill,
image:AssetImage('assets/header.jpg'),
),
),
))),
Positioned(
top:40,
left:200,
child:
Container(
height: 150,
width: 170,
child:Column(
crossAxisAlignment: CrossAxisAlignment.start,
children:[
Text('Title: ', style: TextStyle(fontSize:16,
color:Colors.red),),
Divider(color:Colors.black),
Text('Info 1: ', style: TextStyle(fontSize:16,
color:Colors.red),),
Text('Info 2'),
])
))
],
),
);
}
}
Based on what you're trying to do it looks like what you need is something like this widget:
class InfoBlock {
final String title;
final String description;
InfoBlock({
required this.title,
required this.description,
});
}
class MyCard extends StatefulWidget {
String title;
List<InfoBlock> infoBlocks;
MyCard({
Key? key,
required this.title,
required this.infoBlocks,
}) : super(key: key);
#override
_MyCardState createState() => _MyCardState();
}
class _MyCardState extends State<MyCard> {
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 200,
width: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
image: const DecorationImage(
fit: BoxFit.fill,
image: AssetImage('header.jpeg'),
))),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.title,
style: const TextStyle(fontSize: 30),
),
...widget.infoBlocks.map((block) {
return Text(
'${block.title}: ${block.description}',
style: const TextStyle(fontSize: 16),
);
}),
],
)
],
),
);
}
}
And then you can use it like-so:
Column(
children: [
MyCard(
title: 'Quantity',
infoBlocks: [
InfoBlock(title: 'Price', description: '100'),
],
),
MyCard(
title: 'Status',
infoBlocks: [
InfoBlock(title: 'Txt1', description: 'value1'),
InfoBlock(title: 'Txt2', description: 'value2'),
InfoBlock(title: 'Txt3', description: 'value3'),
],
),
],
));
And here's what the output would look like:
Here's a a link to Flutlab with the above code fully functional: https://flutlab.io/ide/6d8762de-50b1-4cfe-a509-a301edaacebf

Text under the button flutter

this is the code:
class DocClinVisi extends StatelessWidget {
const DocClinVisi({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (){},
child: Container(
padding: const EdgeInsets.all(20.0),
height: 110,
width: 110,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
child: Image.asset('imagens/frame-1.png'),
),
);
}
}
There are several ways to show text "outside" the button.
If you need to "float" text outside the container you can use Stack:
class DocClinVisi extends StatelessWidget {
const DocClinVisi({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {},
child: Stack(
clipBehavior: Clip.none,
children: [
Container(
padding: const EdgeInsets.all(20.0),
height: 110,
width: 110,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
),
const Positioned(
bottom: -20,
left: 0,
right: 0,
child: Text('Hello World', textAlign: TextAlign.center),
),
],
),
);
}
}
Note that this method could overlap the text with other widgets in some cases.
If you need to make the text part of the widget so it will not overlap, you could replace Stack for a Column:
class DocClinVisi extends StatelessWidget {
const DocClinVisi({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {},
child: Column(
children: [
Container(
padding: const EdgeInsets.all(20.0),
height: 110,
width: 110,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
),
const Text('Hello World', textAlign: TextAlign.center),
],
),
);
}
}
Just wrap your container with column, then set text below the container, you can see and copy paste code below:
class DocClinVisi extends StatelessWidget {
const DocClinVisi({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (){},
child: Column( // add column
children: [
Container(
padding: const EdgeInsets.all(20.0),
height: 110,
width: 110,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
child: Image.asset('imagens/frame-1.png'),
),
Text('My Button'), // then add the text
]
),
);
}
}

When I press the button, I cannot change my Text values ​with setState()

When I press the button, I want the value of my _sicaklikSeviyesi variable to change and update it to the screen with setState(), but I can't. I've been dealing with this problem for the last 2 days, I couldn't find a solution for a song. Can you help me pls?
Main Screen Codes :
import 'package:akilli_okul_sistemleri/alert_dialog.dart';
import 'package:akilli_okul_sistemleri/bottom_info_bar.dart';
import 'package:akilli_okul_sistemleri/drawer_panel.dart';
import 'package:flutter/material.dart';
class AnaMenu extends StatefulWidget {
const AnaMenu({Key? key}) : super(key: key);
#override
State<AnaMenu> createState() => _AnaMenuState();
}
class _AnaMenuState extends State<AnaMenu> {
BottomInfoBarState altPanel = BottomInfoBarState();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Akıllı Ev Sistemleri'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
altPanel.degisenDegerler();
AlertBoxDialog.alertBoxCreator(
context: context,
title: "Başlık",
desc: 'Açıklama',
imageLocation: AlertBoxDialog.yuksekSicaklikLogo);
},
child: Text("ISI UYARISI"),
),
),
drawer: DrawerPanel(),
bottomNavigationBar: BottomInfoBar(),
);
}
}
Bottom Info Bar Codes :
import 'package:flutter/material.dart';
import 'package:akilli_okul_sistemleri/ana_menu.dart';
class BottomInfoBar extends StatefulWidget {
BottomInfoBar({Key? key}) : super(key: key);
#override
BottomInfoBarState createState() => BottomInfoBarState();
}
class BottomInfoBarState extends State<BottomInfoBar> {
Color _logoRengi = Colors.white;
int _sicaklikSeviyesi = 15,
_sesSeviyesi = 15,
_hareketSeviyesi = 45,
_isikSeviyesi = 75,
_dumanSeviyesi = 20;
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(color: Colors.black38, spreadRadius: 0, blurRadius: 10),
],
),
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15), topRight: Radius.circular(15)),
child: BottomAppBar(
color: Colors.orange[300],
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_bottomContainerCreator(
Icons.local_fire_department, _sicaklikSeviyesi.toString()),
_bottomContainerCreator(
Icons.record_voice_over, "%$_sesSeviyesi"),
_bottomContainerCreator(
Icons.directions_walk, '%$_hareketSeviyesi'),
_bottomContainerCreator(Icons.light, "%$_isikSeviyesi"),
_bottomContainerCreator(Icons.cloud, "%$_dumanSeviyesi"),
],
),
),
),
);
}
_bottomContainerCreator(IconData icon, String text) {
return Container(
decoration: BoxDecoration(
color: Colors.grey[700], borderRadius: BorderRadius.circular(15.0)),
margin: EdgeInsets.all(3),
padding: EdgeInsets.all(3),
height: 60,
child: Column(
children: [
Container(
padding: EdgeInsets.all(5),
child: Icon(
icon,
size: 27,
color: _logoRengi,
),
),
Padding(
padding: const EdgeInsets.only(top: 1),
child: Text(
text,
style:
TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
),
),
],
),
);
}
void degisenDegerler() {
setState(() {
_sicaklikSeviyesi = 100;
if (_sicaklikSeviyesi < 20) {
_logoRengi = Colors.blue;
} else if (_sicaklikSeviyesi > 50) {
_logoRengi = Colors.red;
} else {
_logoRengi = Colors.yellow;
}
});
}
}
You can move the setState up the widget tree (in the AnaMenu widget). All widgets that depend on the value (in your case the color) will rebuild and update.
Try this code below for a simple function. If your bottombar doesn't require other logic. You might want to convert it to a Stateless widget
class AnaMenu extends StatefulWidget {
const AnaMenu({Key? key}) : super(key: key);
#override
State<AnaMenu> createState() => _AnaMenuState();
}
class _AnaMenuState extends State<AnaMenu> {
late Color color;
#override
void initState() {
color = Colors.blue;
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Akıllı Ev Sistemleri'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
setState(() {
color = Colors.green;
});
// AlertBoxDialog.alertBoxCreator(
// context: context,
// title: "Başlık",
// desc: 'Açıklama',
// imageLocation: AlertBoxDialog.yuksekSicaklikLogo);
},
child: Text("ISI UYARISI"),
),
),
// drawer: DrawerPanel(),
bottomNavigationBar: BottomInfoBar(color: color),
);
}
}
class BottomInfoBar extends StatefulWidget {
final Color color;
BottomInfoBar({required this.color, Key? key}) : super(key: key);
#override
BottomInfoBarState createState() => BottomInfoBarState();
}
class BottomInfoBarState extends State<BottomInfoBar> {
// Color _logoRengi = Colors.white;
late int _sicaklikSeviyesi;
late int _sesSeviyesi;
late int _hareketSeviyesi;
late int _isikSeviyesi;
late int _dumanSeviyesi;
#override
void initState() {
_sicaklikSeviyesi = 15;
_sesSeviyesi = 15;
_hareketSeviyesi = 45;
_isikSeviyesi = 75;
_dumanSeviyesi = 20;
super.initState();
}
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(color: Colors.black38, spreadRadius: 0, blurRadius: 10),
],
),
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15), topRight: Radius.circular(15)),
child: BottomAppBar(
color: Colors.orange[300],
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_bottomContainerCreator(
Icons.local_fire_department, _sicaklikSeviyesi.toString()),
_bottomContainerCreator(
Icons.record_voice_over, "%$_sesSeviyesi"),
_bottomContainerCreator(
Icons.directions_walk, '%$_hareketSeviyesi'),
_bottomContainerCreator(Icons.light, "%$_isikSeviyesi"),
_bottomContainerCreator(Icons.cloud, "%$_dumanSeviyesi"),
],
),
),
),
);
}
_bottomContainerCreator(IconData icon, String text) {
return Container(
decoration: BoxDecoration(
color: Colors.grey[700], borderRadius: BorderRadius.circular(15.0)),
margin: EdgeInsets.all(3),
padding: EdgeInsets.all(3),
height: 60,
child: Column(
children: [
Container(
padding: EdgeInsets.all(5),
child: Icon(
icon,
size: 27,
color: widget.color,
),
),
Padding(
padding: const EdgeInsets.only(top: 1),
child: Text(
text,
style:
TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
),
),
],
),
);
}
}

Flutter ListView is not displaying items

I am trying to set up a very simple ListView that from the top to the bottom of the screen, but right now I don't see any thing on the screen but a green background.
class _HomeScreenState extends State<HomeScreen> {
List wishlists = [Wishlist("Test1"), Wishlist("Yeet")];
#override
Widget build(BuildContext context) {
return Container(
color: Colors.lightGreen,
child: SizedBox(
height: 400,
child: new ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return new WishlistListWidget(
wishlist: wishlists[index],
);
},
itemCount: wishlists.length,
),
),
);
}
}
Wishlist is a simple Class and this is my WishlistListWidget:
class WishlistListWidget extends StatelessWidget {
final Wishlist wishlist;
const WishlistListWidget({Key key, this.wishlist}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
child: Row(
children: [
Container(
height: 150.0,
color: Colors.transparent,
child: new Container(
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(40.0),
bottomLeft: const Radius.circular(40.0),
)),
),
),
],
),
);
}
}
What am I missing here?
You're missing the Text widget in WishlistListWidget class.
class WishlistListWidget extends StatelessWidget {
final Wishlist wishlist; // this value is not being used here
const WishlistListWidget({Key key, this.wishlist}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
child: Row(
children: [
Container(
height: 150.0,
color: Colors.transparent,
child: new Container(
child: Text(wishlist.someProperty), // this one, it should be wishlist.someProperty
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(40.0),
bottomLeft: const Radius.circular(40.0),
),
),
),
),
],
),
);
}
}

Flutter - Button Group style and position

I am trying to create something like the attached image. I got this far ...
Expanded(
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(32),
topRight: Radius.circular(32),
),
),
child: ButtonTheme(
child: ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: () => print('hi'),
child: Text('Referals'),
color: Color(0xff2FBBF0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(15.0),
topLeft: Radius.circular(15.0)),
),
),
RaisedButton(
onPressed: () => print('hii'),
child: Text('Stats'),
color: Color(0xff2FBBF0),
),
RaisedButton(
onPressed: () => print('hiii'),
child: Text('Edit Profile'),
color: Color(0xff2FBBF0),
// color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(15.0),
topRight: Radius.circular(15.0)),
),
),
],
),
),
),
),
But I don't really feel like it will look like the image.
I would also like the button group to be at the top of the Container. Now they're in the absolute center. Just like they would be if wrapped in a Center widget.
Here's the complete code. I have just used Container and Row because I find it more suitable and easy to achieve without any headache. :P
If you want with RaisedButton, figure it out.
Source:
import 'package:flutter/material.dart';
class Demo extends StatefulWidget {
#override
_DemoState createState() => new _DemoState();
}
class _DemoState extends State<Demo> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("DEMO")),
body: Padding( // used padding just for demo purpose to separate from the appbar and the main content
padding: EdgeInsets.all(10),
child: Container(
alignment: Alignment.topCenter,
child: Container(
height: 60,
padding: EdgeInsets.all(3.5),
width: MediaQuery.of(context).size.width * 0.9,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.all(Radius.circular(15)),
),
child: Row(
children: <Widget>[
Expanded(
child: InkWell(
onTap: () {},
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(12),
topLeft: Radius.circular(12))),
child: Text("Referrals",
style: TextStyle(
color: Colors.blue,
fontSize: 17,
)),
))),
Expanded(
child: InkWell(
onTap: () {},
child: Container(
alignment: Alignment.center,
child: Text("Stats",
style: TextStyle(
color: Colors.white, fontSize: 17)),
))),
Padding(
padding: EdgeInsets.symmetric(vertical: 5),
child: Container(color: Colors.white, width: 2)),
Expanded(
child: InkWell(
onTap: () {},
child: Container(
alignment: Alignment.center,
child: Text("Edit Profile",
style: TextStyle(
color: Colors.white, fontSize: 17)),
)))
],
)),
)));
}
}
Output Screenshot:
Check my ButtonGroup widget that I created
import 'package:flutter/material.dart';
class ButtonGroup extends StatelessWidget {
static const double _radius = 10.0;
static const double _outerPadding = 2.0;
final int current;
final List<String> titles;
final ValueChanged<int> onTab;
final Color color;
final Color secondaryColor;
const ButtonGroup({
Key key,
this.titles,
this.onTab,
int current,
Color color,
Color secondaryColor,
}) : assert(titles != null),
current = current ?? 0,
color = color ?? Colors.blue,
secondaryColor = secondaryColor ?? Colors.white,
super(key: key);
#override
Widget build(BuildContext context) {
return Material(
color: color,
borderRadius: BorderRadius.circular(_radius),
child: Padding(
padding: const EdgeInsets.all(_outerPadding),
child: ClipRRect(
borderRadius: BorderRadius.circular(_radius - _outerPadding),
child: IntrinsicHeight(
child: Row(
mainAxisSize: MainAxisSize.min,
children: _buttonList(),
),
),
),
),
);
}
List<Widget> _buttonList() {
final buttons = <Widget>[];
for (int i = 0; i < titles.length; i++) {
buttons.add(_button(titles[i], i));
buttons.add(
VerticalDivider(
width: 1.0,
color: (i == current || i + 1 == current) ? color : secondaryColor,
thickness: 1.5,
indent: 5.0,
endIndent: 5.0,
),
);
}
buttons.removeLast();
return buttons;
}
Widget _button(String title, int index) {
if (index == this.current)
return _activeButton(title);
else
return _inActiveButton(title, index);
}
Widget _activeButton(String title) => FlatButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
disabledColor: secondaryColor,
disabledTextColor: color,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.zero,
),
child: Text(title),
onPressed: null,
);
Widget _inActiveButton(String title, int index) => FlatButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
color: Colors.transparent,
textColor: Colors.white,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.zero,
),
child: Text(title),
onPressed: () {
if (onTab != null) onTab(index);
},
);
}
You can use it like this
ButtonGroup(
titles: ["Button1", "Button2", "Button3"],
current: index,
color: Colors.blue,
secondaryColor: Colors.white,
onTab: (selected) {
setState(() {
index = selected;
});
},
)
Example:
import 'package:flutter/material.dart';
import 'package:flutter_app_test2/btn_grp.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MainPage(),
);
}
}
class MainPage extends StatefulWidget {
#override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int current = 0;
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ButtonGroup(
titles: ["Button1", "Button2", "Button3", "Button3"],
current: current,
onTab: (selected) {
print(selected);
setState(() {
current = selected;
});
},
),
),
);
}
}
try adding following in all RaisedButton widgets:
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
and buttonPadding: EdgeInsets.all(1), in ButtonBar
Source: https://api.flutter.dev/flutter/material/MaterialTapTargetSize-class.html