我的小部件有問題
當我單擊按鈕時,其他小部件繼續移動
看看當我點擊更改名稱時其他小部件繼續移動
當我繼續點擊加號按鈕時,其他小部件繼續移動
我正在嘗試一切我嘗試使用 Fitbox 添加小部件,我嘗試在 continer 中添加小部件并嘗試
列中的列并嘗試行中的行并嘗試行中的列:(
這是我所有的代碼:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => MyCounter()),
ChangeNotifierProvider(create: (context) => MyName()),
],
child: MaterialApp(
theme: ThemeData.dark(),
home: const Home(),
),
);
}
}
class Home extends StatelessWidget {
const Home({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
context.watch<MyCounter>().count.toString(),
),
const SizedBox(width: 99),
Text(
context.watch<MyName>().name,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
context.read<MyCounter>().plusNum();
},
child: const Text('Plus'),
),
const SizedBox(width: 33),
ElevatedButton(
onPressed: () {
context.read<MyName>().changeName();
},
child: const Text('Change Name'),
),
],
),
],
),
);
}
}
class MyCounter with ChangeNotifier {
int count = 0;
plusNum() {
count ;
notifyListeners();
}
}
class MyName with ChangeNotifier {
String name = 'Name12';
changeName() {
name = 'Name678';
notifyListeners();
}
}
uj5u.com熱心網友回復:
您遇到該問題的原因是您的小部件樹結構,將其更改為:
Row(
children: [
Column(
children: [
Text(
context.watch<MyCounter>().count.toString(),
),
ElevatedButton(
onPressed: () {
context.read<MyCounter>().plusNum();
},
child: const Text('Plus'),
),
],
),
SizedBox(
width: 33,
),
Column(
children: [
Text(
context.watch<MyName>().name,
),
ElevatedButton(
onPressed: () {
context.read<MyName>().changeName();
},
child: const Text('Change Name'),
),
],
),
],
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/515998.html
標籤:扑镖颤振布局