Answers for "state widget flutter"

4

flutter stateful widget

class YellowBird extends StatefulWidget {
  const YellowBird({ Key key }) : super(key: key);

  @override
  _YellowBirdState createState() => _YellowBirdState();
}

class _YellowBirdState extends State<YellowBird> {
  @override
  Widget build(BuildContext context) {
    return Container(color: const Color(0xFFFFE306));
  }
}
Posted by: Guest on May-10-2020
7

conditionalstatement in widget flutter

Container(
  color: Colors.white,
  child: ('condition')
  ? Widget1(...)
  : Widget2(...)
)
Posted by: Guest on October-31-2020
2

make stateful widget flutter

class MyClass extends StatefulWidget {
	@override
    MyClassState createState() => new MyClassState();
}

class MyClassState extends State<MyClass> {
	//Widgets and other code here
}
Posted by: Guest on May-13-2020
3

flutter state management

// providers/provider.dart

class Products with ChangeNotifier {
  List<Product> _items = [
    ...dummy data
  ];

  List<Product> get items {
    return [..._items];
  }
  
  Product findById(String id) {		//this func will be used for getting product with id
    return _items.firstWhere((element) => element.id == id);
  }
}


// main.dart (this needs to be the topmost file wwhose children need to access the state

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (ctx) => Products(),		// pass here the provider you want to access
      child: MaterialApp(
        title: 'Flutter Demo',
        home: ProductsOverviewScreen(),
      ),
    );
  }
}


// in the file you want to access the state

class ProductDetailScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final productId = ModalRoute.of(context)!.settings.arguments as String;
    final loadedProduct =
        Provider.of<Products>(context).findById(productId);

    return Scaffold(
      appBar: AppBar(
        title: Text(loadedProduct.title),
      ),
    );
  }
}
Posted by: Guest on July-06-2021

Code answers related to "Dart"

Browse Popular Code Answers by Language