Skip to content

Instantly share code, notes, and snippets.

@acodeb
Created March 16, 2018 16:14
Show Gist options
  • Save acodeb/3e6c0d7f70e112ea9c39f34c8bf38946 to your computer and use it in GitHub Desktop.
Save acodeb/3e6c0d7f70e112ea9c39f34c8bf38946 to your computer and use it in GitHub Desktop.
Flutter: Buttons inside ListView. Button onPressed will change the color of the button
import 'package:flutter/material.dart';
void main() => runApp(
new MaterialApp(home: new MyApp())
);
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
static var btnPressed;
void onPressed(btnNumber){
setState(() {
btnPressed = btnNumber;
});
}
Widget createButton(btnName) {
var clr = Colors.blue;
if(btnPressed != null && btnPressed == btnName) {
clr = Colors.yellow;
}
Widget btn = new FlatButton(
color: clr,
disabledColor: Colors.grey,
child: new Text(btnName, style: new TextStyle(fontSize: 20.0),),
onPressed: () => onPressed(btnName),
);
return btn;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Container(
alignment: Alignment.center,
height: 100.0,
//width: 200.0,
padding: const EdgeInsets.all(10.0),
child: new ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
createButton('b1'),
new Padding(padding: const EdgeInsets.only(right: 10.0),),
createButton('b2'),
new Padding(padding: const EdgeInsets.only(right: 10.0),),
createButton('b3'),
new Padding(padding: const EdgeInsets.only(right: 10.0),),
createButton('b4'),
new Padding(padding: const EdgeInsets.only(right: 10.0),),
createButton('b5'),
],
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment