Your First Flutter App: Polishing the App

Apr 12 2022 · Dart 2.14.1, Flutter 2.5, Visual Studio Code

Part 3: Style the App

23. Style Buttons

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 22. Style Text Next episode: 24. Style the Slider

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

At this point, Bullseye is starting to look great, but we still need to add some styling to the buttons. In the previous episode, we styled Text widgets by creating style classes that we applied to the widgets.

import 'package:flutter/material.dart';
class StyledButton extends StatelessWidget {
  const StyledButton({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      
    );
  }
}
final IconData icon;
final GestureTapCallback onPressed;
const StyledButton({Key? key, required this.icon, required this.onPressed})
      : super(key: key);
    return RawMaterialButton(
      fillColor: Colors.black,
      splashColor: Colors.redAccent)
    return RawMaterialButton(
      fillColor: Colors.black, // old
      splashColor: Colors.redAccent, // old
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Icon(
          icon,
          color: Colors.white,
        ),
      ),
    );
    return RawMaterialButton(
      fillColor: Colors.black,
      splashColor: Colors.redAccent,
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Icon(
          icon,
          color: Colors.white,
        ),
      ), // old code
      onPressed: onPressed,
      shape: const CircleBorder(side: BorderSide(color: Colors.white)),
    );
import 'styled_button.dart';
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[ // old code
        StyledButton(
            icon: Icons.refresh,
            onPressed: () {
            onStartOver();
            },
        ),
Padding(
    padding: const EdgeInsets.only(left: 32, right: 32.0),
    child: Column(
    children: <Widget>[
        Text('Round:', style: LabelTextStyle.bodyText1(context)),
        Text('$round', style: ScoreNumberTextStyle.headline4(context)),
    ],
    ),
), // old code
StyledButton(
    icon: Icons.info,
    onPressed: () {},
),
import 'package:flutter/material.dart';
class HitMeButton extends StatelessWidget {
  const HitMeButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
final String text;
final GestureTapCallback onPressed;
const HitMeButton({Key? key, required this.text, required this.onPressed})
      : super(key: key);
return RawMaterialButton(
    fillColor: Colors.red[700],
    splashColor: Colors.redAccent,
    child: Padding(
        padding: const EdgeInsets.all(14.0),
        child: Text(
            text,
            maxLines: 1,
            style: const TextStyle(
                color: Colors.white,
                fontWeight: FontWeight.bold,
                letterSpacing: 2.0,
            ),
        ),
    ),
 @override
  Widget build(BuildContext context) {
    return RawMaterialButton(
      fillColor: Colors.red[700],
      splashColor: Colors.redAccent,
      child: Padding(
        padding: const EdgeInsets.all(14.0),
        child: Text(
          text,
          maxLines: 1,
          style: const TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.bold,
            letterSpacing: 2.0,
          ),
        ),
      ), // old code
      onPressed: onPressed,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12.0),
        side: const BorderSide(
          color: Colors.white,
        ),
      ),
    );
  }
import 'hit_me_button.dart';
HitMeButton(
    text: 'HIT ME',
    onPressed: () {
        _showAlert(context);
    },
),
import 'styled_button.dart';
var okButton = StyledButton(
    icon: Icons.close,
    onPressed: () {
    Navigator.of(context).pop();
    setState(() {
        _model.totalScore += _pointsForCurrentRound();
        _model.target = Random().nextInt(100) + 1;
        _model.round += 1;
    });
    },
);