1

Hi good day i have a value enum in my mysql pending and done i want to display in my flutter app the the first letter is capitalize like Pending or if if it is done Done

                              Text('Status:\t' + x.status,
                                       style: TextStyle(
                                            fontSize: 25.0,
                                            fontWeight: FontWeight.bold,
                                            color: x.status == 'pending'
                                                ? Colors.red
                                                : Colors.green)),

1 Answers1

0

Check out this example that I have created.

import 'package:flutter/material.dart';

import 'capitalize.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SampleApp(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class SampleApp extends StatefulWidget {
  @override
  _SampleAppState createState() => _SampleAppState();
}

class _SampleAppState extends State<SampleApp> {


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Your heading'),
        ),
        body: Container(
            child:  Column(
                    children: <Widget>[
                     Text("someString".capitalize())
                    ],
                  )));
  }
}



This is the dart file: capitalize.dart



extension StringExtension on String {
    String capitalize() {
      return "${this[0].toUpperCase()}${this.substring(1)}";
    }
} 

for the extension to work you should have sdk at minimum at 2.7.0 in pubspec.yaml

environment:
  sdk: ">=2.7.0 <3.0.0"

even if you face any problem for undefined class extension then check the below link Flutter extension-methods not working, it says "undefined class" and "requires the extension-methods language feature"

Sagar Acharya
  • 3,397
  • 4
  • 12
  • 34